更改 Android 蓝牙设备名称 [英] Change the Android bluetooth device name

查看:22
本文介绍了更改 Android 蓝牙设备名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以按照此问题的解决方案中的描述获取本地设备名称 DisplayAndroid 蓝牙设备名称

I know it's possible to get the local device name as described in the solution to this question Display Android Bluetooth Device Name

我想知道的是,我能否以编程方式更改本地蓝牙名称(当我处于发现模式时,其他设备会看到).我知道您可以手动更改它,但我正在编写和应用程序,我希望能够更改名称(添加一个简单的标志),以便具有相同应用程序的其他设备可以扫描并立即知道手机是否也在运行应用程序.

What I'm interested in knowing is, can I change the local buetooth name (the one other devices see when I'm in discovery mode) programaticlly. I know you can change it by hand, but I'm writing and app and I want to be able to change the name (add a simple flag) so other devices with the same application can scan and instantly know if the phone is also running the app.

tl;dr:我如何在 android 上更改蓝牙设备名称?

tl;dr: How can I change the bluetooth device name on android?

推荐答案

感谢原始答案,以下是我在实施时发现的一些可能对其他人有帮助的事情.

Thanks for the original answer, here are a few things I found when implementing that might help someone else out.

1) 必须启用 BT 才能使 setName() 工作.

1) BT has to be enabled for setName() to work.

2) BT 启用需要时间.IE.你不能只调用 enable() 然后 setName()

2) It takes time for BT to Enable. ie. you Can't just call enable() then setName()

3) 名字沉入"需要时间.IE.您不能在 setName() 之后立即调用 getName() 并期望获得新名称.

3) It takes time for the name to "sink in". ie. you can't call getName() right after setName() and expect the new name.

所以,这是我想出的一段代码,用于使用可运行对象在后台完成工作.它的时间也被限制为 10 秒,因此如果出现问题,它不会永远运行.

So, here is a snippet of code I came up with to use a runnable to get the job done in the background. It is also time bound to 10seconds, so it won't run forever if there is a problem.

最后,这是我们开机检查的一​​部分,我们通常会禁用 BT(由于电池原因).所以,我在之后关闭 BT,你可能不想那样做.

Finally, this is part of our power on check, and we normally leave BT disabled (due to battery). So, I turn BT back off after, you may not want to do that.

// BT Rename
//
final String sNewName = "Syntactics";
final BluetoothAdapter myBTAdapter = BluetoothAdapter.getDefaultAdapter();
final long lTimeToGiveUp_ms = System.currentTimeMillis() + 10000;
if (myBTAdapter != null)
{
    String sOldName = myBTAdapter.getName();
    if (sOldName.equalsIgnoreCase(sNewName) == false)
    {
        final Handler myTimerHandler = new Handler();
        myBTAdapter.enable();
        myTimerHandler.postDelayed(
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        if (myBTAdapter.isEnabled())
                        {
                            myBTAdapter.setName(sNewName);
                            if (sNewName.equalsIgnoreCase(myBTAdapter.getName()))
                            {
                                Log.i(TAG_MODULE, "Updated BT Name to " + myBTAdapter.getName());
                                myBTAdapter.disable();
                            }
                        }
                        if ((sNewName.equalsIgnoreCase(myBTAdapter.getName()) == false) && (System.currentTimeMillis() < lTimeToGiveUp_ms))
                        {
                            myTimerHandler.postDelayed(this, 500);
                            if (myBTAdapter.isEnabled())
                                Log.i(TAG_MODULE, "Update BT Name: waiting on BT Enable");
                            else
                                Log.i(TAG_MODULE, "Update BT Name: waiting for Name (" + sNewName + ") to set in");
                        }
                    }
                } , 500);
    }
}

这篇关于更改 Android 蓝牙设备名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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