Skip to content

Commit

Permalink
ble_transport: Fixed a BLE characteristic caching issue.
Browse files Browse the repository at this point in the history
Root cause: Android's BLE stack caches all characteristics received from peripheral.
Even if peripheral has removed characteristics, app gets that characteristic from the cached data,
but will not be able to communicate on that characteristic.
This results in device communication failure.

Solution: Added API to refresh services. Also ignored the characteristic whose descriptor data is null.
  • Loading branch information
KhushbuShah25 committed Oct 26, 2021
1 parent cf458ca commit 4285429
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To get this app please clone this repository using the below command:
```
And add a dependency code to your app module's `build.gradle` file.
```
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.0.10'
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.0.11'
```

## Using Provisioning Library
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "com.espressif.wifi_provisioning"
minSdkVersion 23
targetSdkVersion 30
versionCode 13
versionName "2.0.10 - ${getGitHash()}"
versionCode 14
versionName "2.0.11 - ${getGitHash()}"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ public void disconnectDevice() {
disableOnlyWifiNetwork();
}

public void refreshServicesOfBleDevice() {

if (transport instanceof BLETransport) {
((BLETransport) transport).refreshServices();
}
}

/**
* This method is used to set Proof Of Possession.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -154,6 +155,20 @@ public void disconnect() {
}
}

public void refreshServices() {
Log.e(TAG, "Refresh services...");
try {
// BluetoothGatt gatt
final Method refresh = bluetoothGatt.getClass().getMethod("refresh");
if (refresh != null) {
refresh.invoke(bluetoothGatt);
}
} catch (Exception e) {
e.printStackTrace();
}
bluetoothGatt.discoverServices();
}

private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {

@Override
Expand Down Expand Up @@ -186,6 +201,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
public void onServicesDiscovered(BluetoothGatt gatt, int status) {

super.onServicesDiscovered(gatt, status);
Log.d(TAG, "On services discovered");

if (status != BluetoothGatt.GATT_SUCCESS) {
Log.d(TAG, "Status not success");
Expand Down Expand Up @@ -233,19 +249,25 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

Log.d(TAG, "DescriptorRead, : Status " + status + " Data : " + new String(descriptor.getValue(), StandardCharsets.UTF_8));
Log.d(TAG, "DescriptorRead, : Status " + status);
byte[] data = descriptor.getValue();
String charUuid = descriptor.getCharacteristic().getUuid().toString();

if (status != BluetoothGatt.GATT_SUCCESS) {
Log.e(TAG, "Failed to read descriptor");
EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
return;
charUuidList.remove(charUuid);
// EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
// return;
}

byte[] data = descriptor.getValue();

String value = new String(data, StandardCharsets.UTF_8);
uuidMap.put(value, descriptor.getCharacteristic().getUuid().toString());
Log.d(TAG, "Value : " + value + " for UUID : " + descriptor.getCharacteristic().getUuid().toString());
if (data == null) {
Log.e(TAG, "Descriptor value is null");
charUuidList.remove(charUuid);
} else {
String value = new String(data, StandardCharsets.UTF_8);
uuidMap.put(value, charUuid);
Log.d(TAG, "DescriptorRead, Value : " + value + " for UUID : " + charUuid);
}

if (isReadingDescriptors) {

Expand Down

0 comments on commit 4285429

Please sign in to comment.