Skip to content

Commit

Permalink
Use Android v33 onCharacteristicChanged & onCharacteristicRead
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Jul 14, 2024
1 parent 7a13a16 commit 4350caf
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/android/Peripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.megster.cordova.ble.central;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;

import android.bluetooth.*;
Expand All @@ -33,6 +34,9 @@

import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.RequiresPermission;

/**
Expand Down Expand Up @@ -432,9 +436,15 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState

}

@TargetApi(32 /*S_V2*/)
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
if (Build.VERSION.SDK_INT >= 33) {
// handled by new callback below
return;
}

LOG.d(TAG, "onCharacteristicChanged %s", characteristic);

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

@RequiresApi(api = 33 /*TIRAMISU*/)
@Override
public void onCharacteristicChanged(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] data) {
super.onCharacteristicChanged(gatt, characteristic, data);
LOG.d(TAG, "onCharacteristicChanged (api:33) %s", characteristic);

SequentialCallbackContext callback = notificationCallbacks.get(generateHashKey(characteristic));

if (callback != null) {
callback.sendSequentialResult(data);
}
}

@TargetApi(32 /*S_V2*/)
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if (Build.VERSION.SDK_INT >= 33) {
// handled by new callback below
return;
}

LOG.d(TAG, "onCharacteristicRead %s", characteristic);

synchronized(this) {
Expand All @@ -464,6 +493,27 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
commandCompleted();
}

@RequiresApi(api = 33 /*TIRAMISU*/)
@Override
public void onCharacteristicRead(@NonNull BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic, @NonNull byte[] value, int status) {
super.onCharacteristicRead(gatt, characteristic, value, status);
LOG.d(TAG, "onCharacteristicRead (api:33) %s", characteristic);

synchronized(this) {
if (readCallback != null) {
if (status == BluetoothGatt.GATT_SUCCESS) {
readCallback.success(value);
} else {
readCallback.error("Error reading " + characteristic.getUuid() + " status=" + status);
}

readCallback = null;
}
}

commandCompleted();
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Expand Down

0 comments on commit 4350caf

Please sign in to comment.