Skip to content

fix_ios_serviceUUID_assigned_numbers #258

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

Merged
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
18 changes: 10 additions & 8 deletions src/bluetooth.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ export function bluetoothEnabled(target: Object, propertyKey: string, descriptor
return descriptor;
}

const pattern = /0000(.{4})-0000-1000-8000-00805f9b34fb/;
// BT assigned-numbers: https://www.bluetooth.com/specifications/assigned-numbers/
const patternBtAssignedNumbers = /0000(.{4})-0000-1000-8000-00805f9b34fb/i;
export function shortenUuidIfAssignedNumber(uuid: string) {
const matcher = uuid.toLowerCase().match(patternBtAssignedNumbers);
return (matcher && matcher.length > 0 ? matcher[1] : uuid);
}

export function prepareArgs(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) {
const originalMethod = descriptor.value as Function; // save a reference to the original method

Expand All @@ -65,13 +71,9 @@ export function prepareArgs(target: Object, propertyKey: string, descriptor: Typ
const value = paramsToCheck[k];
if (value) {
if (Array.isArray(value)) {
paramsToCheck[k] = paramsToCheck[k].map(v=>{
const matcher = (v as string).match(pattern);
return (matcher && matcher.length > 0 ? matcher[1] : v).toLowerCase();
});
paramsToCheck[k] = paramsToCheck[k].map(v => shortenUuidIfAssignedNumber(v));
} else {
const matcher = (paramsToCheck[k] as string).match(pattern);
paramsToCheck[k] = (matcher && matcher.length > 0 ? matcher[1] : paramsToCheck[k]).toLowerCase();
paramsToCheck[k] = shortenUuidIfAssignedNumber(paramsToCheck[k] ?? '');
}
}
});
Expand Down Expand Up @@ -531,7 +533,7 @@ export interface DiscoverCharacteristicsOptions extends DiscoverOptions {
}

// tslint:disable-next-line:no-empty-interface
export interface StopNotifyingOptions extends CRUDOptions {}
export interface StopNotifyingOptions extends CRUDOptions { }

export interface StartNotifyingOptions extends CRUDOptions {
onNotify: (data: ReadResult) => void;
Expand Down
9 changes: 7 additions & 2 deletions src/bluetooth.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
WriteOptions,
bluetoothEnabled,
prepareArgs,
shortenUuidIfAssignedNumber,
} from './bluetooth.common';
import { Trace } from '@nativescript/core';

Expand Down Expand Up @@ -1640,8 +1641,12 @@ export class Bluetooth extends BluetoothCommon {
const subD = {
peripheralDidDiscoverCharacteristicsForServiceError: (peripheral: CBPeripheral, service: CBService, error?: NSError) => {
const UUID = NSUUIDToString(peripheral.identifier);
const sUUID = CBUUIDToString(service.UUID);
if (UUID === pUUID && sUUID === args.serviceUUID) {
const sUuidLong = CBUUIDToString(service.UUID);
const sUuidShort = shortenUuidIfAssignedNumber(sUuidLong);
if (Trace.isEnabled()) {
CLog(CLogTypes.info, `discoverCharacteristics [UUID]: ${UUID}, [pUUID]: ${pUUID}, [args.serviceUUID]: ${args.serviceUUID}, [sUuidLong]: ${sUuidLong}, [sUuidShort]: ${sUuidShort}`);
}
if (UUID === pUUID && (sUuidLong === args.serviceUUID || sUuidShort === args.serviceUUID)) {
if (error) {
reject(
new BluetoothError(error.localizedDescription, {
Expand Down