I have already discovered the other device and I have already paired it. At least I have it in the list of paired devices on my Android phone.
Now on BluetoothSocket.connect() two problems can occur:
- The remote device is switched off or otherwise unavailable
The remote device forgot about the pairing because it can only pair one other device and has been paired with a different phone
=> Then connect fails after a certain timeout.
Is it possible to check that an already paired device is really available and remembers that it was paired with my phone without connecting to it? This is not about detecting if a device is connected. Paired and visible is not the same as connected.
I’m sorr t report but there’s no way for your device to know another is in range except by attempting to connect to it. And, to know if the remote device has removed the pairing one would have to ask it, e.g. connect and see if it asks for pairing then.
Other ways would be get the user to confirm these before connecting, or maybe use an external channel TCP/IP or WiFi or NFC. If none of those then magical powers would be the only alternative. :-,)
- Remembering the device that was connected is actually kinda simple. Once you have a successful bind to a device you get
BluetoothDeviceobject. you can inquire it’s unique mac address with
getAddress(). Once you have the address save it to a shared preference. this covers the “was it paired” – the next time we use the
BluetoothAdapterand receive the list of bound devices we can search for the saved device address among them.
- Now that we know the exact address of the device, how can we tell if it’s “really available” ? Well, if it’s enough for you to try and discover the device (
startDiscovery ()) to check if it’s available in the “discover” level – then you know the trick (list item 1). If you found out the device is discoverable and you need to test the device for full connectivity you’ll have to open a new socket and see if it goes smooth.
In my experience I treat 3 different possible BT device situations:
- Device not bound
- Device bound – but not connected
- Device bound – and connected
You can get an already paired device list and cross verify them with current discoverable devices.So this will give you already paired devices those are in discoverable mode.
But for undiscoverable devices compulsory you have to connect with them. Try to create socket connection between them if data successfully sent remote device is active.
Also you can use getBondState () of remote device to check bonded state and register ACTION_BOND_STATE_CHANGED receiver for getting callback of bonded state changes.