我已经实现了蓝牙低功耗设备扫描逻辑,但是我在使用BluetoothGatt服务方面有些挣扎。我要做的是从我的Android手机顺序连接到每个ESP32设备,以便我可以接收数据,然后继续连接到下一个设备(并与先前的设备断开连接)。因此ESP32和Android手机之间只有一个活动连接。 ESP32已经编程,因此如果Android手机使用BluetoothGatt连接到它,则它会发送数据(最大20个字节)。
真正的困难是了解如何管理这些连接以正确关闭/释放资源。
管理这些BluetoothGatt
连接的正确/最简单的方法是什么?
当发送的数据为"end"
时,我的实现基本上连接到新的设备Gatt服务。问题是,如果一个ESP32设备处于活动状态,则此方法有效。如果有更多设备处于活动状态,则将发生某些情况,并且无法从Gatt服务接收数据。
这是我的实现的一部分(对不起,我无法再减小代码的大小):
1)我使用BLE扫描仪发现新设备
private Handler _leScanHandler = new Handler();
private final BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
final int newRSSI = rssi;
_leScanHandler.post(new Runnable() {
@Override
public void run() {
addDeviceToListOnLEScan(device, newRSSI);
}
});
}
};
2)并且被调用的方法基本上可以管理BluetoothGatt
的对接。
public void addDeviceToListOnLEScan(BluetoothDevice device, int rssi) {
// Gets only user defined devices
boolean isUserDefinedDevice = _definedDevicesHashMap.containsKey(device.getAddress());
if (!isUserDefinedDevice) {
return;
}
// Adds device and updates 'LastModifiedTime = Date(System.currentTimeMillis())'
addOrUpdateDevice(device.getAddress(), _scannedDevices);
// Called only once on each connect button press to enable gatt operations
if (!_isInitialConnectionHasBeenMade) {
_isDataSendingCompleteFromCurrentGatt = true;
_isInitialConnectionHasBeenMade = true;
}
// Sequential BLE device connect/disconnect operations
if (_isDataSendingCompleteFromCurrentGatt) {
BluetoothGatt previousGatt = _definedDevicesHashMap(previousAddress);
if (previousGatt != null) {
previousGatt.disconnect(); // ?
}
BluetoothGatt nextGatt = _definedDevicesHashMap(nextAddress);
if (/* Checks if 'nextAddress' is in _scannedDevices */
/* And whether device 'IsActive()' */) {
if (nextGatt == null) {
nextGatt = connectToDeviceGattService(nextGatt)
}
else {
// Do something here ?
}
_isDataSendingCompleteFromCurrentGatt = false;
}
}
}
3)我正在使用以下变量/类
private boolean _isDataSendingCompleteFromCurrentGatt = false;
private boolean _isInitialConnectionHasBeenMade = false;
private HashMap<String, BluetoothGatt> _definedDevicesHashMap;
_definedDevicesHashMap.put("ff:ff:9f:c8:c2:93", null);
_definedDevicesHashMap.put("ff:ff:9f:c8:c4:91", null);
...
private HashMap<String, MyBLEDevice> _scannedDevices;
public class MyBLEDevice
{
private final int deviceInactivityTimeout = 10;
private String MacAddress;
private Date _lastModifiedDate;
public boolean isDeviceActive() {
// Just gets the time difference (DateNow - lastModified) / 1000 < 10s
}
}
4)我用来连接设备的方法
public BluetoothGatt connectToDeviceGattService(BluetoothGatt currentGatt, BluetoothDevice device, BluetoothGattCallback callback) {
_bluetoothAdapter.cancelDiscovery();
if (currentGatt == null) {
currentGatt = device.connectGatt(_activity, true, callback);
}
else {
// Is anything here needed ?
}
return currentGatt;
}
5)BluetoothGatt
回调
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED) {
// Discover services ?
} else if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_DISCONNECTED) {
// Do nothing ?
} else if (status != BluetoothGatt.GATT_SUCCESS) {
// Disconnect from current BluetoothGatt instance?
// Also close the connection ?
_isDataSendingCompleteFromCurrentGatt = true;
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
enableGattConfigurationCharacteristic();
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
_gattServiceHandler.post(new Runnable() {
@Override
public void run() {
handleMessage(Message.obtain(null, MSG_CHARACTERISTIC_CHANGE, characteristic));
}
});
}
};
6)最后,用于从Gatt回调接收数据的处理程序
private static final int MSG_CHARACTERISTIC_CHANGE = 0;
private Handler _gattServiceHandler = new Handler();
private void handleMessage(Message msg) {
BluetoothGattCharacteristic characteristic;
switch (msg.what) {
case MSG_CHARACTERISTIC_CHANGE: {
characteristic = (BluetoothGattCharacteristic) msg.obj;
if (BLELogic.PROPERTY_NOTIFY_CHAR_UUID.equals(characteristic.getUuid())) {
String notification = BluetoothExtension.getCharacteristicValue(characteristic);
if (notification.equals("end")) {
_isDataSendingCompleteFromCurrentGatt = true;
} else {
UpdateViewOnGattNotificationSent(notification);
}
}
}
default:
return;
}
}
一开始,我想简化所有逻辑,但是看起来当使用BLE / Gatt服务连接时,这没有什么简单的。
答案 0 :(得分:0)
这是您可能会感兴趣的Question and answer
离答案很短:
要实现多个BLE连接,您必须存储多个 BluetoothGatt对象,并将这些对象用于不同的设备。至 存储BluetoothGatt的多个连接对象,可以使用Map <>
私有地图connectedDeviceMap; .............