Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 1 | /* |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.bluetooth; |
| 18 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 25 | import android.os.Binder; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.RemoteException; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | |
Jaikumar Ganesh | 5a1e4cf | 2010-10-18 17:05:09 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 32 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 33 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 34 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 35 | * This class provides the public APIs to control the Bluetooth Input |
| 36 | * Device Profile. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 37 | * |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 38 | * <p>BluetoothHidHost is a proxy object for controlling the Bluetooth |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 39 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 40 | * the BluetoothHidHost proxy object. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 41 | * |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 42 | * <p>Each method is protected with its appropriate permission. |
| 43 | * |
| 44 | * @hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 45 | */ |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 46 | public final class BluetoothHidHost implements BluetoothProfile { |
| 47 | private static final String TAG = "BluetoothHidHost"; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 48 | private static final boolean DBG = true; |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 49 | private static final boolean VDBG = false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 50 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Intent used to broadcast the change in connection state of the Input |
| 53 | * Device profile. |
| 54 | * |
| 55 | * <p>This intent will have 3 extras: |
| 56 | * <ul> |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 57 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 58 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 59 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 60 | * </ul> |
| 61 | * |
| 62 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 63 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 64 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| 65 | * |
| 66 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission to |
| 67 | * receive. |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 68 | */ |
| 69 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 70 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 71 | "android.bluetooth.input.profile.action.CONNECTION_STATE_CHANGED"; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 72 | |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 73 | /** |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 74 | * @hide |
| 75 | */ |
| 76 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 77 | public static final String ACTION_PROTOCOL_MODE_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 78 | "android.bluetooth.input.profile.action.PROTOCOL_MODE_CHANGED"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 79 | |
Mike J. Chen | d96d5cf | 2014-01-27 17:55:40 -0800 | [diff] [blame] | 80 | /** |
| 81 | * @hide |
| 82 | */ |
| 83 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 84 | public static final String ACTION_HANDSHAKE = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 85 | "android.bluetooth.input.profile.action.HANDSHAKE"; |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @hide |
| 89 | */ |
| 90 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike J. Chen | d96d5cf | 2014-01-27 17:55:40 -0800 | [diff] [blame] | 91 | public static final String ACTION_REPORT = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 92 | "android.bluetooth.input.profile.action.REPORT"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * @hide |
| 96 | */ |
| 97 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 98 | public static final String ACTION_VIRTUAL_UNPLUG_STATUS = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 99 | "android.bluetooth.input.profile.action.VIRTUAL_UNPLUG_STATUS"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 100 | |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 101 | /** |
| 102 | * @hide |
| 103 | */ |
| 104 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 105 | public static final String ACTION_IDLE_TIME_CHANGED = |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 106 | "android.bluetooth.input.profile.action.IDLE_TIME_CHANGED"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 109 | * Return codes for the connect and disconnect Bluez / Dbus calls. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 110 | * |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 111 | * @hide |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 112 | */ |
| 113 | public static final int INPUT_DISCONNECT_FAILED_NOT_CONNECTED = 5000; |
| 114 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 115 | /** |
| 116 | * @hide |
| 117 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 118 | public static final int INPUT_CONNECT_FAILED_ALREADY_CONNECTED = 5001; |
| 119 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 120 | /** |
| 121 | * @hide |
| 122 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 123 | public static final int INPUT_CONNECT_FAILED_ATTEMPT_FAILED = 5002; |
| 124 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 125 | /** |
| 126 | * @hide |
| 127 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 128 | public static final int INPUT_OPERATION_GENERIC_FAILURE = 5003; |
| 129 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 130 | /** |
| 131 | * @hide |
| 132 | */ |
Jaikumar Ganesh | fbe807d | 2011-01-19 13:59:32 -0800 | [diff] [blame] | 133 | public static final int INPUT_OPERATION_SUCCESS = 5004; |
| 134 | |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 135 | /** |
| 136 | * @hide |
| 137 | */ |
| 138 | public static final int PROTOCOL_REPORT_MODE = 0; |
| 139 | |
| 140 | /** |
| 141 | * @hide |
| 142 | */ |
| 143 | public static final int PROTOCOL_BOOT_MODE = 1; |
| 144 | |
| 145 | /** |
| 146 | * @hide |
| 147 | */ |
| 148 | public static final int PROTOCOL_UNSUPPORTED_MODE = 255; |
| 149 | |
| 150 | /* int reportType, int reportType, int bufferSize */ |
| 151 | /** |
| 152 | * @hide |
| 153 | */ |
Mike J. Chen | 1b47f7c | 2014-01-27 16:27:04 -0800 | [diff] [blame] | 154 | public static final byte REPORT_TYPE_INPUT = 1; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * @hide |
| 158 | */ |
Mike J. Chen | 1b47f7c | 2014-01-27 16:27:04 -0800 | [diff] [blame] | 159 | public static final byte REPORT_TYPE_OUTPUT = 2; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * @hide |
| 163 | */ |
Mike J. Chen | 1b47f7c | 2014-01-27 16:27:04 -0800 | [diff] [blame] | 164 | public static final byte REPORT_TYPE_FEATURE = 3; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * @hide |
| 168 | */ |
| 169 | public static final int VIRTUAL_UNPLUG_STATUS_SUCCESS = 0; |
| 170 | |
| 171 | /** |
| 172 | * @hide |
| 173 | */ |
| 174 | public static final int VIRTUAL_UNPLUG_STATUS_FAIL = 1; |
| 175 | |
| 176 | /** |
| 177 | * @hide |
| 178 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 179 | public static final String EXTRA_PROTOCOL_MODE = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 180 | "android.bluetooth.BluetoothHidHost.extra.PROTOCOL_MODE"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * @hide |
| 184 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 185 | public static final String EXTRA_REPORT_TYPE = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 186 | "android.bluetooth.BluetoothHidHost.extra.REPORT_TYPE"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * @hide |
| 190 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 191 | public static final String EXTRA_REPORT_ID = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 192 | "android.bluetooth.BluetoothHidHost.extra.REPORT_ID"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * @hide |
| 196 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 197 | public static final String EXTRA_REPORT_BUFFER_SIZE = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 198 | "android.bluetooth.BluetoothHidHost.extra.REPORT_BUFFER_SIZE"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * @hide |
| 202 | */ |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 203 | public static final String EXTRA_REPORT = "android.bluetooth.BluetoothHidHost.extra.REPORT"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * @hide |
| 207 | */ |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 208 | public static final String EXTRA_STATUS = "android.bluetooth.BluetoothHidHost.extra.STATUS"; |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * @hide |
| 212 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 213 | public static final String EXTRA_VIRTUAL_UNPLUG_STATUS = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 214 | "android.bluetooth.BluetoothHidHost.extra.VIRTUAL_UNPLUG_STATUS"; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 215 | |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 216 | /** |
| 217 | * @hide |
| 218 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 219 | public static final String EXTRA_IDLE_TIME = |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 220 | "android.bluetooth.BluetoothHidHost.extra.IDLE_TIME"; |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 221 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 222 | private Context mContext; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 223 | private ServiceListener mServiceListener; |
| 224 | private BluetoothAdapter mAdapter; |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 225 | private volatile IBluetoothHidHost mService; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 226 | |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 227 | private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 228 | new IBluetoothStateChangeCallback.Stub() { |
| 229 | public void onBluetoothStateChange(boolean up) { |
| 230 | if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up); |
| 231 | if (!up) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 232 | if (VDBG) Log.d(TAG, "Unbinding service..."); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 233 | synchronized (mConnection) { |
| 234 | try { |
| 235 | mService = null; |
| 236 | mContext.unbindService(mConnection); |
| 237 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 238 | Log.e(TAG, "", re); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } else { |
| 242 | synchronized (mConnection) { |
| 243 | try { |
| 244 | if (mService == null) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 245 | if (VDBG) Log.d(TAG, "Binding service..."); |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 246 | doBind(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 247 | } |
| 248 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 249 | Log.e(TAG, "", re); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 254 | }; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 255 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 256 | /** |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 257 | * Create a BluetoothHidHost proxy object for interacting with the local |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 258 | * Bluetooth Service which handles the InputDevice profile |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 259 | */ |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 260 | /*package*/ BluetoothHidHost(Context context, ServiceListener l) { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 261 | mContext = context; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 262 | mServiceListener = l; |
| 263 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 264 | |
| 265 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 266 | if (mgr != null) { |
| 267 | try { |
| 268 | mgr.registerStateChangeCallback(mBluetoothStateChangeCallback); |
| 269 | } catch (RemoteException e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 270 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 274 | doBind(); |
| 275 | } |
| 276 | |
| 277 | boolean doBind() { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 278 | Intent intent = new Intent(IBluetoothHidHost.class.getName()); |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 279 | ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0); |
| 280 | intent.setComponent(comp); |
Dianne Hackborn | 466ce96 | 2014-03-19 18:06:58 -0700 | [diff] [blame] | 281 | if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0, |
| 282 | android.os.Process.myUserHandle())) { |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 283 | Log.e(TAG, "Could not bind to Bluetooth HID Service with " + intent); |
| 284 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 285 | } |
Dianne Hackborn | 221ea89 | 2013-08-04 16:50:16 -0700 | [diff] [blame] | 286 | return true; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 289 | /*package*/ void close() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 290 | if (VDBG) log("close()"); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 291 | IBluetoothManager mgr = mAdapter.getBluetoothManager(); |
| 292 | if (mgr != null) { |
| 293 | try { |
| 294 | mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback); |
| 295 | } catch (Exception e) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 296 | Log.e(TAG, "", e); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | synchronized (mConnection) { |
| 301 | if (mService != null) { |
| 302 | try { |
| 303 | mService = null; |
| 304 | mContext.unbindService(mConnection); |
| 305 | } catch (Exception re) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 306 | Log.e(TAG, "", re); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 307 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 308 | } |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 309 | } |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 310 | mServiceListener = null; |
| 311 | } |
| 312 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 313 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 314 | * Initiate connection to a profile of the remote bluetooth device. |
| 315 | * |
| 316 | * <p> The system supports connection to multiple input devices. |
| 317 | * |
| 318 | * <p> This API returns false in scenarios like the profile on the |
| 319 | * device is already connected or Bluetooth is not turned on. |
| 320 | * When this API returns true, it is guaranteed that |
| 321 | * connection state intent for the profile will be broadcasted with |
| 322 | * the state. Users can get the connection state of the profile |
| 323 | * from this intent. |
| 324 | * |
| 325 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 326 | * permission. |
| 327 | * |
| 328 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 329 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 330 | * @hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 331 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 332 | public boolean connect(BluetoothDevice device) { |
| 333 | if (DBG) log("connect(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 334 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 335 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 336 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 337 | return service.connect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 338 | } catch (RemoteException e) { |
| 339 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 340 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 341 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 342 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 343 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 347 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 348 | * Initiate disconnection from a profile |
| 349 | * |
| 350 | * <p> This API will return false in scenarios like the profile on the |
| 351 | * Bluetooth device is not in connected state etc. When this API returns, |
| 352 | * true, it is guaranteed that the connection state change |
| 353 | * intent will be broadcasted with the state. Users can get the |
| 354 | * disconnection state of the profile from this intent. |
| 355 | * |
| 356 | * <p> If the disconnection is initiated by a remote device, the state |
| 357 | * will transition from {@link #STATE_CONNECTED} to |
| 358 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 359 | * host (local) device the state will transition from |
| 360 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 361 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 362 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 363 | * two scenarios. |
| 364 | * |
| 365 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 366 | * permission. |
| 367 | * |
| 368 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 369 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 370 | * @hide |
| 371 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 372 | public boolean disconnect(BluetoothDevice device) { |
| 373 | if (DBG) log("disconnect(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 374 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 375 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 376 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 377 | return service.disconnect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 378 | } catch (RemoteException e) { |
| 379 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 380 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 381 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 382 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 383 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 384 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 388 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 389 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 390 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 391 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 392 | if (VDBG) log("getConnectedDevices()"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 393 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 394 | if (service != null && isEnabled()) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 395 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 396 | return service.getConnectedDevices(); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 397 | } catch (RemoteException e) { |
| 398 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 399 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 400 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 401 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 402 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 403 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 407 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 408 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 409 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 410 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 411 | if (VDBG) log("getDevicesMatchingStates()"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 412 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 413 | if (service != null && isEnabled()) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 414 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 415 | return service.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 416 | } catch (RemoteException e) { |
| 417 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 418 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 419 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 420 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 421 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 422 | return new ArrayList<BluetoothDevice>(); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * {@inheritDoc} |
| 427 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 428 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 429 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 430 | if (VDBG) log("getState(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 431 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 432 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 433 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 434 | return service.getConnectionState(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 435 | } catch (RemoteException e) { |
| 436 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 437 | return BluetoothProfile.STATE_DISCONNECTED; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 438 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 439 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 440 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 441 | return BluetoothProfile.STATE_DISCONNECTED; |
| 442 | } |
| 443 | |
| 444 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 445 | * Set priority of the profile |
| 446 | * |
| 447 | * <p> The device should already be paired. |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 448 | * Priority can be one of {@link #PRIORITY_ON} or |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 449 | * {@link #PRIORITY_OFF}, |
| 450 | * |
| 451 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 452 | * permission. |
| 453 | * |
| 454 | * @param device Paired bluetooth device |
| 455 | * @param priority |
| 456 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 457 | * @hide |
| 458 | */ |
| 459 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 460 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 461 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 462 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 463 | if (priority != BluetoothProfile.PRIORITY_OFF |
| 464 | && priority != BluetoothProfile.PRIORITY_ON) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 465 | return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 466 | } |
| 467 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 468 | return service.setPriority(device, priority); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 469 | } catch (RemoteException e) { |
| 470 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 471 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 472 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 473 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 474 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | |
| 478 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 479 | * Get the priority of the profile. |
| 480 | * |
| 481 | * <p> The priority can be any of: |
| 482 | * {@link #PRIORITY_AUTO_CONNECT}, {@link #PRIORITY_OFF}, |
| 483 | * {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
| 484 | * |
| 485 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH} permission. |
| 486 | * |
| 487 | * @param device Bluetooth device |
| 488 | * @return priority of the device |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 489 | * @hide |
| 490 | */ |
| 491 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 492 | if (VDBG) log("getPriority(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 493 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 494 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 495 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 496 | return service.getPriority(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 497 | } catch (RemoteException e) { |
| 498 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 499 | return BluetoothProfile.PRIORITY_OFF; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 500 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 501 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 502 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 503 | return BluetoothProfile.PRIORITY_OFF; |
| 504 | } |
| 505 | |
Matthew Xie | 9b69399 | 2013-10-10 11:21:40 -0700 | [diff] [blame] | 506 | private final ServiceConnection mConnection = new ServiceConnection() { |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 507 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 508 | if (DBG) Log.d(TAG, "Proxy object connected"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 509 | mService = IBluetoothHidHost.Stub.asInterface(Binder.allowBlocking(service)); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 510 | |
| 511 | if (mServiceListener != null) { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 512 | mServiceListener.onServiceConnected(BluetoothProfile.HID_HOST, |
| 513 | BluetoothHidHost.this); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 516 | |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 517 | public void onServiceDisconnected(ComponentName className) { |
| 518 | if (DBG) Log.d(TAG, "Proxy object disconnected"); |
| 519 | mService = null; |
| 520 | if (mServiceListener != null) { |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 521 | mServiceListener.onServiceDisconnected(BluetoothProfile.HID_HOST); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | }; |
| 525 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 526 | private boolean isEnabled() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 527 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 530 | private static boolean isValidDevice(BluetoothDevice device) { |
| 531 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 534 | /** |
| 535 | * Initiate virtual unplug for a HID input device. |
| 536 | * |
| 537 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 538 | * |
| 539 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 540 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 541 | * @hide |
| 542 | */ |
| 543 | public boolean virtualUnplug(BluetoothDevice device) { |
| 544 | if (DBG) log("virtualUnplug(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 545 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 546 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 547 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 548 | return service.virtualUnplug(device); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 549 | } catch (RemoteException e) { |
| 550 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 551 | return false; |
| 552 | } |
| 553 | } |
| 554 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 555 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 556 | return false; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 561 | * Send Get_Protocol_Mode command to the connected HID input device. |
| 562 | * |
| 563 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 564 | * |
| 565 | * @param device Remote Bluetooth Device |
| 566 | * @return false on immediate error, true otherwise |
| 567 | * @hide |
| 568 | */ |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 569 | public boolean getProtocolMode(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 570 | if (VDBG) log("getProtocolMode(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 571 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 572 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 573 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 574 | return service.getProtocolMode(device); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 575 | } catch (RemoteException e) { |
| 576 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 577 | return false; |
| 578 | } |
| 579 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 580 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 581 | return false; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Send Set_Protocol_Mode command to the connected HID input device. |
| 586 | * |
| 587 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 588 | * |
| 589 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 590 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 591 | * @hide |
| 592 | */ |
| 593 | public boolean setProtocolMode(BluetoothDevice device, int protocolMode) { |
| 594 | if (DBG) log("setProtocolMode(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 595 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 596 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 597 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 598 | return service.setProtocolMode(device, protocolMode); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 599 | } catch (RemoteException e) { |
| 600 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 601 | return false; |
| 602 | } |
| 603 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 604 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Send Get_Report command to the connected HID input device. |
| 610 | * |
| 611 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 612 | * |
| 613 | * @param device Remote Bluetooth Device |
| 614 | * @param reportType Report type |
| 615 | * @param reportId Report ID |
| 616 | * @param bufferSize Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 617 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 618 | * @hide |
| 619 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 620 | public boolean getReport(BluetoothDevice device, byte reportType, byte reportId, |
| 621 | int bufferSize) { |
| 622 | if (VDBG) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 623 | log("getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId |
| 624 | + "bufferSize=" + bufferSize); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 625 | } |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 626 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 627 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 628 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 629 | return service.getReport(device, reportType, reportId, bufferSize); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 630 | } catch (RemoteException e) { |
| 631 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 632 | return false; |
| 633 | } |
| 634 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 635 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 636 | return false; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Send Set_Report command to the connected HID input device. |
| 641 | * |
| 642 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 643 | * |
| 644 | * @param device Remote Bluetooth Device |
| 645 | * @param reportType Report type |
| 646 | * @param report Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 647 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 648 | * @hide |
| 649 | */ |
| 650 | public boolean setReport(BluetoothDevice device, byte reportType, String report) { |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 651 | if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 652 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 653 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 654 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 655 | return service.setReport(device, reportType, report); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 656 | } catch (RemoteException e) { |
| 657 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 658 | return false; |
| 659 | } |
| 660 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 661 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 662 | return false; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Send Send_Data command to the connected HID input device. |
| 667 | * |
| 668 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 669 | * |
| 670 | * @param device Remote Bluetooth Device |
Ang Li | 707fd39 | 2014-06-25 13:16:01 -0700 | [diff] [blame] | 671 | * @param report Report to send |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 672 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 673 | * @hide |
| 674 | */ |
| 675 | public boolean sendData(BluetoothDevice device, String report) { |
| 676 | if (DBG) log("sendData(" + device + "), report=" + report); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 677 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 678 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 679 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 680 | return service.sendData(device, report); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 681 | } catch (RemoteException e) { |
| 682 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 683 | return false; |
| 684 | } |
| 685 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 686 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 687 | return false; |
| 688 | } |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * Send Get_Idle_Time command to the connected HID input device. |
| 692 | * |
| 693 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 694 | * |
| 695 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 696 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 697 | * @hide |
| 698 | */ |
| 699 | public boolean getIdleTime(BluetoothDevice device) { |
| 700 | if (DBG) log("getIdletime(" + device + ")"); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 701 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 702 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 703 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 704 | return service.getIdleTime(device); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 705 | } catch (RemoteException e) { |
| 706 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 707 | return false; |
| 708 | } |
| 709 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 710 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 711 | return false; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Send Set_Idle_Time command to the connected HID input device. |
| 716 | * |
| 717 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 718 | * |
| 719 | * @param device Remote Bluetooth Device |
| 720 | * @param idleTime Idle time to be set on HID Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 721 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 722 | * @hide |
| 723 | */ |
| 724 | public boolean setIdleTime(BluetoothDevice device, byte idleTime) { |
| 725 | if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime); |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame^] | 726 | final IBluetoothHidHost service = mService; |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 727 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 728 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 729 | return service.setIdleTime(device, idleTime); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 730 | } catch (RemoteException e) { |
| 731 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 732 | return false; |
| 733 | } |
| 734 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 735 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 739 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 740 | Log.d(TAG, msg); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 741 | } |
| 742 | } |