Skip to content

Commit aa46506

Browse files
committed
Fixed BLE caching issue.
1 parent 3320225 commit aa46506

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ android {
1818
minSdkVersion 23
1919
targetSdkVersion 30
2020
versionCode 13
21-
versionName "2.0.10 - ${getGitHash()}"
21+
versionName "2.0.11 - ${getGitHash()}"
2222
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2323
}
2424

provisioning/src/main/java/com/espressif/provisioning/ESPDevice.java

+7
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ public void disconnectDevice() {
318318
disableOnlyWifiNetwork();
319319
}
320320

321+
public void refreshServicesOfBleDevice() {
322+
323+
if (transport instanceof BLETransport) {
324+
((BLETransport) transport).refreshServices();
325+
}
326+
}
327+
321328
/**
322329
* This method is used to set Proof Of Possession.
323330
*

provisioning/src/main/java/com/espressif/provisioning/transport/BLETransport.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.json.JSONException;
3636
import org.json.JSONObject;
3737

38+
import java.lang.reflect.Method;
3839
import java.nio.charset.StandardCharsets;
3940
import java.util.ArrayList;
4041
import java.util.HashMap;
@@ -154,6 +155,20 @@ public void disconnect() {
154155
}
155156
}
156157

158+
public void refreshServices() {
159+
Log.e(TAG, "Refresh services...");
160+
try {
161+
// BluetoothGatt gatt
162+
final Method refresh = bluetoothGatt.getClass().getMethod("refresh");
163+
if (refresh != null) {
164+
refresh.invoke(bluetoothGatt);
165+
}
166+
} catch (Exception e) {
167+
e.printStackTrace();
168+
}
169+
bluetoothGatt.discoverServices();
170+
}
171+
157172
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
158173

159174
@Override
@@ -186,6 +201,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
186201
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
187202

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

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

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

238256
if (status != BluetoothGatt.GATT_SUCCESS) {
239257
Log.e(TAG, "Failed to read descriptor");
240-
EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
241-
return;
258+
charUuidList.remove(charUuid);
259+
// EventBus.getDefault().post(new DeviceConnectionEvent(ESPConstants.EVENT_DEVICE_CONNECTION_FAILED));
260+
// return;
242261
}
243262

244-
byte[] data = descriptor.getValue();
245-
246-
String value = new String(data, StandardCharsets.UTF_8);
247-
uuidMap.put(value, descriptor.getCharacteristic().getUuid().toString());
248-
Log.d(TAG, "Value : " + value + " for UUID : " + descriptor.getCharacteristic().getUuid().toString());
263+
if (data == null) {
264+
Log.e(TAG, "Descriptor value is null");
265+
charUuidList.remove(charUuid);
266+
} else {
267+
String value = new String(data, StandardCharsets.UTF_8);
268+
uuidMap.put(value, charUuid);
269+
Log.d(TAG, "DescriptorRead, Value : " + value + " for UUID : " + charUuid);
270+
}
249271

250272
if (isReadingDescriptors) {
251273

0 commit comments

Comments
 (0)