Delete all paired bluetooth devices on Android [duplicate] - android

This question already has answers here:
How to unpair or delete paired bluetooth device programmatically on android?
(5 answers)
Closed 6 months ago.
I would like to delete paired bluetooth low energy devices with names that start with "ABC" on an Android phone programatically.
I am using Android studio.

If you are specific about BLE(bluetooth low energy),
To get all bonded devices you can write a method as.
public List<BluetoothDevice> getConnectedDevices() {
BluetoothManager btManager = (BluetoothManager)getSystemService(BLUETOOTH_SERVICE);
return btManager.getConnectedDevices(BluetoothProfile.GATT);
}
This return the list of BLE devices connected in GATT profile.
Fetch the name confirm if this the device you want to disconnet as:
List<BluetoothDevice> btdevices = getConnectedDevices();
for(int i=0;i<btdevices.size();i++)
{
//match your device here
Log.d("saurav"," BLE Name:"+btdevices.get(i).getName());
}
To disconnect you can simply call disconnect method. You need disconnect with gatt instance(Same gatt instance you used to connect the BLE device).
public void disconnect() {
if (gatt == null) {
return;
}
gatt.disconnect();
}
This will disconnect your BLE device. I have teste tshi personally and working for me.

Related

Android paired Bluetooth disappears when device reboots

I want the connected device to get BluetoothDevice device information without scanning Bluetooth, so I store all Bluetooth-connected devices.
However, while other devices behave as expected, one type of Bluetooth devices will lose their paired information when the phone is rebooted.
Do anyone know why this may happen or have an alternative way to obtain Bluetooth Device information without scanning?
You can use BluetoothAdapter.
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
device.connectGatt(mContext, false, mGattCallback);
// TODO your code
If the bluetooth device is not around, in BluetoothGattCallback's onConnectionStateChange will report BluetoothGatt.STATE_DISCONNECTED.
BluetoothDevice's creator has packages scope. But the source code of BluetoothAdapter's getRemoteDevice is:
public BluetoothDevice getRemoteDevice(String address) {
return new BluetoothDevice(address);
}

Android Bluetooth BLE onDescriptorWrite GATT_INSUFFICIENT_AUTHENTICATION

