RemoteDevices.java revision 74a598e0a8d50343227e3f96bbe56c6be1c240f9
1/* 2 * Copyright (C) 2012 Google Inc. 3 */ 4 5package com.android.bluetooth.btservice; 6 7import android.bluetooth.BluetoothAdapter; 8import android.bluetooth.BluetoothClass; 9import android.bluetooth.BluetoothDevice; 10import android.content.Context; 11import android.content.Intent; 12import android.os.Handler; 13import android.os.Message; 14import android.os.ParcelUuid; 15import android.util.Log; 16 17import com.android.bluetooth.Utils; 18import com.android.bluetooth.btservice.RemoteDevices.DeviceProperties; 19 20import java.util.ArrayList; 21import java.util.HashMap; 22import java.util.LinkedList; 23 24 25final class RemoteDevices { 26 private static final boolean DBG = true; 27 private static final String TAG = "BluetoothRemoteDevices"; 28 29 30 private static BluetoothAdapter mAdapter; 31 private static AdapterService mAdapterService; 32 private static ArrayList<BluetoothDevice> mSdpTracker; 33 34 private Object mObject = new Object(); 35 36 private static final int UUID_INTENT_DELAY = 6000; 37 private static final int MESSAGE_UUID_INTENT = 1; 38 39 private HashMap<BluetoothDevice, DeviceProperties> mDevices; 40 41 RemoteDevices(AdapterService service) { 42 mAdapter = BluetoothAdapter.getDefaultAdapter(); 43 mAdapterService = service; 44 mSdpTracker = new ArrayList<BluetoothDevice>(); 45 mDevices = new HashMap<BluetoothDevice, DeviceProperties>(); 46 } 47 48 49 void cleanup() { 50 mSdpTracker.clear(); 51 mSdpTracker = null; 52 mDevices.clear(); 53 mDevices = null; 54 mAdapterService = null; 55 mAdapter= null; 56 } 57 58 public Object Clone() throws CloneNotSupportedException { 59 throw new CloneNotSupportedException(); 60 } 61 62 DeviceProperties getDeviceProperties(BluetoothDevice device) { 63 synchronized (mDevices) { 64 return mDevices.get(device); 65 } 66 } 67 68 BluetoothDevice getDevice(byte[] address) { 69 for (BluetoothDevice dev : mDevices.keySet()) { 70 if (dev.getAddress().equals(Utils.getAddressStringFromByte(address))) { 71 return dev; 72 } 73 } 74 return null; 75 } 76 77 DeviceProperties addDeviceProperties(byte[] address) { 78 synchronized (mDevices) { 79 DeviceProperties prop = new DeviceProperties(); 80 BluetoothDevice device = 81 mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address)); 82 prop.mAddress = address; 83 mDevices.put(device, prop); 84 return prop; 85 } 86 } 87 88 class DeviceProperties { 89 private String mName; 90 private byte[] mAddress; 91 private int mBluetoothClass; 92 private short mRssi; 93 private ParcelUuid[] mUuids; 94 private int mDeviceType; 95 private String mAlias; 96 private int mBondState; 97 98 DeviceProperties() { 99 mBondState = BluetoothDevice.BOND_NONE; 100 } 101 102 /** 103 * @return the mName 104 */ 105 String getName() { 106 synchronized (mObject) { 107 return mName; 108 } 109 } 110 111 /** 112 * @return the mClass 113 */ 114 int getBluetoothClass() { 115 synchronized (mObject) { 116 return mBluetoothClass; 117 } 118 } 119 120 /** 121 * @return the mUuids 122 */ 123 ParcelUuid[] getUuids() { 124 synchronized (mObject) { 125 return mUuids; 126 } 127 } 128 129 /** 130 * @return the mAddress 131 */ 132 byte[] getAddress() { 133 synchronized (mObject) { 134 return mAddress; 135 } 136 } 137 138 /** 139 * @return mRssi 140 */ 141 short getRssi() { 142 synchronized (mObject) { 143 return mRssi; 144 } 145 } 146 147 /** 148 * 149 * @return mDeviceType 150 */ 151 int getDeviceType() { 152 synchronized (mObject) { 153 return mDeviceType; 154 } 155 } 156 157 /** 158 * @return the mAlias 159 */ 160 String getAlias() { 161 synchronized (mObject) { 162 return mAlias; 163 } 164 } 165 166 /** 167 * @param mAlias the mAlias to set 168 */ 169 void setAlias(String mAlias) { 170 synchronized (mObject) { 171 mAdapterService.setDevicePropertyNative(mAddress, 172 AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME, mAlias.getBytes()); 173 } 174 } 175 176 /** 177 * @param mBondState the mBondState to set 178 */ 179 void setBondState(int mBondState) { 180 synchronized (mObject) { 181 this.mBondState = mBondState; 182 if (mBondState == BluetoothDevice.BOND_NONE) 183 { 184 /* Clearing the Uuids local copy when the device is unpaired. If not cleared, 185 cachedBluetoothDevice issued a connect using the local cached copy of uuids, 186 without waiting for the ACTION_UUID intent. 187 This was resulting in multiple calls to connect().*/ 188 mUuids = null; 189 } 190 } 191 } 192 193 /** 194 * @return the mBondState 195 */ 196 int getBondState() { 197 synchronized (mObject) { 198 return mBondState; 199 } 200 } 201 } 202 203 204 private void sendUuidIntent(BluetoothDevice device) { 205 DeviceProperties prop = getDeviceProperties(device); 206 Intent intent = new Intent(BluetoothDevice.ACTION_UUID); 207 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 208 intent.putExtra(BluetoothDevice.EXTRA_UUID, prop == null? null: prop.mUuids); 209 mAdapterService.sendBroadcast(intent, AdapterService.BLUETOOTH_ADMIN_PERM); 210 211 //Remove the outstanding UUID request 212 mSdpTracker.remove(device); 213 } 214 215 private void sendDisplayPinIntent(byte[] address, int pin) { 216 Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); 217 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address)); 218 intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pin); 219 intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, 220 BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN); 221 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM); 222 } 223 224 void devicePropertyChangedCallback(byte[] address, int[] types, byte[][] values) { 225 Intent intent; 226 byte[] val; 227 int type; 228 BluetoothDevice bdDevice = getDevice(address); 229 DeviceProperties device; 230 if (bdDevice == null) { 231 device = addDeviceProperties(address); 232 bdDevice = getDevice(address); 233 } else { 234 device = getDeviceProperties(bdDevice); 235 } 236 237 for (int j = 0; j < types.length; j++) { 238 type = types[j]; 239 val = values[j]; 240 if(val.length <= 0) 241 errorLog("devicePropertyChangedCallback: bdDevice: " + bdDevice + ", value is empty for type: " + type); 242 else { 243 synchronized(mObject) { 244 switch (type) { 245 case AbstractionLayer.BT_PROPERTY_BDNAME: 246 device.mName = new String(val); 247 intent = new Intent(BluetoothDevice.ACTION_NAME_CHANGED); 248 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice); 249 intent.putExtra(BluetoothDevice.EXTRA_NAME, device.mName); 250 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 251 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM); 252 debugLog("Remote Device name is: " + device.mName); 253 break; 254 case AbstractionLayer.BT_PROPERTY_REMOTE_FRIENDLY_NAME: 255 // TODO(BT) is null device.mAlias a valid senario? 256 if (device.mAlias != null) { 257 System.arraycopy(val, 0, device.mAlias, 0, val.length); 258 } 259 break; 260 case AbstractionLayer.BT_PROPERTY_BDADDR: 261 device.mAddress = val; 262 debugLog("Remote Address is:" + Utils.getAddressStringFromByte(val)); 263 break; 264 case AbstractionLayer.BT_PROPERTY_CLASS_OF_DEVICE: 265 device.mBluetoothClass = Utils.byteArrayToInt(val); 266 intent = new Intent(BluetoothDevice.ACTION_CLASS_CHANGED); 267 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bdDevice); 268 intent.putExtra(BluetoothDevice.EXTRA_CLASS, 269 new BluetoothClass(device.mBluetoothClass)); 270 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 271 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM); 272 debugLog("Remote class is:" + device.mBluetoothClass); 273 break; 274 case AbstractionLayer.BT_PROPERTY_UUIDS: 275 int numUuids = val.length/AbstractionLayer.BT_UUID_SIZE; 276 device.mUuids = Utils.byteArrayToUuid(val); 277 sendUuidIntent(bdDevice); 278 break; 279 case AbstractionLayer.BT_PROPERTY_TYPE_OF_DEVICE: 280 device.mDeviceType = Utils.byteArrayToInt(val); 281 break; 282 case AbstractionLayer.BT_PROPERTY_REMOTE_RSSI: 283 device.mRssi = Utils.byteArrayToShort(val); 284 break; 285 } 286 } 287 } 288 } 289 } 290 291 void deviceFoundCallback(byte[] address) { 292 // The device properties are already registered - we can send the intent 293 // now 294 BluetoothDevice device = getDevice(address); 295 debugLog("deviceFoundCallback: Remote Address is:" + device); 296 DeviceProperties deviceProp = getDeviceProperties(device); 297 if (deviceProp == null) { 298 errorLog("Device Properties is null for Device:" + device); 299 return; 300 } 301 302 Intent intent = new Intent(BluetoothDevice.ACTION_FOUND); 303 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 304 intent.putExtra(BluetoothDevice.EXTRA_CLASS, 305 new BluetoothClass(Integer.valueOf(deviceProp.mBluetoothClass))); 306 intent.putExtra(BluetoothDevice.EXTRA_RSSI, deviceProp.mRssi); 307 intent.putExtra(BluetoothDevice.EXTRA_NAME, deviceProp.mName); 308 309 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM); 310 } 311 312 void pinRequestCallback(byte[] address, byte[] name, int cod) { 313 //TODO(BT): Get wakelock and update name and cod 314 BluetoothDevice bdDevice = getDevice(address); 315 if (bdDevice == null) { 316 addDeviceProperties(address); 317 } 318 BluetoothClass btClass = bdDevice.getBluetoothClass(); 319 int btDeviceClass = btClass.getDeviceClass(); 320 if (btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD || 321 btDeviceClass == BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING) { 322 // Its a keyboard. Follow the HID spec recommendation of creating the 323 // passkey and displaying it to the user. If the keyboard doesn't follow 324 // the spec recommendation, check if the keyboard has a fixed PIN zero 325 // and pair. 326 //TODO: Add sFixedPinZerosAutoPairKeyboard() and maintain list of devices that have fixed pin 327 /*if (mAdapterService.isFixedPinZerosAutoPairKeyboard(address)) { 328 mAdapterService.setPin(address, BluetoothDevice.convertPinToBytes("0000")); 329 return; 330 }*/ 331 // Generate a variable PIN. This is not truly random but good enough. 332 int pin = (int) Math.floor(Math.random() * 1000000); 333 sendDisplayPinIntent(address, pin); 334 return; 335 } 336 infoLog("pinRequestCallback: " + address + " name:" + name + " cod:" + 337 cod); 338 Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); 339 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, getDevice(address)); 340 intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, 341 BluetoothDevice.PAIRING_VARIANT_PIN); 342 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM); 343 return; 344 } 345 346 void sspRequestCallback(byte[] address, byte[] name, int cod, int pairingVariant, 347 int passkey) { 348 //TODO(BT): Get wakelock and update name and cod 349 BluetoothDevice bdDevice = getDevice(address); 350 if (bdDevice == null) { 351 addDeviceProperties(address); 352 } 353 354 infoLog("sspRequestCallback: " + address + " name: " + name + " cod: " + 355 cod + " pairingVariant " + pairingVariant + " passkey: " + passkey); 356 int variant; 357 boolean displayPasskey = false; 358 if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_CONFIRMATION) { 359 variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION; 360 displayPasskey = true; 361 } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_CONSENT) { 362 variant = BluetoothDevice.PAIRING_VARIANT_CONSENT; 363 } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_ENTRY) { 364 variant = BluetoothDevice.PAIRING_VARIANT_PASSKEY; 365 } else if (pairingVariant == AbstractionLayer.BT_SSP_VARIANT_PASSKEY_NOTIFICATION) { 366 variant = BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY; 367 displayPasskey = true; 368 } else { 369 errorLog("SSP Pairing variant not present"); 370 return; 371 } 372 BluetoothDevice device = getDevice(address); 373 if (device == null) { 374 warnLog("Device is not known for:" + Utils.getAddressStringFromByte(address)); 375 addDeviceProperties(address); 376 device = getDevice(address); 377 } 378 Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); 379 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 380 if (displayPasskey) { 381 intent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, passkey); 382 } 383 intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, variant); 384 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_ADMIN_PERM); 385 } 386 387 void aclStateChangeCallback(int status, byte[] address, int newState) { 388 BluetoothDevice device = getDevice(address); 389 390 if (device == null) { 391 errorLog("aclStateChangeCallback: Device is NULL"); 392 return; 393 } 394 395 Intent intent = null; 396 if (newState == AbstractionLayer.BT_ACL_STATE_CONNECTED) { 397 intent = new Intent(BluetoothDevice.ACTION_ACL_CONNECTED); 398 debugLog("aclStateChangeCallback: State:Connected to Device:" + device); 399 } else { 400 intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED); 401 debugLog("aclStateChangeCallback: State:DisConnected to Device:" + device); 402 } 403 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); 404 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 405 mAdapterService.sendBroadcast(intent, mAdapterService.BLUETOOTH_PERM); 406 } 407 408 void fetchUuids(BluetoothDevice device) { 409 if (mSdpTracker.contains(device)) return; 410 mSdpTracker.add(device); 411 412 Message message = mHandler.obtainMessage(MESSAGE_UUID_INTENT); 413 message.obj = device; 414 mHandler.sendMessageDelayed(message, UUID_INTENT_DELAY); 415 416 //mAdapterService.getDevicePropertyNative(Utils.getBytesFromAddress(device.getAddress()), AbstractionLayer.BT_PROPERTY_UUIDS); 417 mAdapterService.getRemoteServicesNative(Utils.getBytesFromAddress(device.getAddress())); 418 } 419 420 private final Handler mHandler = new Handler() { 421 @Override 422 public void handleMessage(Message msg) { 423 switch (msg.what) { 424 case MESSAGE_UUID_INTENT: 425 BluetoothDevice device = (BluetoothDevice)msg.obj; 426 if (device != null) { 427 sendUuidIntent(device); 428 } 429 break; 430 } 431 } 432 }; 433 434 private void errorLog(String msg) { 435 Log.e(TAG, msg); 436 } 437 438 private void debugLog(String msg) { 439 if (DBG) Log.e(TAG, msg); 440 } 441 442 private void infoLog(String msg) { 443 if (DBG) Log.i(TAG, msg); 444 } 445 446 private void warnLog(String msg) { 447 Log.w(TAG, msg); 448 } 449 450} 451