Skip to content

Commit f71573b

Browse files
authored
Merge pull request #258 from two-bridges/fix_ios_serviceUUID_assigned_numbers
fix_ios_serviceUUID_assigned_numbers
2 parents 7054500 + 4c6248b commit f71573b

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/bluetooth.common.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ export function bluetoothEnabled(target: Object, propertyKey: string, descriptor
5252
return descriptor;
5353
}
5454

55-
const pattern = /0000(.{4})-0000-1000-8000-00805f9b34fb/;
55+
// BT assigned-numbers: https://www.bluetooth.com/specifications/assigned-numbers/
56+
const patternBtAssignedNumbers = /0000(.{4})-0000-1000-8000-00805f9b34fb/i;
57+
export function shortenUuidIfAssignedNumber(uuid: string) {
58+
const matcher = uuid.toLowerCase().match(patternBtAssignedNumbers);
59+
return (matcher && matcher.length > 0 ? matcher[1] : uuid);
60+
}
61+
5662
export function prepareArgs(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
5763
const originalMethod = descriptor.value as Function; // save a reference to the original method
5864

@@ -65,13 +71,9 @@ export function prepareArgs(target: Object, propertyKey: string, descriptor: Typ
6571
const value = paramsToCheck[k];
6672
if (value) {
6773
if (Array.isArray(value)) {
68-
paramsToCheck[k] = paramsToCheck[k].map(v=>{
69-
const matcher = (v as string).match(pattern);
70-
return (matcher && matcher.length > 0 ? matcher[1] : v).toLowerCase();
71-
});
74+
paramsToCheck[k] = paramsToCheck[k].map(v => shortenUuidIfAssignedNumber(v));
7275
} else {
73-
const matcher = (paramsToCheck[k] as string).match(pattern);
74-
paramsToCheck[k] = (matcher && matcher.length > 0 ? matcher[1] : paramsToCheck[k]).toLowerCase();
76+
paramsToCheck[k] = shortenUuidIfAssignedNumber(paramsToCheck[k] ?? '');
7577
}
7678
}
7779
});
@@ -531,7 +533,7 @@ export interface DiscoverCharacteristicsOptions extends DiscoverOptions {
531533
}
532534

533535
// tslint:disable-next-line:no-empty-interface
534-
export interface StopNotifyingOptions extends CRUDOptions {}
536+
export interface StopNotifyingOptions extends CRUDOptions { }
535537

536538
export interface StartNotifyingOptions extends CRUDOptions {
537539
onNotify: (data: ReadResult) => void;

src/bluetooth.ios.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
WriteOptions,
2121
bluetoothEnabled,
2222
prepareArgs,
23+
shortenUuidIfAssignedNumber,
2324
} from './bluetooth.common';
2425
import { Trace } from '@nativescript/core';
2526

@@ -1640,8 +1641,12 @@ export class Bluetooth extends BluetoothCommon {
16401641
const subD = {
16411642
peripheralDidDiscoverCharacteristicsForServiceError: (peripheral: CBPeripheral, service: CBService, error?: NSError) => {
16421643
const UUID = NSUUIDToString(peripheral.identifier);
1643-
const sUUID = CBUUIDToString(service.UUID);
1644-
if (UUID === pUUID && sUUID === args.serviceUUID) {
1644+
const sUuidLong = CBUUIDToString(service.UUID);
1645+
const sUuidShort = shortenUuidIfAssignedNumber(sUuidLong);
1646+
if (Trace.isEnabled()) {
1647+
CLog(CLogTypes.info, `discoverCharacteristics [UUID]: ${UUID}, [pUUID]: ${pUUID}, [args.serviceUUID]: ${args.serviceUUID}, [sUuidLong]: ${sUuidLong}, [sUuidShort]: ${sUuidShort}`);
1648+
}
1649+
if (UUID === pUUID && (sUuidLong === args.serviceUUID || sUuidShort === args.serviceUUID)) {
16451650
if (error) {
16461651
reject(
16471652
new BluetoothError(error.localizedDescription, {

0 commit comments

Comments
 (0)