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