我正在研究 android 和 BLE 设备连接。我想同时连接多个 BLE 设备。如何做到这一点?
1 回答
您可以从您的 android 应用程序连接到多个 BLE 设备。
连接:为每个 BLE 设备调用此代码mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
将所有内容保存 mBluetoothGatt
到列表中。
阅读:在mGattCallback
方法中onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
或onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
有gatt
参数。gatt.getDevice().getAddress()
将为您提供接收数据的 BLE 设备的 mac 地址。
写作:使用mBluetoothGatt.getDevice().getAddress()
你总是知道你指向的设备。您可以将命令写入它。在mGattCallback
方法onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
中,参数gatt
会给你mac地址来确认写命令。
您可以mGattCallback
为所有连接使用单个。如果您要区分并且不想总是比较mac地址,请为每个连接进行单个回调。
这将告诉您您的 android 设备可以支持多少个连接。
如果您仍有疑问,请随时询问。