Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 65 additions & 27 deletions android/src/main/java/it/innove/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,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 @@ -383,8 +387,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 @@ -399,13 +407,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, 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, maxByteSize, null, callback, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -420,13 +432,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, maxByteSize, 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, maxByteSize, queueSleepTime, callback, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -440,8 +456,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 @@ -457,13 +477,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 @@ -477,6 +499,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 @@ -492,7 +516,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 @@ -502,7 +530,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 @@ -512,7 +544,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 @@ -720,6 +756,8 @@ private void disconnectPeripherals() {
if (peripheral.isConnected()) {
peripheral.disconnect(null, true);
}
peripheral.errorAndClearAllCallbacks("disconnected by BleManager");
peripheral.resetQueuesAndBuffers();
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions android/src/main/java/it/innove/DefaultScanManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanRecord;
Expand Down Expand Up @@ -45,7 +47,9 @@ public void stopScan(Callback callback) {
// update scanSessionId to prevent stopping next scan by running timeout thread
scanSessionId.incrementAndGet();

getBluetoothAdapter().getBluetoothLeScanner().stopScan(mScanCallback);
final BluetoothLeScanner scanner = getBluetoothAdapter().getBluetoothLeScanner();
if (scanner != null)
scanner.stopScan(mScanCallback);
isScanning = false;
callback.invoke();
}
Expand Down Expand Up @@ -173,7 +177,8 @@ public void run() {
// check current scan session was not stopped
if (scanSessionId.intValue() == currentScanSession) {
if (btAdapter.getState() == BluetoothAdapter.STATE_ON) {
btAdapter.getBluetoothLeScanner().stopScan(mScanCallback);
final BluetoothLeScanner scanner = btAdapter.getBluetoothLeScanner();
if (scanner != null) scanner.stopScan(mScanCallback);
isScanning = false;
}

Expand Down
Loading