|
35 | 35 | import org.json.JSONException;
|
36 | 36 | import org.json.JSONObject;
|
37 | 37 |
|
| 38 | +import java.lang.reflect.Method; |
38 | 39 | import java.nio.charset.StandardCharsets;
|
39 | 40 | import java.util.ArrayList;
|
40 | 41 | import java.util.HashMap;
|
@@ -154,6 +155,20 @@ public void disconnect() {
|
154 | 155 | }
|
155 | 156 | }
|
156 | 157 |
|
| 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 | + |
157 | 172 | private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
|
158 | 173 |
|
159 | 174 | @Override
|
@@ -186,6 +201,7 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
|
186 | 201 | public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
187 | 202 |
|
188 | 203 | super.onServicesDiscovered(gatt, status);
|
| 204 | + Log.d(TAG, "On services discovered"); |
189 | 205 |
|
190 | 206 | if (status != BluetoothGatt.GATT_SUCCESS) {
|
191 | 207 | Log.d(TAG, "Status not success");
|
@@ -233,19 +249,25 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
|
233 | 249 | @Override
|
234 | 250 | public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
|
235 | 251 |
|
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(); |
237 | 255 |
|
238 | 256 | if (status != BluetoothGatt.GATT_SUCCESS) {
|
239 | 257 | 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; |
242 | 261 | }
|
243 | 262 |
|
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 | + } |
249 | 271 |
|
250 | 272 | if (isReadingDescriptors) {
|
251 | 273 |
|
|
0 commit comments