Home » Android » android – How to detect that an already discovered and paired device is available?

android – How to detect that an already discovered and paired device is available?

Posted by: admin May 14, 2020 Leave a comment

Questions:

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:

  1. The remote device is switched off or otherwise unavailable
  2. 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.

How to&Answers:

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. :-,)

Answer:

  1. Remembering the device that was connected is actually kinda simple. Once you have a successful bind to a device you get BluetoothDevice object. 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 BluetoothAdapter and receive the list of bound devices we can search for the saved device address among them.
  2. 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:

  1. Device not bound
  2. Device bound – but not connected
  3. Device bound – and connected

Answer:

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.