0

I'm working with device Texas, for management of the LED, in my custom Android app. The device has enabled the pairing with a default passkey 000000. In my code for app, I have this part of code for reading the paired of device.

    private void getpaireddevices(){
    Set<BluetoothDevice> devicesArray = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    if(devicesArray.size() > 0) {
        for(BluetoothDevice device : devicesArray) {
            device.getName();
            device.getAddress();
        }
    }
}

In this moment, when I enable the BLE the app found the device, it connect but not works. For this to work I should exit and reconnect with my device. Why?

Daniel Selvan
  • 959
  • 10
  • 23
MarioCas
  • 29
  • 5

1 Answers1

1

This is possible if the device is already bonded. Call removeBond() method to clear previous bonding state.

device.removeBond();

For check bonding state of BluetoothDevice, use getBondState().

Ble gatt connection success rate is different per device by device. You may need disconnect ble by hidden method, if your connection fails continuously.

Please read this: BLE Device Bonding Remove Automatically in Android

The method unpairDevice() will unpair bluetooth connection.

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

for (BluetoothDevice bt : pairedDevices) {
        if (bt.getName().contains("String you know has to be in device name")) {
            unpairDevice(bt);
        }
}

// Function to unpair from passed in device
private void unpairDevice(BluetoothDevice device) {
    try {

        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
    } catch (Exception e) { Log.e(TAG, e.getMessage()); }
}
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
  • Hi Stanley, If I use this that you write...I should paired every time that I want to connect with device. Right? – MarioCas Mar 04 '19 at 09:15
  • @MarioCas Well, yes. Oh are you using ble gatt connection? And you want that always connected? – Stanley Ko Mar 04 '19 at 13:13
  • It seems that you need to wait until your device ready to communicate. I suggest you give some delay (maybe 3sec?) before run your code. – Stanley Ko Mar 04 '19 at 13:16
  • Ok, perfect. Sorry @Stanley Kou, I found another problem. If I test the my app with my Samsung S8 it works very well. If I test the app with another smartphone Samsung A8, I have the problem. I click connect, connect and it does not work. I go out and re-connect and asks me for another pairing. What could cause this? – MarioCas Mar 04 '19 at 13:42
  • @MarioCas Welcome to the Hardware world!!! Every single hardware has it's own characteristic. It may not a problem of all Samsung A8. It must be a problem of THE device. Can you change your A8 device to another A8? – Stanley Ko Mar 05 '19 at 00:28
  • No, I can't. I haven't the another A8. – MarioCas Mar 05 '19 at 10:42
  • I edited answer. Try ble disconnection. Or change initial delay. – Stanley Ko Mar 05 '19 at 23:56
  • How make this ? – MarioCas Mar 06 '19 at 09:36