我想等到我的设备完全配对(博美)



我正在研究BLE的东西,但为了配对,我想在相同的活动中等待

在您的应用程序中。 注册用于蓝牙配对的广播接收器

        val filter = IntentFilter()
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
        filter.addAction(BluetoothDevice.ACTION_FOUND)
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED)
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
        filter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY
        registerReceiver(mReceiver, filter)

在你的接收器里面做你的事情

 mReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
   //Do your Stuff
  }
}

谢谢 .我已经用这个做到了

if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
            BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //3 cases:
            //case1: bonded already
            if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED){
                Log.d(TAG, "BroadcastReceiver: BOND_BONDED.");
                startActivity(intentNewActivity);
            }
            //case2: creating a bone
            if (mDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
                Log.d(TAG, "BroadcastReceiver: BOND_BONDING.");
            }
            //case3: breaking a bond
            if (mDevice.getBondState() == BluetoothDevice.BOND_NONE) {
                Log.d(TAG, "BroadcastReceiver: BOND_NONE.");
            }
        }

最新更新