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