Skip to content

Commit 4e85c63

Browse files
authored
Merge pull request #702 from bastiaanv/master
[Feature] Added support for multiple & same uuid's
2 parents 8f5a06f + c5be5fa commit 4e85c63

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
@@ -1149,7 +1149,9 @@ bluetoothle.read(readSuccess, readError, params);
11491149
##### Params #####
11501150
* address = The address/identifier provided by the scan's return object
11511151
* service = The service's UUID
1152+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
11521153
* characteristic = The characteristic's UUID
1154+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
11531155

11541156
```javascript
11551157
{
@@ -1187,7 +1189,9 @@ bluetoothle.subscribe(subscribeSuccess, subscribeError, params);
11871189
##### Params #####
11881190
* address = The address/identifier provided by the scan's return object
11891191
* service = The service's UUID
1192+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
11901193
* characteristic = The characteristic's UUID
1194+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
11911195

11921196
```javascript
11931197
{
@@ -1208,17 +1212,21 @@ bluetoothle.subscribe(subscribeSuccess, subscribeError, params);
12081212
{
12091213
"status": "subscribed",
12101214
"characteristic": "2a37",
1215+
"characteristicIndex": 0,
12111216
"name": "Polar H7 3B321015",
12121217
"service": "180d",
1218+
"serviceIndex": 0,
12131219
"address": "ECC037FD-72AE-AFC5-9213-CA785B3B5C63"
12141220
}
12151221

12161222
{
12171223
"status": "subscribedResult",
12181224
"value": "U3Vic2NyaWJlIEhlbGxvIFdvcmxk", //Subscribe Hello World
12191225
"characteristic": "2a37",
1226+
"characteristicIndex": 0,
12201227
"name": "Polar H7 3B321015",
12211228
"service": "180d",
1229+
"serviceIndex": 0,
12221230
"address": "ECC037FD-72AE-AFC5-9213-CA785B3B5C63"
12231231
}
12241232
```
@@ -1235,7 +1243,9 @@ bluetoothle.unsubscribe(unsubscribeSuccess, unsubscribeError, params);
12351243
##### Params #####
12361244
* address = The address/identifier provided by the scan's return object
12371245
* service = The service's UUID
1246+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
12381247
* characteristic = The characteristic's UUID
1248+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
12391249

12401250
```javascript
12411251
{
@@ -1272,7 +1282,9 @@ bluetoothle.write(writeSuccess, writeError, params);
12721282
##### Params #####
12731283
* address = The address/identifier provided by the scan's return object
12741284
* service = The service's UUID
1285+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
12751286
* characteristic = The characteristic's UUID
1287+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
12761288
* value = Base64 encoded string
12771289
* type = Set to "noResponse" to enable write without response, all other values will write normally.
12781290

@@ -1319,7 +1331,9 @@ bluetoothle.writeQ(writeSuccess, writeError, params);
13191331
##### Params #####
13201332
* address = The address/identifier provided by the scan's return object
13211333
* service = The service's UUID
1334+
* serviceIndex = When dealing with multiple services with the same UUID, this index will determine which service will be used (OPTIONAL)
13221335
* characteristic = The characteristic's UUID
1336+
* characteristicIndex = When dealing with multiple characteristics with the same UUID, this index will determine which chracteristic will be used (OPTIONAL)
13231337
* value = Base64 encoded string
13241338
* type = Set to "noResponse" to enable write without response, all other values will write normally.
13251339
* 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
@@ -3148,25 +3148,35 @@ private UUID getUUID(String value) {
31483148
private BluetoothGattService getService(BluetoothGatt bluetoothGatt, JSONObject obj) {
31493149
UUID uuid = getUUID(obj.optString("service", null));
31503150

3151-
BluetoothGattService service = bluetoothGatt.getService(uuid);
3152-
3153-
if (service == null) {
3154-
return null;
3151+
int serviceIndex = obj.optInt("serviceIndex", 0);
3152+
int found = 0;
3153+
List<BluetoothGattService> services = bluetoothGatt.getServices();
3154+
for (BluetoothGattService service : services) {
3155+
if (service.getUuid().equals(uuid) && serviceIndex == found) {
3156+
return service;
3157+
} else if (service.getUuid().equals(uuid) && serviceIndex != found) {
3158+
found++;
3159+
}
31553160
}
31563161

3157-
return service;
3162+
return null;
31583163
}
31593164

31603165
private BluetoothGattCharacteristic getCharacteristic(JSONObject obj, BluetoothGattService service) {
31613166
UUID uuid = getUUID(obj.optString("characteristic", null));
31623167

3163-
BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);
3164-
3165-
if (characteristic == null) {
3166-
return null;
3168+
int characteristicIndex = obj.optInt("characteristicIndex", 0);
3169+
int found = 0;
3170+
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
3171+
for (BluetoothGattCharacteristic characteristic : characteristics) {
3172+
if (characteristic.getUuid().equals(uuid) && characteristicIndex == found) {
3173+
return characteristic;
3174+
} else if (characteristic.getUuid().equals(uuid) && characteristicIndex != found) {
3175+
found++;
3176+
}
31673177
}
31683178

3169-
return characteristic;
3179+
return null;
31703180
}
31713181

31723182
private BluetoothGattDescriptor getDescriptor(JSONObject obj, BluetoothGattCharacteristic characteristic) {
@@ -4252,6 +4262,29 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
42524262
addProperty(returnObj, keyStatus, statusSubscribedResult);
42534263
addPropertyBytes(returnObj, keyValue, characteristic.getValue());
42544264

4265+
int serviceIndex = 0;
4266+
BluetoothGattService serviceFromCharacteristic = characteristic.getService();
4267+
4268+
for (BluetoothGattService service : gatt.getServices()) {
4269+
if (service.equals(serviceFromCharacteristic)) {
4270+
break;
4271+
} else if(service.getUuid().equals(serviceFromCharacteristic.getUuid())) {
4272+
serviceIndex++;
4273+
}
4274+
}
4275+
4276+
int characteristicIndex = 0;
4277+
for (BluetoothGattCharacteristic characteristicFromService : serviceFromCharacteristic.getCharacteristics()) {
4278+
if (characteristicFromService.equals(characteristic)) {
4279+
break;
4280+
} else if (characteristicFromService.getUuid().equals(characteristic.getUuid())) {
4281+
characteristicIndex++;
4282+
}
4283+
}
4284+
4285+
addProperty(returnObj, "serviceIndex", serviceIndex);
4286+
addProperty(returnObj, "characteristicIndex", characteristicIndex);
4287+
42554288
//Return the characteristic value
42564289
callbackContext.sendSequentialResult(returnObj);
42574290
}

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)