The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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.server; |
| 18 | |
| 19 | import android.bluetooth.BluetoothA2dp; |
| 20 | import android.bluetooth.BluetoothClass; |
| 21 | import android.bluetooth.BluetoothDevice; |
| 22 | import android.bluetooth.BluetoothError; |
| 23 | import android.bluetooth.BluetoothIntent; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 24 | import android.bluetooth.BluetoothUuid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
| 31 | import java.util.HashMap; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 32 | import java.util.UUID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * TODO: Move this to |
| 36 | * java/services/com/android/server/BluetoothEventLoop.java |
| 37 | * and make the contructor package private again. |
| 38 | * |
| 39 | * @hide |
| 40 | */ |
| 41 | class BluetoothEventLoop { |
| 42 | private static final String TAG = "BluetoothEventLoop"; |
| 43 | private static final boolean DBG = false; |
| 44 | |
| 45 | private int mNativeData; |
| 46 | private Thread mThread; |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 47 | private boolean mStarted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | private boolean mInterrupted; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 49 | private final HashMap<String, Integer> mPasskeyAgentRequestData; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 50 | private final BluetoothDeviceService mBluetoothService; |
| 51 | private final Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | private static final int EVENT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 1; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 54 | private static final int EVENT_RESTART_BLUETOOTH = 2; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | // The time (in millisecs) to delay the pairing attempt after the first |
| 57 | // auto pairing attempt fails. We use an exponential delay with |
| 58 | // INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY as the initial value and |
| 59 | // MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY as the max value. |
| 60 | private static final long INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 3000; |
| 61 | private static final long MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY = 12000; |
| 62 | |
| 63 | private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; |
| 64 | private static final String BLUETOOTH_PERM = android.Manifest.permission.BLUETOOTH; |
| 65 | |
| 66 | private final Handler mHandler = new Handler() { |
| 67 | @Override |
| 68 | public void handleMessage(Message msg) { |
| 69 | switch (msg.what) { |
| 70 | case EVENT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY: |
| 71 | String address = (String)msg.obj; |
| 72 | if (address != null) { |
| 73 | mBluetoothService.createBond(address); |
| 74 | return; |
| 75 | } |
| 76 | break; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 77 | case EVENT_RESTART_BLUETOOTH: |
Nick Pelly | 37b5a10 | 2009-03-24 20:46:18 -0700 | [diff] [blame] | 78 | mBluetoothService.restart(); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 79 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | static { classInitNative(); } |
| 85 | private static native void classInitNative(); |
| 86 | |
| 87 | /* pacakge */ BluetoothEventLoop(Context context, BluetoothDeviceService bluetoothService) { |
| 88 | mBluetoothService = bluetoothService; |
| 89 | mContext = context; |
| 90 | mPasskeyAgentRequestData = new HashMap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | initializeNativeDataNative(); |
| 92 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
| 94 | protected void finalize() throws Throwable { |
| 95 | try { |
| 96 | cleanupNativeDataNative(); |
| 97 | } finally { |
| 98 | super.finalize(); |
| 99 | } |
| 100 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 102 | /* package */ HashMap<String, Integer> getPasskeyAgentRequestData() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | return mPasskeyAgentRequestData; |
| 104 | } |
| 105 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 106 | /* package */ void start() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 108 | if (!isEventLoopRunningNative()) { |
| 109 | if (DBG) log("Starting Event Loop thread"); |
| 110 | startEventLoopNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Robert Greenwalt | 28d139f | 2009-04-02 22:41:08 -0700 | [diff] [blame] | 114 | public void stop() { |
| 115 | if (isEventLoopRunningNative()) { |
| 116 | if (DBG) log("Stopping Event Loop thread"); |
| 117 | stopEventLoopNative(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public boolean isEventLoopRunning() { |
| 122 | return isEventLoopRunningNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 125 | private void onDeviceFound(String address, String[] properties) { |
| 126 | if (properties == null) { |
| 127 | Log.e(TAG, "ERROR: Remote device properties are null"); |
| 128 | return; |
| 129 | } |
| 130 | mBluetoothService.addRemoteDeviceProperties(address, properties); |
| 131 | String rssi = mBluetoothService.getRemoteDeviceProperty(address, "RSSI"); |
| 132 | String classValue = mBluetoothService.getRemoteDeviceProperty(address, "Class"); |
Jaikumar Ganesh | 395d102 | 2009-06-19 16:12:31 -0700 | [diff] [blame] | 133 | String name = mBluetoothService.getRemoteDeviceProperty(address, "Name"); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 134 | |
| 135 | if (rssi != null && classValue != null) { |
| 136 | Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_FOUND_ACTION); |
| 137 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
Jaikumar Ganesh | 395d102 | 2009-06-19 16:12:31 -0700 | [diff] [blame] | 138 | intent.putExtra(BluetoothIntent.CLASS, Integer.valueOf(classValue)); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 139 | intent.putExtra(BluetoothIntent.RSSI, (short)Integer.valueOf(rssi).intValue()); |
Jaikumar Ganesh | 395d102 | 2009-06-19 16:12:31 -0700 | [diff] [blame] | 140 | intent.putExtra(BluetoothIntent.NAME, name); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 141 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 143 | } else { |
| 144 | log ("RSSI: " + rssi + " or ClassValue: " + classValue + |
| 145 | " for remote device: " + address + " is null"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 149 | private void onDeviceDisappeared(String address) { |
| 150 | mBluetoothService.removeRemoteDeviceProperties(address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_DISAPPEARED_ACTION); |
| 153 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
| 154 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 155 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 157 | private void onCreatePairedDeviceResult(String address, int result) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | address = address.toUpperCase(); |
| 159 | if (result == BluetoothError.SUCCESS) { |
| 160 | mBluetoothService.getBondState().setBondState(address, BluetoothDevice.BOND_BONDED); |
| 161 | if (mBluetoothService.getBondState().isAutoPairingAttemptsInProgress(address)) { |
| 162 | mBluetoothService.getBondState().clearPinAttempts(address); |
| 163 | } |
| 164 | } else if (result == BluetoothDevice.UNBOND_REASON_AUTH_FAILED && |
| 165 | mBluetoothService.getBondState().getAttempt(address) == 1) { |
| 166 | mBluetoothService.getBondState().addAutoPairingFailure(address); |
| 167 | pairingAttempt(address, result); |
| 168 | } else if (result == BluetoothDevice.UNBOND_REASON_REMOTE_DEVICE_DOWN && |
| 169 | mBluetoothService.getBondState().isAutoPairingAttemptsInProgress(address)) { |
| 170 | pairingAttempt(address, result); |
| 171 | } else { |
| 172 | mBluetoothService.getBondState().setBondState(address, |
| 173 | BluetoothDevice.BOND_NOT_BONDED, result); |
| 174 | if (mBluetoothService.getBondState().isAutoPairingAttemptsInProgress(address)) { |
| 175 | mBluetoothService.getBondState().clearPinAttempts(address); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private void pairingAttempt(String address, int result) { |
| 181 | // This happens when our initial guess of "0000" as the pass key |
| 182 | // fails. Try to create the bond again and display the pin dialog |
| 183 | // to the user. Use back-off while posting the delayed |
| 184 | // message. The initial value is |
| 185 | // INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY and the max value is |
| 186 | // MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY. If the max value is |
| 187 | // reached, display an error to the user. |
| 188 | int attempt = mBluetoothService.getBondState().getAttempt(address); |
| 189 | if (attempt * INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY > |
| 190 | MAX_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY) { |
| 191 | mBluetoothService.getBondState().clearPinAttempts(address); |
| 192 | mBluetoothService.getBondState().setBondState(address, |
| 193 | BluetoothDevice.BOND_NOT_BONDED, result); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | Message message = mHandler.obtainMessage(EVENT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY); |
| 198 | message.obj = address; |
| 199 | boolean postResult = mHandler.sendMessageDelayed(message, |
| 200 | attempt * INIT_AUTO_PAIRING_FAILURE_ATTEMPT_DELAY); |
| 201 | if (!postResult) { |
| 202 | mBluetoothService.getBondState().clearPinAttempts(address); |
| 203 | mBluetoothService.getBondState().setBondState(address, |
| 204 | BluetoothDevice.BOND_NOT_BONDED, result); |
| 205 | return; |
| 206 | } |
| 207 | mBluetoothService.getBondState().attempt(address); |
| 208 | } |
| 209 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 210 | private void onDeviceCreated(String deviceObjectPath) { |
| 211 | // do nothing. |
| 212 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 215 | private void onDeviceRemoved(String deviceObjectPath) { |
| 216 | String address = mBluetoothService.getAddressFromObjectPath(deviceObjectPath); |
| 217 | if (address != null) |
| 218 | mBluetoothService.getBondState().setBondState(address.toUpperCase(), |
| 219 | BluetoothDevice.BOND_NOT_BONDED, BluetoothDevice.UNBOND_REASON_REMOVED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 222 | /*package*/ void onPropertyChanged(String[] propValues) { |
| 223 | String name = propValues[0]; |
| 224 | if (name.equals("Name")) { |
| 225 | Intent intent = new Intent(BluetoothIntent.NAME_CHANGED_ACTION); |
| 226 | intent.putExtra(BluetoothIntent.NAME, propValues[1]); |
| 227 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 228 | mBluetoothService.setProperty(name, propValues[1]); |
| 229 | } else if (name.equals("Pairable") || name.equals("Discoverable")) { |
| 230 | String pairable = name.equals("Pairable") ? propValues[1] : |
| 231 | mBluetoothService.getProperty("Pairable"); |
| 232 | String discoverable = name.equals("Discoverable") ? propValues[1] : |
| 233 | mBluetoothService.getProperty("Discoverable"); |
| 234 | |
| 235 | // This shouldn't happen, unless Adapter Properties are null. |
| 236 | if (pairable == null || discoverable == null) |
| 237 | return; |
| 238 | |
| 239 | int mode = BluetoothDeviceService.bluezStringToScanMode( |
| 240 | pairable.equals("true"), |
| 241 | discoverable.equals("true")); |
| 242 | if (mode >= 0) { |
| 243 | Intent intent = new Intent(BluetoothIntent.SCAN_MODE_CHANGED_ACTION); |
| 244 | intent.putExtra(BluetoothIntent.SCAN_MODE, mode); |
| 245 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
| 246 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 247 | } |
| 248 | mBluetoothService.setProperty(name, propValues[1]); |
| 249 | } else if (name.equals("Discovering")) { |
| 250 | Intent intent; |
| 251 | if (propValues[1].equals("true")) { |
| 252 | mBluetoothService.setIsDiscovering(true); |
| 253 | intent = new Intent(BluetoothIntent.DISCOVERY_STARTED_ACTION); |
| 254 | } else { |
| 255 | // Stop the discovery. |
| 256 | mBluetoothService.cancelDiscovery(); |
| 257 | mBluetoothService.setIsDiscovering(false); |
| 258 | intent = new Intent(BluetoothIntent.DISCOVERY_COMPLETED_ACTION); |
| 259 | } |
| 260 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 261 | mBluetoothService.setProperty(name, propValues[1]); |
| 262 | } else if (name.equals("Devices")) { |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 263 | String value = null; |
| 264 | int len = Integer.valueOf(propValues[1]); |
| 265 | if (len > 0) { |
| 266 | value = ""; |
| 267 | for (int i = 2; i < propValues.length; i++) { |
| 268 | value = value + propValues[i] + ','; |
| 269 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 270 | } |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 271 | mBluetoothService.setProperty(name, value); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 272 | } else if (name.equals("Powered")) { |
| 273 | // bluetoothd has restarted, re-read all our properties. |
| 274 | // Note: bluez only sends this property change when it restarts. |
| 275 | if (propValues[1].equals("true")) |
| 276 | onRestartRequired(); |
| 277 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 280 | private void onDevicePropertyChanged(String deviceObjectPath, String[] propValues) { |
| 281 | String name = propValues[0]; |
| 282 | String address = mBluetoothService.getAddressFromObjectPath(deviceObjectPath); |
| 283 | if (address == null) { |
| 284 | Log.e(TAG, "onDevicePropertyChanged: Address of the remote device in null"); |
| 285 | return; |
| 286 | } |
| 287 | if (name.equals("Name")) { |
| 288 | Intent intent = new Intent(BluetoothIntent.REMOTE_NAME_UPDATED_ACTION); |
| 289 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
| 290 | intent.putExtra(BluetoothIntent.NAME, propValues[1]); |
| 291 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 292 | mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]); |
| 293 | } else if (name.equals("Class")) { |
| 294 | Intent intent = new Intent(BluetoothIntent.REMOTE_DEVICE_CLASS_UPDATED_ACTION); |
| 295 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
| 296 | intent.putExtra(BluetoothIntent.CLASS, propValues[1]); |
| 297 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 298 | mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]); |
| 299 | } else if (name.equals("Connected")) { |
| 300 | Intent intent = null; |
| 301 | if (propValues[1].equals("true")) { |
| 302 | intent = new Intent(BluetoothIntent.REMOTE_DEVICE_CONNECTED_ACTION); |
| 303 | } else { |
| 304 | intent = new Intent(BluetoothIntent.REMOTE_DEVICE_DISCONNECTED_ACTION); |
| 305 | } |
| 306 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
| 307 | mContext.sendBroadcast(intent, BLUETOOTH_PERM); |
| 308 | mBluetoothService.setRemoteDeviceProperty(address, name, propValues[1]); |
| 309 | } else if (name.equals("UUIDs")) { |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 310 | String uuid = null; |
| 311 | int len = Integer.valueOf(propValues[1]); |
| 312 | if (len > 0) { |
| 313 | uuid = ""; |
| 314 | for (int i = 2; i < propValues.length; i++) { |
| 315 | uuid = uuid + propValues[i] + ","; |
| 316 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 317 | } |
Jaikumar Ganesh | 8bc8ce4 | 2009-06-24 16:42:51 -0700 | [diff] [blame] | 318 | mBluetoothService.setRemoteDeviceProperty(address, name, uuid); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 319 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 322 | private String checkPairingRequestAndGetAddress(String objectPath, int nativeData) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 323 | String address = mBluetoothService.getAddressFromObjectPath(objectPath); |
| 324 | if (address == null) { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 325 | Log.e(TAG, "Unable to get device address in checkPairingRequestAndGetAddress, " + |
| 326 | "returning null"); |
| 327 | return null; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 328 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | address = address.toUpperCase(); |
| 330 | mPasskeyAgentRequestData.put(address, new Integer(nativeData)); |
| 331 | |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 332 | if (mBluetoothService.getBluetoothState() == BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF) { |
| 333 | // shutdown path |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 334 | mBluetoothService.cancelPairingUserInput(address); |
| 335 | return null; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 336 | } |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 337 | return address; |
| 338 | } |
| 339 | |
| 340 | private void onRequestConfirmation(String objectPath, int passkey, int nativeData) { |
| 341 | String address = checkPairingRequestAndGetAddress(objectPath, nativeData); |
| 342 | if (address == null) return; |
| 343 | |
| 344 | Intent intent = new Intent(BluetoothIntent.PAIRING_REQUEST_ACTION); |
| 345 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
| 346 | intent.putExtra(BluetoothIntent.PASSKEY, passkey); |
| 347 | intent.putExtra(BluetoothIntent.PAIRING_VARIANT, |
| 348 | BluetoothDevice.PAIRING_VARIANT_CONFIRMATION); |
| 349 | mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | private void onRequestPasskey(String objectPath, int nativeData) { |
| 354 | String address = checkPairingRequestAndGetAddress(objectPath, nativeData); |
| 355 | if (address == null) return; |
| 356 | |
| 357 | Intent intent = new Intent(BluetoothIntent.PAIRING_REQUEST_ACTION); |
| 358 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
| 359 | intent.putExtra(BluetoothIntent.PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_PASSKEY); |
| 360 | mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | private void onRequestPinCode(String objectPath, int nativeData) { |
| 365 | String address = checkPairingRequestAndGetAddress(objectPath, nativeData); |
| 366 | if (address == null) return; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | if (mBluetoothService.getBondState().getBondState(address) == |
| 369 | BluetoothDevice.BOND_BONDING) { |
| 370 | // we initiated the bonding |
| 371 | int btClass = mBluetoothService.getRemoteClass(address); |
| 372 | |
| 373 | // try 0000 once if the device looks dumb |
| 374 | switch (BluetoothClass.Device.getDevice(btClass)) { |
| 375 | case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET: |
| 376 | case BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE: |
| 377 | case BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES: |
| 378 | case BluetoothClass.Device.AUDIO_VIDEO_PORTABLE_AUDIO: |
| 379 | case BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO: |
| 380 | case BluetoothClass.Device.AUDIO_VIDEO_HIFI_AUDIO: |
| 381 | if (!mBluetoothService.getBondState().hasAutoPairingFailed(address) && |
| 382 | !mBluetoothService.getBondState().isAutoPairingBlacklisted(address)) { |
| 383 | mBluetoothService.getBondState().attempt(address); |
| 384 | mBluetoothService.setPin(address, BluetoothDevice.convertPinToBytes("0000")); |
| 385 | return; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | Intent intent = new Intent(BluetoothIntent.PAIRING_REQUEST_ACTION); |
| 390 | intent.putExtra(BluetoothIntent.ADDRESS, address); |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 391 | intent.putExtra(BluetoothIntent.PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_PIN); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 393 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 396 | private boolean onAgentAuthorize(String objectPath, String deviceUuid) { |
| 397 | String address = mBluetoothService.getAddressFromObjectPath(objectPath); |
| 398 | if (address == null) { |
| 399 | Log.e(TAG, "Unable to get device address in onAuthAgentAuthorize"); |
| 400 | return false; |
| 401 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | boolean authorized = false; |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 404 | UUID uuid = UUID.fromString(deviceUuid); |
Jaikumar Ganesh | ade4052 | 2009-07-30 13:32:25 -0700 | [diff] [blame^] | 405 | if (mBluetoothService.isEnabled() && |
| 406 | (BluetoothUuid.isAudioSink(uuid) || BluetoothUuid.isAvrcpController(uuid))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | BluetoothA2dp a2dp = new BluetoothA2dp(mContext); |
| 408 | authorized = a2dp.getSinkPriority(address) > BluetoothA2dp.PRIORITY_OFF; |
| 409 | if (authorized) { |
Jaikumar Ganesh | ade4052 | 2009-07-30 13:32:25 -0700 | [diff] [blame^] | 410 | Log.i(TAG, "Allowing incoming A2DP / AVRCP connection from " + address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } else { |
Jaikumar Ganesh | ade4052 | 2009-07-30 13:32:25 -0700 | [diff] [blame^] | 412 | Log.i(TAG, "Rejecting incoming A2DP / AVRCP connection from " + address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | } |
| 414 | } else { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 415 | Log.i(TAG, "Rejecting incoming " + deviceUuid + " connection from " + address); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | } |
| 417 | return authorized; |
| 418 | } |
| 419 | |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 420 | private void onAgentCancel() { |
Jaikumar Ganesh | b0eca41 | 2009-07-16 18:26:28 -0700 | [diff] [blame] | 421 | Intent intent = new Intent(BluetoothIntent.PAIRING_CANCEL_ACTION); |
| 422 | mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); |
| 423 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 426 | private void onRestartRequired() { |
| 427 | if (mBluetoothService.isEnabled()) { |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 428 | Log.e(TAG, "*** A serious error occured (did bluetoothd crash?) - " + |
| 429 | "restarting Bluetooth ***"); |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 430 | mHandler.sendEmptyMessage(EVENT_RESTART_BLUETOOTH); |
| 431 | } |
| 432 | } |
| 433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | private static void log(String msg) { |
| 435 | Log.d(TAG, msg); |
| 436 | } |
Jaikumar Ganesh | d5ac1ae | 2009-05-05 22:26:12 -0700 | [diff] [blame] | 437 | |
| 438 | private native void initializeNativeDataNative(); |
| 439 | private native void startEventLoopNative(); |
| 440 | private native void stopEventLoopNative(); |
| 441 | private native boolean isEventLoopRunningNative(); |
| 442 | private native void cleanupNativeDataNative(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | } |