Skip to content

Commit

Permalink
Merge pull request #1305 from innoveit/fix/check_connections
Browse files Browse the repository at this point in the history
Checking the connection before a command
  • Loading branch information
marcosinigaglia authored Dec 20, 2024
2 parents dad8d33 + 4518c70 commit 87002cd
Showing 1 changed file with 63 additions and 31 deletions.
94 changes: 63 additions & 31 deletions android/src/main/java/it/innove/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,12 @@ public void startNotification(String deviceUUID, String serviceUUID, String char
}
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
peripheral.registerNotify(UUIDHelper.uuidFromString(serviceUUID),
UUIDHelper.uuidFromString(characteristicUUID), 1, callback);
if (peripheral.isConnected()) {
peripheral.registerNotify(UUIDHelper.uuidFromString(serviceUUID),
UUIDHelper.uuidFromString(characteristicUUID), 1, callback);
} else {
callback.invoke("Peripheral not connected", null);
}
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -485,8 +489,12 @@ public void stopNotification(String deviceUUID, String serviceUUID, String chara
}
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
peripheral.removeNotify(UUIDHelper.uuidFromString(serviceUUID),
UUIDHelper.uuidFromString(characteristicUUID), callback);
if (peripheral.isConnected()) {
peripheral.removeNotify(UUIDHelper.uuidFromString(serviceUUID),
UUIDHelper.uuidFromString(characteristicUUID), callback);
} else {
callback.invoke("Peripheral not connected", null);
}
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -501,13 +509,17 @@ public void write(String deviceUUID, String serviceUUID, String characteristicUU
}
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
byte[] decoded = new byte[message.size()];
for (int i = 0; i < message.size(); i++) {
decoded[i] = Integer.valueOf(message.getInt(i)).byteValue();
if (peripheral.isConnected()) {
byte[] decoded = new byte[message.size()];
for (int i = 0; i < message.size(); i++) {
decoded[i] = Integer.valueOf(message.getInt(i)).byteValue();
}
Log.d(LOG_TAG, "Message(" + decoded.length + "): " + bytesToHex(decoded));
peripheral.write(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID),
decoded, (int) maxByteSize, null, callback, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
} else {
callback.invoke("Peripheral not connected", null);
}
Log.d(LOG_TAG, "Message(" + decoded.length + "): " + bytesToHex(decoded));
peripheral.write(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID),
decoded, (int) maxByteSize, null, callback, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -522,13 +534,17 @@ public void writeWithoutResponse(String deviceUUID, String serviceUUID, String c
}
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
byte[] decoded = new byte[message.size()];
for (int i = 0; i < message.size(); i++) {
decoded[i] = Integer.valueOf(message.getInt(i)).byteValue();
if (peripheral.isConnected()) {
byte[] decoded = new byte[message.size()];
for (int i = 0; i < message.size(); i++) {
decoded[i] = Integer.valueOf(message.getInt(i)).byteValue();
}
Log.d(LOG_TAG, "Message(" + decoded.length + "): " + bytesToHex(decoded));
peripheral.write(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID),
decoded, (int) maxByteSize, (int) queueSleepTime, callback, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
} else {
callback.invoke("Peripheral not connected", null);
}
Log.d(LOG_TAG, "Message(" + decoded.length + "): " + bytesToHex(decoded));
peripheral.write(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID),
decoded, (int) maxByteSize, (int) queueSleepTime, callback, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -542,8 +558,12 @@ public void read(String deviceUUID, String serviceUUID, String characteristicUUI
}
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
peripheral.read(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID),
callback);
if (peripheral.isConnected()) {
peripheral.read(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID),
callback);
} else {
callback.invoke("Peripheral not connected", null);
}
} else
callback.invoke("Peripheral not found", null);
}
Expand All @@ -559,13 +579,15 @@ public void readDescriptor(String deviceUUID, String serviceUUID, String charact
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral == null) {
callback.invoke("Peripheral not found", null);
} else if (!peripheral.isConnected()) {
callback.invoke("Peripheral not connected", null);
} else {
peripheral.readDescriptor(
UUIDHelper.uuidFromString(serviceUUID),
UUIDHelper.uuidFromString(characteristicUUID),
UUIDHelper.uuidFromString(descriptorUUID),
callback);
}

peripheral.readDescriptor(
UUIDHelper.uuidFromString(serviceUUID),
UUIDHelper.uuidFromString(characteristicUUID),
UUIDHelper.uuidFromString(descriptorUUID),
callback);
}

@ReactMethod
Expand All @@ -579,6 +601,8 @@ public void writeDescriptor(String deviceUUID, String serviceUUID, String charac
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral == null) {
callback.invoke("Peripheral not found", null);
} else if (!peripheral.isConnected()) {
callback.invoke("Peripheral not connected", null);
} else {
byte[] decoded = new byte[message.size()];
for (int i = 0; i < message.size(); i++) {
Expand All @@ -594,7 +618,11 @@ public void retrieveServices(String deviceUUID, ReadableArray services, Callback
Log.d(LOG_TAG, "Retrieve services from: " + deviceUUID);
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
peripheral.retrieveServices(callback);
if (peripheral.isConnected()) {
peripheral.retrieveServices(callback);
} else {
callback.invoke("Peripheral not connected", null);
}
} else
callback.invoke("Peripheral not found", null);
}
Expand All @@ -604,7 +632,11 @@ public void refreshCache(String deviceUUID, Callback callback) {
Log.d(LOG_TAG, "Refreshing cache for: " + deviceUUID);
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
peripheral.refreshCache(callback);
if (peripheral.isConnected()) {
peripheral.refreshCache(callback);
} else {
callback.invoke("Peripheral not connected", null);
}
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -614,7 +646,11 @@ public void readRSSI(String deviceUUID, Callback callback) {
Log.d(LOG_TAG, "Read RSSI from: " + deviceUUID);
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null) {
peripheral.readRSSI(callback);
if (peripheral.isConnected()) {
peripheral.readRSSI(callback);
} else {
callback.invoke("Peripheral not connected", null);
}
} else
callback.invoke("Peripheral not found", null);
}
Expand Down Expand Up @@ -949,8 +985,4 @@ public void invalidate() {
}
}

@Override
public void onCatalystInstanceDestroy() {
invalidate();
}
}

0 comments on commit 87002cd

Please sign in to comment.