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