简体   繁体   English

Android BLE:如何检查绑定的BLE设备是否可用于连接

[英]Android BLE: How to check if bonded BLE Device is available to connect

I'm currently testing the Glucose Profile of BLE. 我目前正在测试BLE的葡萄糖谱。 In order to get Glucose Records the BLE Glucose Meter has to be bonded first. 为了获得葡萄糖记录,必须首先粘合BLE葡萄糖计。 After the Device is bonded I don't get this device anymore in the onLeScan callback when doing startLeScan . 该设备结合后,我再也不会在得到这个设备onLeScan回调时做startLeScan

I however can get a List of all bonded BLE devices by using BluetoothAdapter.getDefaultAdapter().getBondedDevices(); 然而,我可以使用BluetoothAdapter.getDefaultAdapter().getBondedDevices();得到所有绑定BLE设备的列表BluetoothAdapter.getDefaultAdapter().getBondedDevices();

Now I want to filter the bonded devices for devices that are currently in range/available. 现在,我想过滤目前在范围/可用设备中的绑定设备。

To check if there are devices in range/available you need to scan, so that is not a solution for you given that you said they are not being detected for some reason. 要检查是否有范围/可用的设备需要扫描,因此,如果您说它们由于某种原因未被检测到,那么这不是您的解决方案。

Therefore, you could try to connect directly to the bonded device by knowing it Adress or Name. 因此,您可以尝试通过了解Adress或Name来直接连接到绑定设备。

    Set<BluetoothDevice> myBondedDevices = mBluetoothAdapter.getBondedDevices();
    String MAC = "your device Adress"
    for(BluetoothDevice mydevice:myBondedDevices ){
        Log.i("BondedInfo", MAC + "   " + mydevice.getAddress());
        if(mydevice.getAddress().equals(MAC)){
            mydevice.connectGatt(mycontext,true,mygattcallback);
            break;
        }

    }

Or the most adequated aproximation(according to myself): 或者最充分的近似(根据我自己):

  • Implement a broadcast receiver that detect bonding and connect when the device bonding is complete: 在设备绑定完成时实现检测绑定和连接的广播接收器:

      BroadcastReceiver myServiceBroadcast = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final int state=intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,BluetoothDevice.ERROR); switch(state){ case BluetoothDevice.BOND_BONDING: Log.i("Bondind Status:"," Bonding..."); break; case BluetoothDevice.BOND_BONDED: Log.i("Bondind Status:","Bonded!!"); myBluetoothDevice.connectGatt(mycontext, true, myBluetoothGattCallBack); break; case BluetoothDevice.BOND_NONE: Log.i("Bondind Status:","Fail"); break; } }; 
  • Register it properly: 正确注册:

     registerReceiver(myServiceBroadcast,new IntenFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM