关于ACTION_LOCAL_NAME_CHANGED使用时的注意事项

  先附上SDK中关ACTION_LOCAL_NAME_CHANGED的说明:

Broadcast Action: The local Bluetooth adapter has changed its friendly Bluetooth name.

This name is visible to remote Bluetooth devices.

Always contains the extra field EXTRA_LOCAL_NAME containing the name.

Requires BLUETOOTH to receive.

Constant Value: "android.bluetooth.adapter.action.LOCAL_NAME_CHANGED"

  最近开发蓝牙相关APP的时候发现了一个现象:启动蓝牙时候系统也会发布ACTION_LOCAL_NAME_CHANGED这个广播,而此时并没有使用APP中的重命名蓝牙适配器的功能。这就可能会给自身APP中的广播接收器带来一些误操作,因此需要在进行相关操作前进行一个判断操作,广播接收部分代码段如下:

public void onReceive(Context c, Intent i) 
{
  switch (i.getAction()) 
  {
    case BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED:
    {
       //必须先进行此层if判断避免启动时的误判断
       if(mBluetoothStateManager.BluetoothStateManger_getBluetoothAdapter().isEnabled())
	{
           System.out.println("Name has changed!\n");//可替换成你想要的响应操作
        }
     }break;
     default:break;
  }
}


猜你喜欢

转载自blog.csdn.net/ZHISHAN_IoT/article/details/53510577