Skip to content

Commit

Permalink
Added read RSSI method
Browse files Browse the repository at this point in the history
Fix 16 bit UUID on Android
  • Loading branch information
marcosinigaglia committed Nov 29, 2016
1 parent e24dace commit 61ac11b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -26,3 +26,5 @@ DerivedData
*.xcuserstate

xcuserdata

*.iml
20 changes: 15 additions & 5 deletions android/src/main/java/it/innove/BleManager.java
Expand Up @@ -301,7 +301,7 @@ public void startNotification(String deviceUUID, String serviceUUID, String char

Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null){
peripheral.registerNotify(UUID.fromString(serviceUUID), UUID.fromString(characteristicUUID), callback);
peripheral.registerNotify(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID), callback);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -312,7 +312,7 @@ public void stopNotification(String deviceUUID, String serviceUUID, String chara

Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null){
peripheral.removeNotify(UUID.fromString(serviceUUID), UUID.fromString(characteristicUUID), callback);
peripheral.removeNotify(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID), callback);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -327,7 +327,7 @@ public void write(String deviceUUID, String serviceUUID, String characteristicUU
if (peripheral != null){
byte[] decoded = Base64.decode(message.getBytes(), Base64.DEFAULT);
Log.d(LOG_TAG, "Message(" + decoded.length + "): " + bytesToHex(decoded));
peripheral.write(UUID.fromString(serviceUUID), UUID.fromString(characteristicUUID), decoded, maxByteSize, null, callback, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
peripheral.write(UUIDHelper.uuidFromString(serviceUUID), UUIDHelper.uuidFromString(characteristicUUID), decoded, maxByteSize, null, callback, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
} else
callback.invoke("Peripheral not found");
}
Expand All @@ -340,7 +340,7 @@ public void writeWithoutResponse(String deviceUUID, String serviceUUID, String c
if (peripheral != null){
byte[] decoded = Base64.decode(message.getBytes(), Base64.DEFAULT);
Log.d(LOG_TAG, "Message(" + decoded.length + "): " + bytesToHex(decoded));
peripheral.write(UUID.fromString(serviceUUID), UUID.fromString(characteristicUUID), decoded, maxByteSize, queueSleepTime, callback, BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
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 @@ -350,7 +350,17 @@ public void read(String deviceUUID, String serviceUUID, String characteristicUUI
Log.d(LOG_TAG, "Read from: " + deviceUUID);
Peripheral peripheral = peripherals.get(deviceUUID);
if (peripheral != null){
peripheral.read(ParcelUuid.fromString(serviceUUID).getUuid(), UUID.fromString(characteristicUUID), callback);
peripheral.read(ParcelUuid.fromString(serviceUUID).getUuid(), UUIDHelper.uuidFromString(characteristicUUID), callback);
} else
callback.invoke("Peripheral not found", null);
}

@ReactMethod
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);
} else
callback.invoke("Peripheral not found", null);
}
Expand Down
32 changes: 31 additions & 1 deletion android/src/main/java/it/innove/Peripheral.java
Expand Up @@ -36,6 +36,7 @@ public class Peripheral extends BluetoothGattCallback {

private Callback connectCallback;
private Callback readCallback;
private Callback readRSSICallback;
private Callback writeCallback;

private List<byte[]> writeQueue = new ArrayList<>();
Expand Down Expand Up @@ -339,6 +340,21 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
super.onDescriptorWrite(gatt, descriptor, status);
}

@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
if (readRSSICallback != null) {
if (status == BluetoothGatt.GATT_SUCCESS) {
updateRssi(rssi);
readRSSICallback.invoke(null, rssi);
} else {
readRSSICallback.invoke("Error reading RSSI status=" + status, null);
}

readRSSICallback = null;
}
}

private void setNotify(UUID serviceUUID, UUID characteristicUUID, Boolean notify, Callback callback){
Log.d(LOG_TAG, "setNotify");

Expand All @@ -353,7 +369,7 @@ private void setNotify(UUID serviceUUID, UUID characteristicUUID, Boolean notify
if (characteristic != null) {
if (gatt.setCharacteristicNotification(characteristic, notify)) {

BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUIDHelper.uuidFromString(CHARACTERISTIC_NOTIFICATION_CONFIG));
if (descriptor != null) {

// Prefer notify over indicate
Expand Down Expand Up @@ -460,7 +476,21 @@ public void read(UUID serviceUUID, UUID characteristicUUID, Callback callback) {
callback.invoke("Read failed", null);
}
}
}

public void readRSSI(Callback callback) {
if (gatt == null) {
callback.invoke("BluetoothGatt is null", null);
return;
}

readRSSICallback = callback;

if (gatt.readRemoteRssi()) {
} else {
readCallback = null;
callback.invoke("Read RSSI failed", null);
}
}


Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/it/innove/UUIDHelper.java
Expand Up @@ -15,7 +15,7 @@ public static UUID uuidFromString(String uuid) {
if (uuid.length() == 4) {
uuid = UUID_BASE.replace("XXXX", uuid);
}
return UUID.fromString(uuid);
return UUIDHelper.uuidFromString(uuid);
}

// return 16 bit UUIDs where possible
Expand Down
1 change: 1 addition & 0 deletions ios/BleManager.h
Expand Up @@ -8,6 +8,7 @@
NSMutableDictionary* connectCallbacks;
NSMutableDictionary *readCallbacks;
NSMutableDictionary *writeCallbacks;
NSMutableDictionary *readRSSICallbacks;
NSMutableArray *writeQueue;
NSMutableDictionary *notificationCallbacks;
NSMutableDictionary *stopNotificationCallbacks;
Expand Down
35 changes: 32 additions & 3 deletions ios/BleManager.m
Expand Up @@ -289,9 +289,8 @@ - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeri
if (peripheral == nil){
// Try to retrieve the peripheral
NSLog(@"Retrieving peripheral with UUID : %@", peripheralUUID);
NSMutableArray *deviceUUIDs = [NSMutableArray new];
[deviceUUIDs addObject:[CBUUID UUIDWithString:peripheralUUID]];
NSArray<CBPeripheral *> *peripheralArray = [manager retrievePeripheralsWithIdentifiers:deviceUUIDs];
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:peripheralUUID];
NSArray<CBPeripheral *> *peripheralArray = [manager retrievePeripheralsWithIdentifiers:@[uuid]];
if([peripheralArray count] > 0){
peripheral = [peripheralArray objectAtIndex:0];
[peripherals addObject:peripheral];
Expand Down Expand Up @@ -448,6 +447,21 @@ - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(C

}

RCT_EXPORT_METHOD(readRSSI:(NSString *)deviceUUID callback:(nonnull RCTResponseSenderBlock)callback)
{
NSLog(@"readRSSI");

CBPeripheral *peripheral = [self findPeripheralByUUID:deviceUUID];

if (peripheral && peripheral.state == CBPeripheralStateConnected) {
[readRSSICallbacks setObject:callback forKey:[peripheral uuidAsString]];
[peripheral readRSSI];
} else {
callback(@[@"Peripheral not found or not connected"]);
}

}

RCT_EXPORT_METHOD(startNotification:(NSString *)deviceUUID serviceUUID:(NSString*)serviceUUID characteristicUUID:(NSString*)characteristicUUID callback:(nonnull RCTResponseSenderBlock)callback)
{
NSLog(@"startNotification");
Expand Down Expand Up @@ -518,6 +532,21 @@ - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CB

}

- (void)peripheralDidUpdateRSSI:(CBPeripheral*)peripheral error:(NSError*)error {
[self peripheral: peripheral didReadRSSI: [peripheral RSSI] error: error];
}

- (void)peripheral:(CBPeripheral*)peripheral didReadRSSI:(NSNumber*)rssi error:(NSError*)error {
NSLog(@"didReadRSSI %@", rssi);
NSString *key = [peripheral uuidAsString];
RCTResponseSenderBlock readRSSICallback = [readRSSICallbacks objectForKey: key];
if (readRSSICallback) {
readRSSICallback(@[[NSNull null], [NSNumber numberWithInteger:[rssi integerValue]]]);
[readRSSICallbacks removeObjectForKey:readRSSICallback];
}
}



- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
Expand Down
2 changes: 1 addition & 1 deletion ios/CBPeripheral+Extensions.m
Expand Up @@ -91,7 +91,7 @@ - (NSDictionary *) serializableAdvertisementData: (NSDictionary *) advertisement
if (serviceData) {
NSLog(@"%@", serviceData);

for(CBUUID *key in serviceData) {
for (CBUUID *key in [serviceData allKeys]) {
[serviceData setObject:dataToArrayBuffer([serviceData objectForKey:key]) forKey:[key UUIDString]];
[serviceData removeObjectForKey:key];
}
Expand Down

0 comments on commit 61ac11b

Please sign in to comment.