关于ACTION_LOCAL_NAME_CHANGED使用时的注意事项

本文解析了Android系统中蓝牙适配器更改名称时触发的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;
  }
}

 

 

 

 

 

详细讲解: BluetoothBondStateMachine: Bond address is:24:1A:E6:5A:BD:5B bt_btif : LogMsg: btif_dm_create_bond: bd_addr=24:1a:e6:5a:bd:5b, transport=0 bt_btif_dm: get_cod: get_cod remote_cod = 0x00240404 bt_btm_sec: btm_sec_change_pairing_state: Pairing state changed IDLE => GET_REM_NAME pairing_flags:0x1 BluetoothBondStateMachine: Entering PendingCommandState State bt_btif : LogMsg: operator(): HAL bt_hal_cbacks->discovery_state_changed_cb bt_btif : LogMsg: operator(): HAL bt_hal_cbacks->bond_state_changed_cb BluetoothBondStateMachine: Bond State Change Intent:24:1A:E6:5A:BD:5B BOND_NONE => BOND_BONDING BluetoothDeviceManager: action:android.bluetooth.device.action.BOND_STATE_CHANGED bt_btm_sec: btm_sec_change_pairing_state: Pairing state changed GET_REM_NAME => WAIT_PIN_REQ pairing_flags:0x5 bt_btm_sec: btm_sec_change_pairing_state: Pairing state changed WAIT_PIN_REQ => WAIT_LOCAL_IOCAPS pairing_flags:0x5 bt_btif : LogMsg: BTA_DM_LINK_UP_EVT. Sending BT_ACL_STATE_CONNECTED bt_btif : LogMsg: btif_dm_upstreams_evt: ev: BTA_DM_LINK_UP_EVT HeadsetService: ACTION_ACL_CONNECTED mac is 24:1A:E6:5A:BD:5B bt_btm_sec: btm_sec_change_pairing_state: Pairing state changed WAIT_LOCAL_IOCAPS => WAIT_NUM_CONFIRM pairing_flags:0x5 bt_btm_sec: btm_sec_change_pairing_state: Pairing state changed WAIT_NUM_CONFIRM => WAIT_AUTH_COMPLETE pairing_flags:0x5 bt_btm_sec: btm_sec_change_pairing_state: Pairing state changed WAIT_AUTH_COMPLETE => IDLE pairing_flags:0x5 BluetoothBondStateMachine: Bond State Change Intent:24:1A:E6:5A:BD:5B BOND_BONDING => BOND_BONDED BluetoothDeviceManager: action:android.bluetooth.device.action.BOND_STATE_CHANGED BluetoothHeadsetService: ACTION_CONNECTION_STATE_CHANGED mPreWearStateList = {24:1A:E6:5A:BD:5B=0} BluetoothHeadsetService: device 24:1A:E6:5A:BD:5B connected
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值