【问题标题】:Bluetooth BLE device not getting Bonded after pairing process配对过程后蓝牙 BLE 设备未绑定
【发布时间】:2021-10-27 10:06:49
【问题描述】:

我正在研究 BLE,要求如下:

有一个从我的本地数据库加载的设备列表。

在点击特定设备或行时,我必须检查设备是否已绑定,如果已绑定,我必须导航到特定屏幕。如果未绑定,则配对对话框已成功打开,我还可以输入密码以配对该 BLE 设备。

但问题是在输入正确的 Pair 密码后,我无法在广播接收器中获得 BLE 设备的 BONDED 状态,如下所示:

(注意:我们知道如果 BLE 未绑定并且我们尝试连接,则配对对话框将自动打开。)

请检查以下代码:

 private fun initializeBluetoothBroadcastReceiver() {
    bluetoothBroadCastReceiver= object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            Log.e("%%%%% ", "%%%%% inside onreceive")
            val action = intent.action
            if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
                Log.e("%%%%% ", "%%%%% inside ACTION_STATE_CHANGED")
                if (bleManager.bluetoothGatt?.device?.bondState == BluetoothDevice.BOND_BONDED) {
                    Log.e("%%%%% ", "%%%%% inside BOND_BONDED")
                    if (isBleDeviceConnected) {
                        Log.e("%%%%% ", "%%%%% inside isBleDeviceConnected")
                        bleManager.bluetoothGattCallback?.writeTime()
                        loadBatteryStatusFragement(false)
                    }
                }else{
                    Log.e("%%%%% ", "%%%%% inside not BOND_BONDED")
                }
            }else{
                Log.e("%%%%% ", "%%%%% inside ACTION_STATE_CHANGED ELSE")
            }
        }
    }
    val filter1 = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
    filter1.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
    registerReceiver(bluetoothBroadCastReceiver, filter1)
}

所以,如果 BLE 设备未配对,如果我尝试然后 --> 正在打开配对弹出窗口,输入成功密码后,我得到以下日志:

%%%%%: %%%%% inside onreceive
%%%%%: %%%%% inside ACTION_STATE_CHANGED ELSE

而不是进入 BOND_BONDED

可能是什么问题?谢谢。

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy broadcastreceiver android-bluetooth


    【解决方案1】:

    使用的 FLAG 存在问题。

    而不是使用这个标志:ACTION_STATE_CHANGED

    if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
    

    我们必须使用这个标志:ACTION_BOND_STATE_CHANGED

    if (action == BluetoothDevice.ACTION_BOND_STATE_CHANGED) {
    

    解决的代码如下:

    private fun initializeBluetoothBroadcastReceiver() {
        bluetoothBroadCastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val action = intent.action
                if (action == BluetoothDevice.ACTION_BOND_STATE_CHANGED) {
                    if (bleManager.bluetoothGatt?.device?.bondState == BluetoothDevice.BOND_BONDED) {
                        if(isBleDeviceConnected)
                        {
                            performNecessaryAction()
                        }else{
                            showConnectionFailedDialog()
                        }
    
                    } else if (bleManager.bluetoothGatt?.device?.bondState == BluetoothDevice.BOND_NONE) {
                        try {
                            isBleDeviceConnected = false
                            DialogUtils.showPairingFailedDialog(
                                this@HomeActivity,
                                objLanguageJson?.error_message?.err_battery_pairing_failed
                            )
                            performNecessaryAction()
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    } else if (bleManager.bluetoothGatt?.device?.bondState == BluetoothDevice.BOND_BONDING) {
                        Log.e("%%%%% ", "%%%%% bonding none")
                    }
                } else if (action == BluetoothAdapter.ACTION_STATE_CHANGED) {
                    val state =
                        intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
                    Log.e("%%%%% ", "%%%%% state name >>> " + state)
                    when (state) {
                        BluetoothAdapter.STATE_OFF -> {
                            Log.e("%%%%% ", "%%%%% bluetooth turns off ")
                            val bluetoothAdapter: BluetoothAdapter =
                                BluetoothAdapter.getDefaultAdapter()
                            val isEnabled: Boolean = bluetoothAdapter.isEnabled
                            if (!isEnabled) {
                                bluetoothAdapter.enable()
                            }
                            if (isBleDeviceConnected) {
                                isBleDeviceConnected = false
                               
                        BluetoothAdapter.STATE_TURNING_OFF -> {
    
                        }
                        BluetoothAdapter.STATE_ON -> {
                        }
                        BluetoothAdapter.STATE_TURNING_ON -> {
                        }
                    }
                }
            }
        }
    
        val filter = IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
        registerReceiver(bluetoothBroadCastReceiver, filter)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 2014-02-04
      • 1970-01-01
      • 2016-09-26
      相关资源
      最近更新 更多