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