javaandroidbluetoothandroid-bluetoothpairing

Catching specific MAC Address from Paired Devices


everyone.

new here.

I'm experiencing an issue comparing MAC address from paired devices. I want my app to do something if it finds that a certain MAC address is already paired to it.

Here's part of my java class:

Boolean pair_status = false;

Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice device : pairedDevices) {
        if (device.getAddress() == String.valueOf(R.string.BT_MWAddress)) {
            pair_status = true;
        }
    }
}

Here's my string initialization:

<string name="BT_MWAddress">28:A3:91:E6:A1:50</string>

I can see that the device is paired with my phone.

I've tried changing my condition statement to the ones below but I'm still getting the same result.

Another check I did is changing my code like below for troubleshooting. and pair_status always updates to -1. Posted the pair_status in a TextView.

int pair_status = 0;

Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
    for (BluetoothDevice device : pairedDevices) {
        if (device.getAddress() == String.valueOf(R.string.BT_MWAddress)) {
            pair_status = 1;
        } else {
            pair_status = -1;
        }
    }
}

Solution

  • Strings are immutable in Java, so == will compare memory locations. You could try using the .equals method, which would compare the actual characters in the string instead of the memory location.

    If that doesn't work, can you put print outs for the 2 addresses.