Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to find if a paired Android Bluetooth device is in range?

I'm suppose to write an application that will act as the bluetooth client. What I'm trying to do is figure out is what would be the best way to figure out if the specific device that I'm supporting is in range without attempting to do a BluetoothDevice.connect() on it all of the time and failing if it's not in range. Here we're assuming that the device as already been paired.

I'm afraid that it would be bad practice to attempt to connect to a specific device all of the time until it's in range. Seems to me that it would be bad for the battery.

Would anyone know of any methods or concepts that should be used to accomplish what I'm trying to do?

Thanks!

like image 795
Miguel Avatar asked Jul 10 '12 14:07

Miguel


People also ask

How do I find hidden Bluetooth devices on Android?

Using this property of Bluetooth, an app called Wunderfind for iPhone or Android can help you physically locate a lost, hidden, or unknown Bluetooth device using your smartphone. This includes PCs, laptops, tablets, smartphones, Bluetooth headphones, Airpods, smartwatches, smart home devices, and more.

How do I check Bluetooth visibility?

Tap Settings. Tap Bluetooth. Tap the indicator next to "Bluetooth" to turn the function on or off. Tap the indicator next to "Open detection" to turn Bluetooth visibility on or off.

How do I find a paired Bluetooth device?

Make sure Bluetooth is turned on. Touch and hold Bluetooth . In the list of paired devices, tap a paired but unconnected device. When your phone and the Bluetooth device are connected, the device shows as "Connected."

Can you check Bluetooth connection history Android?

How Do I View Bluetooth History On Android? You can access settings on an Android device by going to the Settings menu. Select Developer options. Once enabled, Bluetooth HCI snoop logging will take place.


2 Answers

Ok, so as I understand from your question is you want to get name of device you paired with, and if its in range??

So here is the solution:-

  1. Create a class named DeviceDetails:

class DeviceDetails{ public String name; public String address; public BluetoothDevice remoteDevice; }

  1. You need to connect and pair your devices as explained here, once this is done and connection is made, create an object of DeviceDetails.

  2. DeviceDetails selectedDevice; and if you have a custom adapter to show list of devices pass on the position of from the view to selectedDevice reference. Example :- selectedDevice = adapter.getItem(pos);

  3. Now you have the SelectedDevice object which you have selected to pair, thus you can save its address and name in

    preferences.

    Example:- 
    savePairedDeviceInfo(selectedDevice.name, selectedDevice.address);
        public void savePairedDeviceInfo(String name, String addr)
            {
                if(name != null && !name.equalsIgnoreCase(""))
                    editor.putString(PNAME_OF_DEVICE, name);
    
                if(addr != null && !addr.equalsIgnoreCase(""))
                    editor.putString(MAC_ADDRESS_OF_DEVICE, addr);
    
                editor.commit();
            }
    
  4. Now, next time when ever you want to check if setup of pairing is done or not check for name and address of the device by getting values from preferences. Use a check(if cond.) to return true that older device that was paired was same or some other.

  5. If paired device is in range you would get the same value else it would try to pair with some other device.

Let me know, if you understood from my explanation or not.

like image 100
Ali Ashraf Avatar answered Oct 21 '22 10:10

Ali Ashraf


You can follow below way :

  1. Get paired devices list from bluetooth adapter
  2. Start discovery
  3. When a device is found let's say Device A, you can check if Device A is there in paired devices list or not. If Device A is one of the paired device, means it is a paired available device in range.
like image 24
dpaksoni Avatar answered Oct 21 '22 12:10

dpaksoni