Android Bluetooth is pairing a device even if the connection fails
I'm writing a Bluetooth app targeting API 7 (my device is API 8 - 2.2). When I attempt to connect the Bluetooth socket I get the pop-up prompting the user to pair with the device, but sometimes the connection times out or fails. In these cases, the pop-up disappears without clicking anything but the device shows as "Paired but n开发者_StackOverflowot connected" in the Bluetooth settings.
If I connect to the Bluetooth device directly from the Bluetooth settings then this doesn't happen; the device only shows "Paired but not connected" if the user actually clicks "Pair".
Here's the code that's creating the connection, it seems simple enough:
try {
BluetoothDevice btDevice = _btAdapter.getRemoteDevice(address);
_btAdapter.cancelDiscovery();
sock = btDevice.createRfcommSocketToServiceRecord(SafConstants.BT_UUID);
sock.connect();
} catch (IOException e) {
logger.error("socket connection failed", e);
if (sock != null) {
try {sock.close();}
catch (IOException e1) {}
}
}
Is this an Android issue or am I supposed to be doing something else? I searched the API to see if I could remove the device from the set of bonded devices (if the connection fails) but it seems this isn't possible.
EDIT: One thing I notice is that when I try connecting through my app, I get prompted on my laptop that it's trying to access the Headset service before pairing. After a few seconds I'll eventually get the prompt on my laptop that a device is trying to pair.
If I connect through bluetooth settings then I get the prompt to pair the device first and then after it's successful I get the prompt that a device is attempting to access the Headset service. I'm not sure if that's significant or not...
The UUID you are using is for SPP (Serial Port Profile), and being a headset, would require the Bluetooth profile - A2DP (Advanced Audio Distribution ). Currently, the Android implementation of the Bluetooth stack, does not have support for Bluetooth profiles and is only a bare bones implementation.
For Bluetooth profile support to be implemented on Android, there is a project called “Sybase-iAnywhere-Blue-SDK-for-Android”, which replaces Android's version, and provides all interfaces into the underlying Bluetooth profiles and protocols. Using this, printing over bluetooth using your Android phone will be possible using the BPP profile provided by this SDK.
See links below for more details:
link 1: http://www.sybase.com/detail?id=1064424
Link 2: http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks
精彩评论