Home » Android » Getting the renamed name of an Android BluetoothDevice

Getting the renamed name of an Android BluetoothDevice

Posted by: admin June 15, 2020 Leave a comment

Questions:

My android phone allows me rename devices that I have paired with, by going to the [Settings > Wireless & Networkds > Bluetooth] Activity page and clicking the settings button to the right of a paired bluetooth device. However, when I query for a list of Bonded devices with the BluetoothAdapter.getBondedDevices() function, the name that shows up in the results is the default name for the device.

How can I access the renamed name for a Bluetooth device?

How to&Answers:

You should use alias name.

For setting rename device:

try {
    Method method = device.getClass().getMethod("setAlias", String.class);
    if(method != null) {
        method.invoke(device, "new_device_name");
    }
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}

For getting device name:

String deviceAlias = device.getName();
try {
    Method method = device.getClass().getMethod("getAliasName");
    if(method != null) {
        deviceAlias = (String)method.invoke(device);
    }
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
}