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