Get Bluetooth active paired devices - android

I am getting the Bluetooth paired by method getBondedDevices(). The method returns all the devices which are already paired.
In the list of paired devices there may be devices where Bluetooth may be turned off or out of range(Not reachable).
My requirement is to get the Bluetooth devices which are active(turned on).
Does anyone please can help me with this ?

So you scan your area for Bluetooth-Devices [Take a look at this answer].
When you receive a new Bluetooth-Device, you check if this Device is in your Bonded-Device list. If it is a Bonded-Device, you can sace it in a seperate list, where you save the Devices, which are bonded and active.

Related

Detect which BT device is going out of range

Is there a way to check which device among the paired Bluetooth devices is out of range/disconnected? I am able to see that some device is going out of range but need to identify which device is going out of range.
As #marcman mentioned in the comments, comparing the list of paired devices before and after would help to see which device is going out of range.

How to differentiate the connected bluetooth device in android programmatically?

Whether it is a bluetooth headset or mobile phones?
how to differentiate the bluetooth headset and bluetooth enabled android device in android code.
I am developing a small application,in that I have a feature of blocking the data transfer via bluetooth but it need to allow communication via bluetooth headset.
I referred the examples and codes in that they suggest me to
pair/unpair the bluetooth devices only.
Android: How to pair bluetooth devices programmatically?
or else
get all connected devices.
In Android, how to get the profile of a connected bluetooth device?
Whether can i get any broadcast message in device related to type of
device connection?
Please help me on this to differentiate the connected bluetooth device as headset /android devices(mobile phone).etc.,
Thank you in advance.
Once you scan and find a BluetoothDevice call the method BluetoothDevice.getBluetoothClass(). This will return a BluetoothClass object and the documentation states the following:
Represents a Bluetooth class, which describes general characteristics
and capabilities of a device. For example, a Bluetooth class will
specify the general device type such as a phone, a computer, or
headset, and whether it's capable of services such as audio or
telephony.
So before you allow the user to select the device to connect to, or to filter the list of BluetoothDevices shown, try seeing if the BluetoothClass has the correct device type.
BluetoothClass bluetoothClass = bluetoothDevice.getBluetoothClass();
if(bluetoothClass.getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES) {
// allow user to select this device. I'm not sure exactly which type
// headphones will be but this is a good guess. You can connect to
// your Bluetooth headset to find out for sure.
}
The different device class constants can be found here in case you want to differentiate by device class further.

android app will receive the BluetoothDevice.BOND_NONE broadcast when phone is away from the paired device at a distance

I used "createBond" method to pair my phone and my device with BLE.it succeed.but I took my phone away from the device at a distance, about 10 meters.My app received the broadcast: BluetoothDevice.ACTION_BOND_STATE_CHANGED, and the device status is became BluetoothDevice.BOND_NONE. Is it normal or something wrong?
after my app receive the broadcast I will unpaired the device.because I think the device is unpaired manually by user from setting activity.
10 meters is a normal range for a BLE device. You got too far from it so you couldn't maintain a connection. This is normal. With some devices the distance will be even shorter (perhaps 5m).
BluetoothDevice.BOND_NONE broadcast when phone is away from the paired device at a distance is not normal I guess.
Yes, the disconnection from the paired device when you're out of the bluetooth range is normal, but, as I said earlier in the comment, pairing and connectivity are two different things. The BLE might not be connected but, it can be shown in the paired list of Android once its paired.
And another interesting thing what I want to share in this case is, the bluetooth devices which are already paired once with an Android, are somehow remembered in Android device even if you un-pair them. I had this tested for some of my personal projects.

Can I notify when any bluetooth of paired device is enabled?

Hi I am working with embedded bluetooth device connection with my android application.
My application is installed in Android phone and phone has some paired bluetooth devices, Now I want that if any paired bluetooth device is enabled(starts/turns on) the application should receive notification.
of-course bluetooth will be enabled in phone and bluetooth devices will be within range.
please let me know How this is possible ?
Thanks!
How about using http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_CONNECTION_STATE_CHANGED
To check all changed states and then comparing what the new state is:
STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED etc.
There is nothing automatic that can detect this, you will have to make both your App and the paired device do some work to detect this. TYhere could be 2 options :
Your app could do some periodic device discovery (low frequency - else it will kill the battery) to search for the paired devices in vicinity.
The paired devices on getting enabled / starting on should put itself to be discoverable.
A better way is for the apired device to autimaically initiate a connection once it comes on to the device it was paired with, and typically on most phones by default if Bluetooth is turned on its also scanning for incoming connection , so your app can be ready to accept incoming connections.

How to detect that an already discovered and paired device is available? [duplicate]

This question already has answers here:
How can I programmatically tell if a Bluetooth device is connected?
(7 answers)
Closed 10 years ago.
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 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.
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.

Categories

Resources