views:

222

answers:

1

Hi-

I am using the BluetoothSerialPortInfo class to get Bluetooth devices paired with my blackberry. When I tried to print the value of device address for each device, I don't see or get actual Bluetooth address. I am using the following code.

String btAddress = mPortInfo[count].getDeviceAddress().toString();

I actually get [@4d4cd14c from the above code. But the actual Bluetooth address of my device is : 00:21:3c:2d:1F:5c.

If I use javax.bluetooth, I get the actual Bluetooth address. But I want to use BluetoothSerialPortInfo to establish the serial connection to the device. So I want to correctly identify my device based on the Bluetooth address without using the friendly name of the device.

How do I convert the raw address which get from getDeviceAddress() method to the actual Bluetooth address???

Thanks,

+1  A: 

getDeviceAddress() returns a byte array so you'll need to convert each byte into it's hex representation. If you're on 5.0 you can use ByteArrayUtilities.byteArrayToHex() but if you're on a lower OS version you'll need to write your own conversion code. Something like this should work (found on another SO post):

public static String toHexString(byte bytes[]) {
    if (bytes == null) {
        return null;
    }

    StringBuffer sb = new StringBuffer();
    for (int iter = 0; iter < bytes.length; iter++) {
        byte high = (byte) ( (bytes[iter] & 0xf0) >> 4);
        byte low =  (byte)   (bytes[iter] & 0x0f);
        sb.append(nibble2char(high));
        sb.append(nibble2char(low));
    }

    return sb.toString();
}

private static char nibble2char(byte b) {
    byte nibble = (byte) (b & 0x0f);
    if (nibble < 10) {
        return (char) ('0' + nibble);
    }
    return (char) ('a' + nibble - 10);
}
Marc Novakowski