如何在Android中获取远程设备的自定义蓝牙名称? [英] How to get remote devices custom Bluetooth name in Android?

查看:404
本文介绍了如何在Android中获取远程设备的自定义蓝牙名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中检索远程蓝牙设备的自定义名称. 我说的是在设置"/蓝牙"和配对设备"下的手机设置中找到的名称.

I'd like to retrieve a custom name of remote Bluetooth device in Android. I'm talking about the name that is found in phones' settings under Settings/Bluetooth and paired device.

例如我有一个名为"DoorControl"的远程蓝牙设备.在设置"->蓝牙"->配对设备"下,我已将该设备重命名为"CTRL".现在,我想访问定义的名称,以便可以为用户显示该名称.

E.g. I have a remote Bluetooth device that has name "DoorControl". Under Settings->Bluetooth->paired devices, I have renamed the device as "CTRL". Now I'd like to access the defined name so I can display it for the user.

我想在蓝牙设备列表中显示该名称.

I'd like to display that name in list of Bluetooth devices.

knownDevicesAdapter.clear();
knownDevicesArray = mBluetoothAdapter.getBondedDevices();

if (knownDevicesArray.size() > 0) {
    for (BluetoothDevice device : knownDevicesArray) {
        if (device.getName().contains("Door")) {
            knownDevicesAdapter.add(device.getName() 
                    + /*HERE I WANT THE CUSTOM NAME TO SHOW*/ "\n" 
                    + device.getAddress());
        }
    }
}

device.getName(),仅返回设备的完整原始名称,在这种情况下为"DoorControl".

device.getName(), returns only the full original name of the device, what is in this case "DoorControl".

这是必要的,因为可以有4个名称为DoorControl的设备.区别它们的唯一方法是通过他们的地址.但是对于用户友好的方法,让他们在Bluetooth设置中重命名设备并仅将其显示为设备的昵称"会容易得多.

This would be necessary since there can be 4 devices with the name DoorControl. Only way to tell the difference between them is by their address. But for the user friendly approach it would be much easier to let them rename the device in Bluetooth settings and just show that name as "nickname" for the device.

是否可以访问自定义名称,所以我不必在自己的应用程序中编写完整的重命名->为某些地址保存名称->加载名称"循环?

Is there way to access the custom name, so I don't have to write the full "rename->save the name for certain address->load the name"-cycle in my own application?

搜索了一段时间之后,我决定在自己的应用程序中编写重命名功能,因为我找不到其他方法来获取名称.

After searching for a while, I decided to write renaming ability in my own application, since I couldn't find out a way to get the names otherwise.

如果有人正在阅读并且知道我的原始问题的答案,我将很高兴知道它.

If anyone is reading this and knows the answer to my original question, I'd be happy to know it.

推荐答案

我发现的问题是getName()函数仅返回默认设备名称,而不返回用户可以设置的别名.就我而言,我有多个相同的设备配对,其默认名称为"Motorola T605".我无法区分它们,因此我使用Android设置来重命名它们.尽管如此,getName()返回了"Motorola T605".我需要获取别名.该功能存在但未公开.您可以在以下Java类中看到它:

The problem I found was the getName() function only returns the default device name, not the alias name that the user can set. In my case, I have multiple of the same devices paired and their default name is "Motorola T605". I can't tell them apart so I use Android settings to rename them. Still, getName() returned "Motorola T605". I needed to get the Alias name. The function for that exists but is not exposed. You can see it in this Java class: https://android.googlesource.com/platform/frameworks/base/+/56a2301/core/java/android/bluetooth/BluetoothDevice.java This worked for me.

首先获取配对设备的阵列:

First get the array of paired devices:

Set<BluetoothDevice> pairedDevices = mBTA.getBondedDevices();

然后遍历数组:

for (BluetoothDevice device : pairedDevices) 

然后查看是否可以找到别名:

Then see if you can find an alias name:

                           String name = null;

                        try {
                            Method m = device.getClass().getMethod("getAlias");
                            Object res = m.invoke(device);
                            if(res != null)
                            name = res.toString();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }

                        if(name == null)
                        name = device.getName();

如果无法获得别名,请获取常规名称.我可以做得更好,防止和处理异常,但似乎可以.

If you can't get an alias, get the normal name. I could do a better job of preventing and handling exceptions but it seems to work.

这篇关于如何在Android中获取远程设备的自定义蓝牙名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