Skip to content

Add a native method for disconnecting all connected Peripherals #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: pff
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion BleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class BleManager {

connectLe(peripheralId) {
return new Promise((fulfill, reject) => {
bleManager.connect(peripheralId, error => {
bleManager.connectLe(peripheralId, error => {
if (error) {
reject(error);
} else {
Expand Down
36 changes: 35 additions & 1 deletion android/src/main/java/it/innove/BleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@
import static android.bluetooth.BluetoothProfile.GATT;
import static android.os.Build.VERSION_CODES.LOLLIPOP;

class BleManager extends ReactContextBaseJavaModule {
public class BleManager extends ReactContextBaseJavaModule {

private static BleManager instance;

public static BleManager getInstance() {
return instance;
}

public static final String LOG_TAG = "ReactNativeBleManager";
private static final int ENABLE_REQUEST = 539;
Expand Down Expand Up @@ -99,6 +105,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,

public BleManager(ReactApplicationContext reactContext) {
super(reactContext);
instance = this; // Assign instance when JS initializes it
context = reactContext;
this.reactContext = reactContext;
reactContext.addActivityEventListener(mActivityEventListener);
Expand Down Expand Up @@ -765,4 +772,31 @@ public void onCatalystInstanceDestroy() {
scanManager.stopScan(args -> {});
}
}

/**
* Disconnects all connected peripherals by first sending a termination message.
*
* This method:
* 1. Iterates through all connected peripherals.
* 2. Calls `disconnectWithServiceUUID` on each peripheral.
* 3. Ensures all devices receive a termination message before disconnecting.
*
* @param serviceUUID The UUID of the BLE service that contains the characteristic for termination messaging.
* @param characteristicUUID The UUID of the BLE characteristic to which the termination message will be written.
* @param message A byte array containing the termination message.
*
* @note This method ensures that all connected peripherals receive a termination message before being disconnected.
*/
public void disconnectAllPeripheralsWithServiceUUID(String serviceUUID, String characteristicUUID, byte[] message) {
Log.d(LOG_TAG, "Disconnecting all connected peripherals...");

synchronized (peripherals) {
for (Peripheral peripheral : peripherals.values()) {
if (peripheral.isConnected()) {
peripheral.disconnectWithServiceUUID(serviceUUID, characteristicUUID, message);
}
}
}
}

}
51 changes: 51 additions & 0 deletions android/src/main/java/it/innove/Peripheral.java
Original file line number Diff line number Diff line change
Expand Up @@ -1051,4 +1051,55 @@ private String generateHashKey(UUID serviceUUID, BluetoothGattCharacteristic cha
return String.valueOf(serviceUUID) + "|" + characteristic.getUuid() + "|" + characteristic.getInstanceId();
}

/**
* Disconnects the peripheral by first sending a termination message to a specific BLE characteristic.
*
* This method:
* 1. Checks if the peripheral is connected.
* 2. Finds the specified service and characteristic.
* 3. Writes a termination message to the characteristic.
* 4. Disconnects the peripheral after the message is sent.
*
* @param serviceUUID The UUID of the BLE service that contains the characteristic for termination messaging.
* @param characteristicUUID The UUID of the BLE characteristic to which the termination message will be written.
* @param message A byte array containing the termination message.
*
* @note If the service or characteristic is not found, the peripheral will be disconnected immediately.
*/
public void disconnectWithServiceUUID(String serviceUUID, String characteristicUUID, byte[] message) {
if (!isConnected() || gatt == null) {
Log.e(BleManager.LOG_TAG, "Peripheral is not connected or BluetoothGatt is null. Skipping...");
return;
}

// Retrieve the BluetoothGattService
BluetoothGattService service = gatt.getService(UUIDHelper.uuidFromString(serviceUUID));
if (service == null) {
Log.e(BleManager.LOG_TAG, "Service " + serviceUUID + " not found on device: " + device.getAddress());
disconnect(true);
return;
}

// Retrieve the BluetoothGattCharacteristic
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUIDHelper.uuidFromString(characteristicUUID));
if (characteristic == null) {
Log.e(BleManager.LOG_TAG, "Characteristic " + characteristicUUID + " not found on device: " + device.getAddress());
disconnect(true);
return;
}

// Write the termination message to the characteristic
characteristic.setValue(message);
boolean success = gatt.writeCharacteristic(characteristic);
if (success) {
Log.d(BleManager.LOG_TAG, "Termination message sent to device: " + device.getAddress());
} else {
Log.e(BleManager.LOG_TAG, "Failed to send termination message to device: " + device.getAddress());
}

// Disconnect the peripheral
disconnect(true);
Log.d(BleManager.LOG_TAG, "Disconnected device: " + device.getAddress());
}

}
5 changes: 5 additions & 0 deletions ios/BleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
// For integration with external libraries, advanced use only.
+(BleManager *)getInstance;

// Disconnects all currently connected Gita devices after sending a termination message.
- (void)disconnectAllPeripheralsWithServiceUUID:(NSString *)serviceUUID
characteristicUUID:(NSString *)characteristicUUID
message:(NSArray<NSNumber *> *)message;

@end
55 changes: 55 additions & 0 deletions ios/BleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,61 @@ -(int) compareCBUUID:(CBUUID *) UUID1 UUID2:(CBUUID *)UUID2
return 0;
}

/**
* @brief Disconnects all currently connected peripherals by sending a termination message.
*
* This method iterates through all connected peripherals, writes a specified termination message
* to a given characteristic on each device.
*
* @param serviceUUID The UUID of the BLE service that contains the characteristic for termination messaging.
* @param characteristicUUID The UUID of the BLE characteristic to which the termination message will be written.
* @param message An NSArray of NSNumber values representing the termination message in bytes.
*
* @note The termination message should be 20 bytes or less to fit in a single BLE packet. If a device is connected,
* this function will first send the message using `writeWithoutResponse`, then initiate a disconnection.
*
* @see writeWithoutResponse:serviceUUID:characteristicUUID:message:maxByteSize:callback:
*/
- (void)disconnectAllPeripheralsWithServiceUUID:(NSString *)serviceUUID
characteristicUUID:(NSString *)characteristicUUID
message:(NSArray<NSNumber *> *)message {
if (!manager) {
NSLog(@"BLE Manager is NULL. Cannot disconnect peripherals.");
return;
}

NSLog(@"Disconnecting all connected peripherals...");

@synchronized(peripherals) {
for (CBPeripheral *peripheral in peripherals) {
if (peripheral.state == CBPeripheralStateConnected) {

// Get the device UUID
NSString *deviceUUID = peripheral.identifier.UUIDString;

// Set maximum byte size and queue sleep time (adjustable)
NSInteger maxByteSize = 20; // Default max bytes per BLE packet
NSInteger queueSleepTime = 10; // Delay between packets in ms

NSLog(@"Sending termination message to %@ on service %@, characteristic %@",
deviceUUID, serviceUUID, characteristicUUID);

// Call the writeWithoutResponse method directly
[self writeWithoutResponse:deviceUUID
serviceUUID:serviceUUID
characteristicUUID:characteristicUUID
message:message
maxByteSize:maxByteSize
queueSleepTime:queueSleepTime
callback:^(NSArray *response) {
NSLog(@"Termination message sent successfully to %@", deviceUUID);
}];
}
}
}
}


RCT_EXPORT_METHOD(getDiscoveredPeripherals:(nonnull RCTResponseSenderBlock)callback)
{
NSLog(@"Get discovered peripherals");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-ble-manager",
"version": "8.4.3-pff",
"version": "8.4.4-pff",
"description": "A BLE module for react native.",
"main": "BleManager",
"types": "./index.d.ts",
Expand Down