Skip to content

Commit 58b92cf

Browse files
committed
feat: add acces to native device
1 parent d13c599 commit 58b92cf

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"plugin.prepare": "npm run tsc && cd demo && tns plugin remove @nativescript-community/ble && tns plugin add ../plugin",
2020
"clean.demo": "rimraf demo/hooks demo/node_modules demo/platforms",
2121
"ci.tslint": "npm i && tslint '**/*.ts' --config '../tslint.json' --exclude '**/node_modules/**'",
22-
"publish": "ts-patch install && npm run build && lerna publish --create-release=github",
22+
"publish": "ts-patch install && npm run build && lerna publish --create-release=github --force-publish",
2323
"doc": "typedoc --out docs --tsconfig tsconfig.doc.json --includeDeclarations --excludeExternals --mode file --ignoreCompilerErrors --excludeNotExported --excludePrivate --theme minimal",
2424
"commitmsg": "commitlint -e $GIT_PARAMS"
2525
},

src/bluetooth.android.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function printValueToString(value: number[]) {
215215
}
216216

217217
// JS UUID -> Java
218-
export function stringToUuid(uuidStr) {
218+
export function stringToUuid(uuidStr: string) {
219219
if (uuidStr.length === 4) {
220220
uuidStr = '0000' + uuidStr + '-0000-1000-8000-00805f9b34fb';
221221
}
@@ -524,6 +524,7 @@ function initLeScanCallback() {
524524
RSSI: rssi,
525525
state: 'disconnected',
526526
advertismentData,
527+
nativeDevice: device,
527528
manufacturerId: advertismentData.manufacturerId,
528529
};
529530
if (Trace.isEnabled()) {
@@ -1603,6 +1604,23 @@ export class Bluetooth extends BluetoothCommon {
16031604
this.stopCurrentScan();
16041605
}
16051606

1607+
@bluetoothEnabled
1608+
@prepareArgs
1609+
public async getDevice(args: DisconnectOptions) {
1610+
const methodName = 'getDevice';
1611+
if (!args.UUID) {
1612+
return Promise.reject(
1613+
new BluetoothError(BluetoothCommon.msg_missing_parameter, {
1614+
method: methodName,
1615+
type: BluetoothCommon.UUIDKey,
1616+
arguments: args,
1617+
})
1618+
);
1619+
}
1620+
const pUUID = args.UUID;
1621+
return this.adapter.getRemoteDevice(pUUID);
1622+
}
1623+
16061624
@bluetoothEnabled
16071625
@prepareArgs
16081626
public async connect(args: ConnectOptions) {
@@ -1738,6 +1756,7 @@ export class Bluetooth extends BluetoothCommon {
17381756
state: stateObject.state,
17391757
services,
17401758
mtu,
1759+
nativeDevice: bluetoothDevice,
17411760
localName: adv?.localName,
17421761
manufacturerId: adv?.manufacturerId,
17431762
advertismentData: adv,

src/bluetooth.common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export function prepareArgs(target: Object, propertyKey: string, descriptor: Typ
6464
['serviceUUID', 'characteristicUUID'].forEach(function (k) {
6565
if (paramsToCheck[k]) {
6666
const matcher = (paramsToCheck[k] as string).match(pattern);
67-
// console.log('test regex', paramsToCheck[k], matcher);
6867
paramsToCheck[k] = (matcher && matcher.length > 0 ? matcher[1] : paramsToCheck[k]).toLowerCase();
6968
}
7069
});

src/bluetooth.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export class Bluetooth extends BluetoothCommon {
141141

142142
// android only
143143
getAndroidLocationManager(): any; //android.location.LocationManager
144+
145+
public getDevice(args: DisconnectOptions): any; // CBPeripheral or android.bluetooth.BluetoothDevice
144146
}
145147
export function getBluetoothInstance(): Bluetooth;
146148

src/bluetooth.ios.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export function stringToUint8Array(value, encoding = 'iso-8859-1') {
9494
}
9595

9696
import { iOSNativeHelper } from '@nativescript/core/utils/native-helper';
97+
import { DisconnectOptions } from './bluetooth';
9798

9899
export type SubPeripheralDelegate = Partial<CBPeripheralDelegate>;
99100
export type SubCentralManagerDelegate = Partial<CBCentralManagerDelegate>;
@@ -482,6 +483,7 @@ export class CBCentralManagerDelegateImpl extends NSObject implements CBCentralM
482483
localName: advertismentData.localName,
483484
RSSI,
484485
advertismentData,
486+
nativeDevice: peripheral,
485487
state: this._owner.get()._getState(peripheral.state),
486488
manufacturerId: advertismentData.manufacturerId,
487489
};
@@ -894,6 +896,22 @@ export class Bluetooth extends BluetoothCommon {
894896
this._advData = {};
895897
}
896898

899+
@bluetoothEnabled
900+
@prepareArgs
901+
public async getDevice(args: DisconnectOptions) {
902+
const methodName = 'getDevice';
903+
if (!args.UUID) {
904+
return Promise.reject(
905+
new BluetoothError(BluetoothCommon.msg_missing_parameter, {
906+
method: methodName,
907+
type: BluetoothCommon.UUIDKey,
908+
arguments: args,
909+
})
910+
);
911+
}
912+
return this.findDiscoverPeripheral(args.UUID);
913+
}
914+
897915
@bluetoothEnabled
898916
@prepareArgs
899917
public async connect(args: ConnectOptions) {
@@ -970,6 +988,7 @@ export class Bluetooth extends BluetoothCommon {
970988
name: peripheral.name,
971989
state: this._getState(peripheral.state),
972990
services,
991+
nativeDevice: peripheral,
973992
localName: adv?.localName,
974993
manufacturerId: adv?.manufacturerId,
975994
advertismentData: adv,

0 commit comments

Comments
 (0)