I have a service that connects to a Bluetooth glucose device directly via the mac address.
if (mBluetoothGatt != null) {
if (mBluetoothGatt.connect()) {
return true;
}
else {
return false;
}
}
 
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
The pairing routine and downloading of data from the device work perfect the first time after pairing, but if I try to re-connect to the device and register for notifications I receive a GATT_INSUFFICIENT_AUTHENTICATION error in my BluetoothGatt.onDescriptorWrite method.
#Override
public void onDescriptorWrite (BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
...
if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
...
The system then prompts the user for a pin code and asks them to re-authenticate with the device, even though the BONDING STATE shows as BONDED.
I've read quite a few StackOverflow posts about BLE and some of them are conflicting or do not address the question of connection handling directly.
If we are trying to connect to a previously paired device, do we use
auto connect or not?
Do we need to re-enable notifications for a
device each time we connect to it? Or only the first time we
connect?
The device I'm using is a Moto G with KitKat 4.4.
Upgrading to Lollipop fixed the problem. The authentication now works perfectly and I do not get prompted every time I create a new connection.

Why the App doesn't reconnect to the BLE device when set autoConnect to true in Android?

I am develop in Android and BLE. I want the App automatic reconnect to the BLE device after the BLE device disconnect but come back in the range and advertising.
I use the following code to connect to the BLE device:
public void connect(final String address) {
// TODO Auto-generated method stub
Log.w(TAG, "BluetoothLeService Connect function.");
if(mBluetoothAdapter == null || address == null){
Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
//return false;
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
}
I have set the AutoConnect to the true , but it didn't reconnect when the BLE device has disconnect and come back in the range.
Why the App doesn't reconnect to the BLE device when set autoConnect to true in Android?
Did I missing something ?
Thanks in advance.
The auto connect parameter determines whether to actively connect to the remote device (or) rather passively scan and finalize the connection when the remote device is in range.
But this does not mean that a peripheral that's been disconnected for days then reappears will be reconnected.
Generally, the first ever connection to a device should be direct (autoConnect set to false) and subsequent connections to known devices should be invoked with the autoConnect parameter set to true.
Also please note, the auto connect will only work when the device is still broadcasting. If not, then it will not work.
I would prefer that you re-connect manually when the device is disconnected. If in case you do end up following this, you would need a marker to determine whether the device was actually disconnected without the user consent.
If true then unbind/unregister your service/broadcast receiver and connect again using the device address which you must have saved previously.
As per my experimentation with the BLE devices it has different behavior in different builds like Kitkat and Lollipop. Even I have observed, using ScanCallback is not so reliable introduced in API level 24.
For auto connect to work the BLE device must be active.
For me i had to support kitkat and lollipop so while connecting gatt i called as:
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
gatt = device.connectGatt(this, true, executor);
} else {
gatt = device.connectGatt(this, false, executor);
}
Now auto connect is working for me in both Lollipop and Kitkat.

Android Bluetooth Scan for classic AND BTLE Devices

The android documentation states:
Note: You can only scan for Bluetooth LE devices or scan for Classic Bluetooth devices, as described in Bluetooth. You cannot scan for both Bluetooth LE and classic devices at the same time.
However I am noticing that calling mBtAdapter.startDiscovery(); is returning both classic and btle devices. Does anybody know what is correct here?
From my understanding what the documentation means is that you cannot have a startLeScan() and a startDiscovery() running at the same time. The reason might be that there is only one BluetoothAdapter object (the object that represents the local Bluetooth hardware) therefor it cannot do two different operations that use the BluetoothAdapter at the same time.(If someone knows anything different as to how it works in the background, let us know)
startLeScan() -> scans only for BLE devices
startDiscovery() -> discovers all Bluetooth devices, also it only scans for 12 seconds and this cannot be changed (have a read through the method description)
Note: After doing a startDiscovery() inquiry scan when a BT device is found you can get the device type to identify what each device is, For example:
int deviceType = device.getType();
if (deviceType == BluetoothDevice.DEVICE_TYPE_CLASSIC) {
} else if (deviceType == BluetoothDevice.DEVICE_TYPE_LE) {
} else if (deviceType == BluetoothDevice.DEVICE_TYPE_DUAL) {
} else if (deviceType == BluetoothDevice.DEVICE_TYPE_UNKNOWN) {
}
device: BluetoothDevice
when (device.type) {
1 -> edt.text = "DEVICE_TYPE_CLASSIC"
2 -> edt.text = "DEVICE_TYPE_LE"
3 -> edt.text = "DEVICE_TYPE_DUAL"
else -> edt.text = "DEVICE_TYPE_UNKNOWN"
}

Android 2.2 progammically tell if my Bluetooth Headset is connected on application start

I am programming for android 2.2 API Level 8 and I am trying to find out if there is currently a bluetooth headset connected when my application starts initially. I can not seem to find a solution to this issue.
Once my application is up and running I can listen for BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED as denoted here which works great mind you. But I can't figure out how to tell before the BroadcastReceiver is on if there is currently a bluetooth headset connected.
I have also figured out how to see if the device has any paired devices via the following.
BluetoothAdapter mBluetoothAdapter = null;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter != null)
{
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() != 0)
{
// the device has paired devices
for (BluetoothDevice device : pairedDevices)
{
Log.i(TAG, "device name: " + device.getName());
Log.i(TAG, "device address: " + device.getAddress());
}
}
else
{
// no paired devices
Log.i(TAG, "no paired devices");
}
}
I am hoping that someone out there already has a solution to this issue for API Level 8. I do realize that in API 11 there are some new classes such as
BluetoothProfile and
BluetoothHeadset that could probably do this in 1 line of code, but again I am trying to do this in API Level 8
I am willing to try anything at this point.
Thanks in advance
-H
Following code works for me. First connect your BT device with android manually.
AudioManager am = getSystemService(Context.AUDIO_SERVICE)
if( am.isBluetoothA2dpOn() )
{
//bt device is connected with phone
}
else{
// no device is connected with phone
}
Unfortunately there is really no way to do this in Java on an unrooted device. If you have a device with root access and you use the NDK you can drop into the BlueZ programming layer and you can do it there, but with the publicly available Java API there is no way to do this.

Categories

Resources