private synchronized void getSdpRecordsAndConnect(BluetoothDevice device) { if (!device.equals(getCurrentDevice())) { // stale return; } // Check if incoming connection has already connected. if (mRemoteHeadsets.get(device).mState == BluetoothHeadset.STATE_CONNECTED) { return; } ParcelUuid[] uuids = device.getUuids(); int type = BluetoothHandsfree.TYPE_UNKNOWN; if (uuids != null) { if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree)) { log("SDP UUID: TYPE_HANDSFREE"); type = BluetoothHandsfree.TYPE_HANDSFREE; mRemoteHeadsets.get(device).mHeadsetType = type; int channel = device.getServiceChannel(BluetoothUuid.Handsfree); mConnectThread = new RfcommConnectThread(device, channel, type); if (mAdapter.isDiscovering()) { mAdapter.cancelDiscovery(); } mConnectThread.start(); if (getPriority(device) < BluetoothHeadset.PRIORITY_AUTO_CONNECT) { setPriority(device, BluetoothHeadset.PRIORITY_AUTO_CONNECT); } return; } else if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP)) { log("SDP UUID: TYPE_HEADSET"); type = BluetoothHandsfree.TYPE_HEADSET; mRemoteHeadsets.get(device).mHeadsetType = type; int channel = device.getServiceChannel(BluetoothUuid.HSP); mConnectThread = new RfcommConnectThread(device, channel, type); if (mAdapter.isDiscovering()) { mAdapter.cancelDiscovery(); } mConnectThread.start(); if (getPriority(device) < BluetoothHeadset.PRIORITY_AUTO_CONNECT) { setPriority(device, BluetoothHeadset.PRIORITY_AUTO_CONNECT); } return; } } log("SDP UUID: TYPE_UNKNOWN"); mRemoteHeadsets.get(device).mHeadsetType = type; setState( device, BluetoothHeadset.STATE_DISCONNECTED, BluetoothHeadset.RESULT_FAILURE, BluetoothHeadset.LOCAL_DISCONNECT); return; }