获取Android BluetoothDevice的重命名名称

Ubi*_*ous 7 android android-bluetooth

我的Android手机允许我重命名我配对的设备,方法是转到[设置>无线和网络>蓝牙]活动页面,然后点击配对蓝牙设备右侧的设置按钮.但是,当我使用BluetoothAdapter.getBondedDevices()函数查询Bonded设备列表时,结果中显示的名称是设备的默认名称.

如何访问蓝牙设备的重命名名称?

Dmy*_*yuk 14

您应该使用别名.

用于设置重命名设备:

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();
}
Run Code Online (Sandbox Code Playgroud)

获取设备名称:

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();
}
Run Code Online (Sandbox Code Playgroud)