Skip to content

Commit c5be5fa

Browse files
committed
[Feature] Added support for multiple & same uuid's
1 parent d9517d8 commit c5be5fa

File tree

4 files changed

+118
-23
lines changed

4 files changed

+118
-23
lines changed

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,9 @@ bluetoothle.read(readSuccess, readError, params);
11421142
##### Params #####
11431143
* address = The address/identifier provided by the scan's return object
11441144
* service = The service's UUID
1145+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
11451146
* characteristic = The characteristic's UUID
1147+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
11461148

11471149
```javascript
11481150
{
@@ -1180,7 +1182,9 @@ bluetoothle.subscribe(subscribeSuccess, subscribeError, params);
11801182
##### Params #####
11811183
* address = The address/identifier provided by the scan's return object
11821184
* service = The service's UUID
1185+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
11831186
* characteristic = The characteristic's UUID
1187+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
11841188

11851189
```javascript
11861190
{
@@ -1201,17 +1205,21 @@ bluetoothle.subscribe(subscribeSuccess, subscribeError, params);
12011205
{
12021206
"status": "subscribed",
12031207
"characteristic": "2a37",
1208+
"characteristicIndex": 0,
12041209
"name": "Polar H7 3B321015",
12051210
"service": "180d",
1211+
"serviceIndex": 0,
12061212
"address": "ECC037FD-72AE-AFC5-9213-CA785B3B5C63"
12071213
}
12081214

12091215
{
12101216
"status": "subscribedResult",
12111217
"value": "U3Vic2NyaWJlIEhlbGxvIFdvcmxk", //Subscribe Hello World
12121218
"characteristic": "2a37",
1219+
"characteristicIndex": 0,
12131220
"name": "Polar H7 3B321015",
12141221
"service": "180d",
1222+
"serviceIndex": 0,
12151223
"address": "ECC037FD-72AE-AFC5-9213-CA785B3B5C63"
12161224
}
12171225
```
@@ -1228,7 +1236,9 @@ bluetoothle.unsubscribe(unsubscribeSuccess, unsubscribeError, params);
12281236
##### Params #####
12291237
* address = The address/identifier provided by the scan's return object
12301238
* service = The service's UUID
1239+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
12311240
* characteristic = The characteristic's UUID
1241+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
12321242

12331243
```javascript
12341244
{
@@ -1265,7 +1275,9 @@ bluetoothle.write(writeSuccess, writeError, params);
12651275
##### Params #####
12661276
* address = The address/identifier provided by the scan's return object
12671277
* service = The service's UUID
1278+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
12681279
* characteristic = The characteristic's UUID
1280+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
12691281
* value = Base64 encoded string
12701282
* type = Set to "noResponse" to enable write without response, all other values will write normally.
12711283

@@ -1312,7 +1324,9 @@ bluetoothle.writeQ(writeSuccess, writeError, params);
13121324
##### Params #####
13131325
* address = The address/identifier provided by the scan's return object
13141326
* service = The service's UUID
1327+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
13151328
* characteristic = The characteristic's UUID
1329+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
13161330
* value = Base64 encoded string
13171331
* type = Set to "noResponse" to enable write without response, all other values will write normally.
13181332
* chunkSize = Define the size of packets. This should be according to MTU value

src/android/BluetoothLePlugin.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,25 +3132,35 @@ private UUID getUUID(String value) {
31323132
private BluetoothGattService getService(BluetoothGatt bluetoothGatt, JSONObject obj) {
31333133
UUID uuid = getUUID(obj.optString("service", null));
31343134

3135-
BluetoothGattService service = bluetoothGatt.getService(uuid);
3136-
3137-
if (service == null) {
3138-
return null;
3135+
int serviceIndex = obj.optInt("serviceIndex", 0);
3136+
int found = 0;
3137+
List<BluetoothGattService> services = bluetoothGatt.getServices();
3138+
for (BluetoothGattService service : services) {
3139+
if (service.getUuid().equals(uuid) && serviceIndex == found) {
3140+
return service;
3141+
} else if (service.getUuid().equals(uuid) && serviceIndex != found) {
3142+
found++;
3143+
}
31393144
}
31403145

3141-
return service;
3146+
return null;
31423147
}
31433148

31443149
private BluetoothGattCharacteristic getCharacteristic(JSONObject obj, BluetoothGattService service) {
31453150
UUID uuid = getUUID(obj.optString("characteristic", null));
31463151

3147-
BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);
3148-
3149-
if (characteristic == null) {
3150-
return null;
3152+
int characteristicIndex = obj.optInt("characteristicIndex", 0);
3153+
int found = 0;
3154+
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
3155+
for (BluetoothGattCharacteristic characteristic : characteristics) {
3156+
if (characteristic.getUuid().equals(uuid) && characteristicIndex == found) {
3157+
return characteristic;
3158+
} else if (characteristic.getUuid().equals(uuid) && characteristicIndex != found) {
3159+
found++;
3160+
}
31513161
}
31523162

3153-
return characteristic;
3163+
return null;
31543164
}
31553165

31563166
private BluetoothGattDescriptor getDescriptor(JSONObject obj, BluetoothGattCharacteristic characteristic) {
@@ -4204,6 +4214,29 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
42044214
addProperty(returnObj, keyStatus, statusSubscribedResult);
42054215
addPropertyBytes(returnObj, keyValue, characteristic.getValue());
42064216

4217+
int serviceIndex = 0;
4218+
BluetoothGattService serviceFromCharacteristic = characteristic.getService();
4219+
4220+
for (BluetoothGattService service : gatt.getServices()) {
4221+
if (service.equals(serviceFromCharacteristic)) {
4222+
break;
4223+
} else if(service.getUuid().equals(serviceFromCharacteristic.getUuid())) {
4224+
serviceIndex++;
4225+
}
4226+
}
4227+
4228+
int characteristicIndex = 0;
4229+
for (BluetoothGattCharacteristic characteristicFromService : serviceFromCharacteristic.getCharacteristics()) {
4230+
if (characteristicFromService.equals(characteristic)) {
4231+
break;
4232+
} else if (characteristicFromService.getUuid().equals(characteristic.getUuid())) {
4233+
characteristicIndex++;
4234+
}
4235+
}
4236+
4237+
addProperty(returnObj, "serviceIndex", serviceIndex);
4238+
addProperty(returnObj, "characteristicIndex", characteristicIndex);
4239+
42074240
//Return the characteristic value
42084241
callbackContext.sendSequentialResult(returnObj);
42094242
}

src/ios/BluetoothLePlugin.m

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
NSString *const keyAdvertisement = @"advertisement";
1515
NSString *const keyUuid = @"uuid";
1616
NSString *const keyService = @"service";
17+
NSString *const keyServiceIndex = @"serviceIndex";
1718
NSString *const keyServices = @"services";
1819
NSString *const keyCharacteristic = @"characteristic";
1920
NSString *const keyCharacteristics = @"characteristics";
21+
NSString *const keyCharacteristicIndex = @"characteristicIndex";
2022
NSString *const keyDescriptor = @"descriptor";
2123
NSString *const keyDescriptors = @"descriptors";
2224
NSString *const keyValue = @"value";
@@ -2576,6 +2578,28 @@ - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(C
25762578
[self addDevice:peripheral :returnObj];
25772579
[self addCharacteristic:characteristic :returnObj];
25782580

2581+
// find & set serviceIndex & characteristicIndex
2582+
int serviceIndex = 0;
2583+
for (CBService* item in peripheral.services) {
2584+
if ([item isEqual: characteristic.service]) {
2585+
break;
2586+
} else if ([item.UUID isEqual: characteristic.service.UUID]) {
2587+
serviceIndex++;
2588+
}
2589+
}
2590+
2591+
int characteristicIndex = 0;
2592+
for (CBCharacteristic* item in characteristic.service.characteristics) {
2593+
if ([item isEqual: characteristic]) {
2594+
break;
2595+
} else if ([item.UUID isEqual: characteristic.UUID]) {
2596+
characteristicIndex++;
2597+
}
2598+
}
2599+
2600+
[returnObj setValue:@(serviceIndex) forKey:keyServiceIndex];
2601+
[returnObj setValue:@(characteristicIndex) forKey:keyCharacteristicIndex];
2602+
25792603
//If an error exists...
25802604
if (error != nil) {
25812605
//Get the callback based on whether subscription or read
@@ -3615,15 +3639,23 @@ -(CBService*) getService:(NSDictionary *)obj forPeripheral:(CBPeripheral*)periph
36153639
return nil;
36163640
}
36173641

3618-
CBService* service = nil;
3642+
NSString* serviceIndexFromDict = [obj valueForKey:keyServiceIndex];
3643+
int serviceIndex = 0;
36193644

3645+
if (serviceIndexFromDict != nil) {
3646+
serviceIndex = [serviceIndexFromDict intValue];
3647+
}
3648+
3649+
int found = 0;
36203650
for (CBService* item in peripheral.services) {
3621-
if ([item.UUID isEqual: uuid]) {
3622-
service = item;
3651+
if ([item.UUID isEqual: uuid] && (serviceIndex == found)) {
3652+
return item;
3653+
} else if ([item.UUID isEqual: uuid] && (serviceIndex != found)) {
3654+
found++;
36233655
}
36243656
}
3625-
3626-
return service;
3657+
3658+
return nil;
36273659
}
36283660

36293661
-(CBCharacteristic*) getCharacteristic:(NSDictionary *) obj forService:(CBService*) service {
@@ -3647,15 +3679,23 @@ -(CBCharacteristic*) getCharacteristic:(NSDictionary *) obj forService:(CBServic
36473679
return nil;
36483680
}
36493681

3650-
CBCharacteristic* characteristic = nil;
3682+
NSString* characteristicIndexFromDict = [obj valueForKey:keyCharacteristicIndex];
3683+
int characteristicIndex = 0;
36513684

3685+
if (characteristicIndexFromDict != nil) {
3686+
characteristicIndex = [characteristicIndexFromDict intValue];
3687+
}
3688+
3689+
int found = 0;
36523690
for (CBCharacteristic* item in service.characteristics) {
3653-
if ([item.UUID isEqual: uuid]) {
3654-
characteristic = item;
3691+
if ([item.UUID isEqual: uuid] && (characteristicIndex == found)) {
3692+
return item;
3693+
} else if ([item.UUID isEqual: uuid] && (characteristicIndex != found)) {
3694+
found++;
36553695
}
36563696
}
3657-
3658-
return characteristic;
3697+
3698+
return nil;
36593699
}
36603700

36613701
-(CBDescriptor*) getDescriptor:(NSDictionary *) obj forCharacteristic:(CBCharacteristic*) characteristic {

types/index.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,9 @@ declare namespace BluetoothlePlugin {
625625
/** The address/identifier provided by the scan's return object */
626626
address: string,
627627
/** The service's ID */
628-
service: string
628+
service: string,
629+
/** When dealing with multiple services with the same UUID, this index will determine which service will be used */
630+
serviceIndex?: number
629631
}
630632

631633
interface InitPeripheralParams {
@@ -714,8 +716,10 @@ declare namespace BluetoothlePlugin {
714716
}
715717

716718
interface DescriptorParams extends Params {
717-
/** The characteristic's ID */
718-
characteristic: string
719+
/** The characteristic's ID */
720+
characteristic: string;
721+
/** When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used */
722+
characteristicIndex?: number;
719723
}
720724

721725
interface OperationDescriptorParams extends DescriptorParams {
@@ -917,8 +921,12 @@ declare namespace BluetoothlePlugin {
917921
interface OperationResult extends DeviceInfo {
918922
/** Characteristic UUID */
919923
characteristic: string,
924+
/** When dealing with multiple characteristics with the same UUID, this index will determine which characteristic it is */
925+
characteristicIndex: number,
920926
/** Service's UUID */
921927
service: string,
928+
/** When dealing with multiple services with the same UUID, this index will determine which service it is */
929+
serviceIndex: number,
922930
/** Base64 encoded string of bytes */
923931
value: string
924932
}

0 commit comments

Comments
 (0)