Getting the renamed name of an Android BluetoothDevice

时光毁灭记忆、已成空白 提交于 2019-12-05 22:52:47

问题


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?


回答1:


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();
}


来源:https://stackoverflow.com/questions/20658142/getting-the-renamed-name-of-an-android-bluetoothdevice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!