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 | |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 19 | import android.Manifest; |
| 20 | import android.annotation.RequiresPermission; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
| 22 | import android.annotation.SdkConstant.SdkConstantType; |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 23 | import android.annotation.SystemApi; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 24 | import android.content.Context; |
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 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 222 | private BluetoothAdapter mAdapter; |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 223 | private final BluetoothProfileConnector<IBluetoothHidHost> mProfileConnector = |
| 224 | new BluetoothProfileConnector(this, BluetoothProfile.HID_HOST, |
| 225 | "BluetoothHidHost", IBluetoothHidHost.class.getName()) { |
| 226 | @Override |
| 227 | public IBluetoothHidHost getServiceInterface(IBinder service) { |
| 228 | return IBluetoothHidHost.Stub.asInterface(Binder.allowBlocking(service)); |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 229 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 230 | }; |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 231 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 232 | /** |
Hansong Zhang | c26c76c | 2017-10-20 15:55:59 -0700 | [diff] [blame] | 233 | * Create a BluetoothHidHost proxy object for interacting with the local |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 234 | * Bluetooth Service which handles the InputDevice profile |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 235 | */ |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 236 | /*package*/ BluetoothHidHost(Context context, ServiceListener listener) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 237 | mAdapter = BluetoothAdapter.getDefaultAdapter(); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 238 | mProfileConnector.connect(context, listener); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 241 | /*package*/ void close() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 242 | if (VDBG) log("close()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 243 | mProfileConnector.disconnect(); |
| 244 | } |
fredc | 0f42037 | 2012-04-12 00:02:00 -0700 | [diff] [blame] | 245 | |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 246 | private IBluetoothHidHost getService() { |
| 247 | return mProfileConnector.getService(); |
Jaikumar Ganesh | 9bb2751 | 2011-11-28 09:59:08 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 250 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 251 | * Initiate connection to a profile of the remote bluetooth device. |
| 252 | * |
| 253 | * <p> The system supports connection to multiple input devices. |
| 254 | * |
| 255 | * <p> This API returns false in scenarios like the profile on the |
| 256 | * device is already connected or Bluetooth is not turned on. |
| 257 | * When this API returns true, it is guaranteed that |
| 258 | * connection state intent for the profile will be broadcasted with |
| 259 | * the state. Users can get the connection state of the profile |
| 260 | * from this intent. |
| 261 | * |
| 262 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 263 | * permission. |
| 264 | * |
| 265 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 266 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 267 | * @hide |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 268 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 269 | public boolean connect(BluetoothDevice device) { |
| 270 | if (DBG) log("connect(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 271 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 272 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 273 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 274 | return service.connect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 275 | } catch (RemoteException e) { |
| 276 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 277 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 278 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 279 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 280 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 284 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 285 | * Initiate disconnection from a profile |
| 286 | * |
| 287 | * <p> This API will return false in scenarios like the profile on the |
| 288 | * Bluetooth device is not in connected state etc. When this API returns, |
| 289 | * true, it is guaranteed that the connection state change |
| 290 | * intent will be broadcasted with the state. Users can get the |
| 291 | * disconnection state of the profile from this intent. |
| 292 | * |
| 293 | * <p> If the disconnection is initiated by a remote device, the state |
| 294 | * will transition from {@link #STATE_CONNECTED} to |
| 295 | * {@link #STATE_DISCONNECTED}. If the disconnect is initiated by the |
| 296 | * host (local) device the state will transition from |
| 297 | * {@link #STATE_CONNECTED} to state {@link #STATE_DISCONNECTING} to |
| 298 | * state {@link #STATE_DISCONNECTED}. The transition to |
| 299 | * {@link #STATE_DISCONNECTING} can be used to distinguish between the |
| 300 | * two scenarios. |
| 301 | * |
| 302 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} |
| 303 | * permission. |
| 304 | * |
| 305 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 306 | * @return false on immediate error, true otherwise |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 307 | * @hide |
| 308 | */ |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 309 | public boolean disconnect(BluetoothDevice device) { |
| 310 | if (DBG) log("disconnect(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 311 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 312 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 313 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 314 | return service.disconnect(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 315 | } catch (RemoteException e) { |
| 316 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 317 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 318 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 319 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 320 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 321 | return false; |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 325 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 326 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 327 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 328 | public List<BluetoothDevice> getConnectedDevices() { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 329 | if (VDBG) log("getConnectedDevices()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 330 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 331 | if (service != null && isEnabled()) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 332 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 333 | return service.getConnectedDevices(); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 334 | } catch (RemoteException e) { |
| 335 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 336 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 337 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 338 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 339 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 340 | return new ArrayList<BluetoothDevice>(); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 344 | * {@inheritDoc} |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 345 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 346 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 347 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 348 | if (VDBG) log("getDevicesMatchingStates()"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 349 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 350 | if (service != null && isEnabled()) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 351 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 352 | return service.getDevicesMatchingConnectionStates(states); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 353 | } catch (RemoteException e) { |
| 354 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 355 | return new ArrayList<BluetoothDevice>(); |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 356 | } |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 357 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 358 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 359 | return new ArrayList<BluetoothDevice>(); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * {@inheritDoc} |
| 364 | */ |
Jack He | 2992cd0 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 365 | @Override |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 366 | public int getConnectionState(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 367 | if (VDBG) log("getState(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 368 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 369 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 370 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 371 | return service.getConnectionState(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 372 | } catch (RemoteException e) { |
| 373 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 374 | return BluetoothProfile.STATE_DISCONNECTED; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 375 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 376 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 377 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 378 | return BluetoothProfile.STATE_DISCONNECTED; |
| 379 | } |
| 380 | |
| 381 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 382 | * Set priority of the profile |
| 383 | * |
| 384 | * <p> The device should already be paired. |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 385 | * Priority can be one of {@link #PRIORITY_ON} or {@link #PRIORITY_OFF}, |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 386 | * |
| 387 | * @param device Paired bluetooth device |
| 388 | * @param priority |
| 389 | * @return true if priority is set, false on error |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 390 | * @hide |
| 391 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 392 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 393 | public boolean setPriority(BluetoothDevice device, int priority) { |
| 394 | if (DBG) log("setPriority(" + device + ", " + priority + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 395 | return setConnectionPolicy(device, BluetoothAdapter.priorityToConnectionPolicy(priority)); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Set connection policy of the profile |
| 400 | * |
| 401 | * <p> The device should already be paired. |
| 402 | * Connection policy can be one of {@link #CONNECTION_POLICY_ALLOWED}, |
| 403 | * {@link #CONNECTION_POLICY_FORBIDDEN}, {@link #CONNECTION_POLICY_UNKNOWN} |
| 404 | * |
| 405 | * @param device Paired bluetooth device |
| 406 | * @param connectionPolicy is the connection policy to set to for this profile |
| 407 | * @return true if connectionPolicy is set, false on error |
| 408 | * @hide |
| 409 | */ |
| 410 | @SystemApi |
| 411 | @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) |
| 412 | public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) { |
| 413 | if (DBG) log("setConnectionPolicy(" + device + ", " + connectionPolicy + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 414 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 415 | if (service != null && isEnabled() && isValidDevice(device)) { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 416 | if (connectionPolicy != BluetoothProfile.CONNECTION_POLICY_FORBIDDEN |
| 417 | && connectionPolicy != BluetoothProfile.CONNECTION_POLICY_ALLOWED) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 418 | return false; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 419 | } |
| 420 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 421 | return service.setConnectionPolicy(device, connectionPolicy); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 422 | } catch (RemoteException e) { |
| 423 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 424 | return false; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 425 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 426 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 427 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
| 431 | /** |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 432 | * Get the priority of the profile. |
| 433 | * |
| 434 | * <p> The priority can be any of: |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 435 | * {@link #PRIORITY_OFF}, {@link #PRIORITY_ON}, {@link #PRIORITY_UNDEFINED} |
Jaikumar Ganesh | f878916 | 2011-05-26 13:56:40 -0700 | [diff] [blame] | 436 | * |
| 437 | * @param device Bluetooth device |
| 438 | * @return priority of the device |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 439 | * @hide |
| 440 | */ |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 441 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 442 | public int getPriority(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 443 | if (VDBG) log("getPriority(" + device + ")"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 444 | return BluetoothAdapter.connectionPolicyToPriority(getConnectionPolicy(device)); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Get the connection policy of the profile. |
| 449 | * |
| 450 | * <p> The connection policy can be any of: |
| 451 | * {@link #CONNECTION_POLICY_ALLOWED}, {@link #CONNECTION_POLICY_FORBIDDEN}, |
| 452 | * {@link #CONNECTION_POLICY_UNKNOWN} |
| 453 | * |
| 454 | * @param device Bluetooth device |
| 455 | * @return connection policy of the device |
| 456 | * @hide |
| 457 | */ |
| 458 | @SystemApi |
| 459 | @RequiresPermission(Manifest.permission.BLUETOOTH) |
| 460 | public int getConnectionPolicy(BluetoothDevice device) { |
| 461 | if (VDBG) log("getConnectionPolicy(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 462 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 463 | if (service != null && isEnabled() && isValidDevice(device)) { |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 464 | try { |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 465 | return service.getConnectionPolicy(device); |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 466 | } catch (RemoteException e) { |
| 467 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 468 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Matthew Xie | bf246ef | 2012-03-21 23:15:06 -0700 | [diff] [blame] | 469 | } |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 470 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 471 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Rahul Sabnis | df1ef4a | 2019-11-27 18:09:33 -0800 | [diff] [blame^] | 472 | return BluetoothProfile.CONNECTION_POLICY_FORBIDDEN; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | private boolean isEnabled() { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 476 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
Jaikumar Ganesh | 4ab0e77 | 2011-02-18 14:52:32 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 479 | private static boolean isValidDevice(BluetoothDevice device) { |
| 480 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Initiate virtual unplug for a HID input device. |
| 485 | * |
| 486 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 487 | * |
| 488 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 489 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 490 | * @hide |
| 491 | */ |
| 492 | public boolean virtualUnplug(BluetoothDevice device) { |
| 493 | if (DBG) log("virtualUnplug(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 494 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 495 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 496 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 497 | return service.virtualUnplug(device); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 498 | } catch (RemoteException e) { |
| 499 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 504 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 505 | return false; |
| 506 | |
| 507 | } |
| 508 | |
| 509 | /** |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 510 | * Send Get_Protocol_Mode command to the connected HID input device. |
| 511 | * |
| 512 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 513 | * |
| 514 | * @param device Remote Bluetooth Device |
| 515 | * @return false on immediate error, true otherwise |
| 516 | * @hide |
| 517 | */ |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 518 | public boolean getProtocolMode(BluetoothDevice device) { |
Matthew Xie | 563e414 | 2012-10-09 22:10:37 -0700 | [diff] [blame] | 519 | if (VDBG) log("getProtocolMode(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 520 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 521 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 522 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 523 | return service.getProtocolMode(device); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 524 | } catch (RemoteException e) { |
| 525 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 526 | return false; |
| 527 | } |
| 528 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 529 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 530 | return false; |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
| 534 | * Send Set_Protocol_Mode command to the connected HID input device. |
| 535 | * |
| 536 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 537 | * |
| 538 | * @param device Remote Bluetooth Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 539 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 540 | * @hide |
| 541 | */ |
| 542 | public boolean setProtocolMode(BluetoothDevice device, int protocolMode) { |
| 543 | if (DBG) log("setProtocolMode(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 544 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 545 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 546 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 547 | return service.setProtocolMode(device, protocolMode); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 548 | } catch (RemoteException e) { |
| 549 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 550 | return false; |
| 551 | } |
| 552 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 553 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Send Get_Report command to the connected HID input device. |
| 559 | * |
| 560 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 561 | * |
| 562 | * @param device Remote Bluetooth Device |
| 563 | * @param reportType Report type |
| 564 | * @param reportId Report ID |
| 565 | * @param bufferSize Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 566 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 567 | * @hide |
| 568 | */ |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 569 | public boolean getReport(BluetoothDevice device, byte reportType, byte reportId, |
| 570 | int bufferSize) { |
| 571 | if (VDBG) { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 572 | log("getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId |
| 573 | + "bufferSize=" + bufferSize); |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 574 | } |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 575 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 576 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 577 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 578 | return service.getReport(device, reportType, reportId, bufferSize); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 579 | } catch (RemoteException e) { |
| 580 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 581 | return false; |
| 582 | } |
| 583 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 584 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 585 | return false; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Send Set_Report command to the connected HID input device. |
| 590 | * |
| 591 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 592 | * |
| 593 | * @param device Remote Bluetooth Device |
| 594 | * @param reportType Report type |
| 595 | * @param report Report receiving buffer size |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 596 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 597 | * @hide |
| 598 | */ |
| 599 | public boolean setReport(BluetoothDevice device, byte reportType, String report) { |
Mike J. Chen | 8faffa4 | 2014-03-04 17:27:16 -0800 | [diff] [blame] | 600 | if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 601 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 602 | if (service != null && isEnabled() && isValidDevice(device)) { |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 603 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 604 | return service.setReport(device, reportType, report); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 605 | } catch (RemoteException e) { |
| 606 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 607 | return false; |
| 608 | } |
| 609 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 610 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Send Send_Data command to the connected HID input device. |
| 616 | * |
| 617 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 618 | * |
| 619 | * @param device Remote Bluetooth Device |
Ang Li | 707fd39 | 2014-06-25 13:16:01 -0700 | [diff] [blame] | 620 | * @param report Report to send |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 621 | * @return false on immediate error, true otherwise |
Priti Aghera | 349e62f | 2012-04-09 12:13:17 -0700 | [diff] [blame] | 622 | * @hide |
| 623 | */ |
| 624 | public boolean sendData(BluetoothDevice device, String report) { |
| 625 | if (DBG) log("sendData(" + device + "), report=" + report); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 626 | final IBluetoothHidHost service = getService(); |
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.sendData(device, report); |
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 | } |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 638 | |
| 639 | /** |
| 640 | * Send Get_Idle_Time 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 |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 645 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 646 | * @hide |
| 647 | */ |
| 648 | public boolean getIdleTime(BluetoothDevice device) { |
| 649 | if (DBG) log("getIdletime(" + device + ")"); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 650 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 651 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 652 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 653 | return service.getIdleTime(device); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 654 | } catch (RemoteException e) { |
| 655 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 656 | return false; |
| 657 | } |
| 658 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 659 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 660 | return false; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Send Set_Idle_Time command to the connected HID input device. |
| 665 | * |
| 666 | * <p>Requires {@link android.Manifest.permission#BLUETOOTH_ADMIN} permission. |
| 667 | * |
| 668 | * @param device Remote Bluetooth Device |
| 669 | * @param idleTime Idle time to be set on HID Device |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 670 | * @return false on immediate error, true otherwise |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 671 | * @hide |
| 672 | */ |
| 673 | public boolean setIdleTime(BluetoothDevice device, byte idleTime) { |
| 674 | if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime); |
Ugo Yu | d011546 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 675 | final IBluetoothHidHost service = getService(); |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 676 | if (service != null && isEnabled() && isValidDevice(device)) { |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 677 | try { |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 678 | return service.setIdleTime(device, idleTime); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 679 | } catch (RemoteException e) { |
| 680 | Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); |
| 681 | return false; |
| 682 | } |
| 683 | } |
Jack He | 16eeac3 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 684 | if (service == null) Log.w(TAG, "Proxy not attached to service"); |
Hemant Gupta | cef9ce3 | 2013-07-30 16:09:20 +0530 | [diff] [blame] | 685 | return false; |
| 686 | } |
| 687 | |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 688 | private static void log(String msg) { |
Jack He | a355e5e | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 689 | Log.d(TAG, msg); |
Jaikumar Ganesh | 545e670 | 2010-06-04 10:23:03 -0700 | [diff] [blame] | 690 | } |
| 691 | } |