Set Bluetooth device name in Flutter

本小妞迷上赌 提交于 2020-06-17 04:25:12

问题


It is possible to change the Bluetooth device name using BluetoothAdapter.getDefaultAdapter().setName() in Android, but I am unable to find how we can do it in Flutter. I've tried using the flutter_blue package; however, I don't see how we can achieve it.

Thanks in advance!


回答1:


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


来源:https://stackoverflow.com/questions/60788450/set-bluetooth-device-name-in-flutter

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