Skip to content

Commit f88650a

Browse files
committed
Use Android v33 onCharacteristicChanged & onCharacteristicRead
1 parent 6a5fb0c commit f88650a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/android/Peripheral.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.megster.cordova.ble.central;
1616

1717
import android.annotation.SuppressLint;
18+
import android.annotation.TargetApi;
1819
import android.app.Activity;
1920

2021
import android.bluetooth.*;
@@ -33,6 +34,9 @@
3334

3435
import java.lang.reflect.Method;
3536
import java.util.concurrent.atomic.AtomicBoolean;
37+
38+
import androidx.annotation.NonNull;
39+
import androidx.annotation.RequiresApi;
3640
import androidx.annotation.RequiresPermission;
3741

3842
/**
@@ -432,9 +436,15 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
432436

433437
}
434438

439+
@TargetApi(32 /*S_V2*/)
435440
@Override
436441
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
437442
super.onCharacteristicChanged(gatt, characteristic);
443+
if (Build.VERSION.SDK_INT >= 33) {
444+
// handled by new callback below
445+
return;
446+
}
447+
438448
LOG.d(TAG, "onCharacteristicChanged %s", characteristic);
439449

440450
SequentialCallbackContext callback = notificationCallbacks.get(generateHashKey(characteristic));
@@ -444,9 +454,28 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
444454
}
445455
}
446456

457+
@RequiresApi(api = 33 /*TIRAMISU*/)
458+
@Override
459+
public void onCharacteristicChanged(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] data) {
460+
super.onCharacteristicChanged(gatt, characteristic, data);
461+
LOG.d(TAG, "onCharacteristicChanged (api:33) %s", characteristic);
462+
463+
SequentialCallbackContext callback = notificationCallbacks.get(generateHashKey(characteristic));
464+
465+
if (callback != null) {
466+
callback.sendSequentialResult(data);
467+
}
468+
}
469+
470+
@TargetApi(32 /*S_V2*/)
447471
@Override
448472
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
449473
super.onCharacteristicRead(gatt, characteristic, status);
474+
if (Build.VERSION.SDK_INT >= 33) {
475+
// handled by new callback below
476+
return;
477+
}
478+
450479
LOG.d(TAG, "onCharacteristicRead %s", characteristic);
451480

452481
synchronized(this) {
@@ -464,6 +493,27 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
464493
commandCompleted();
465494
}
466495

496+
@RequiresApi(api = 33 /*TIRAMISU*/)
497+
@Override
498+
public void onCharacteristicRead(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value, int status) {
499+
super.onCharacteristicRead(gatt, characteristic, value, status);
500+
LOG.d(TAG, "onCharacteristicRead (api:33) %s", characteristic);
501+
502+
synchronized(this) {
503+
if (readCallback != null) {
504+
if (status == BluetoothGatt.GATT_SUCCESS) {
505+
readCallback.success(value);
506+
} else {
507+
readCallback.error("Error reading " + characteristic.getUuid() + " status=" + status);
508+
}
509+
510+
readCallback = null;
511+
}
512+
}
513+
514+
commandCompleted();
515+
}
516+
467517
@Override
468518
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
469519
super.onCharacteristicWrite(gatt, characteristic, status);

0 commit comments

Comments
 (0)