Skip to content

Commit 016135b

Browse files
committed
feat: readRssi
1 parent c275475 commit 016135b

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

src/bluetooth.android.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
MtuOptions,
1919
Peripheral,
2020
ReadOptions,
21+
ReadRSSIOptions,
2122
ReadResult,
2223
Service,
2324
StartNotifyingOptions,
@@ -921,6 +922,11 @@ function initBluetoothGattCallback() {
921922
if (Trace.isEnabled()) {
922923
CLog(CLogTypes.info, `TNS_BluetoothGattCallback.onReadRemoteRssi ---- gatt: ${gatt} rssi: ${rssi}, status: ${status}`);
923924
}
925+
this.subDelegates.forEach((d) => {
926+
if (d.onMtuChanged) {
927+
d.onReadRemoteRssi(gatt, rssi, status);
928+
}
929+
});
924930
}
925931

926932
/**
@@ -2014,6 +2020,79 @@ export class Bluetooth extends BluetoothCommon {
20142020
);
20152021
}
20162022

2023+
private async getGattDevice(args: {peripheralUUID: string}, parentMethodName: string) {
2024+
if (!args.peripheralUUID) {
2025+
throw new BluetoothError(BluetoothCommon.msg_missing_parameter, {
2026+
method: parentMethodName,
2027+
type: BluetoothCommon.peripheralUUIDKey,
2028+
arguments: args,
2029+
});
2030+
}
2031+
const pUUID = args.peripheralUUID;
2032+
const stateObject = this.connections[pUUID];
2033+
if (!stateObject) {
2034+
throw new BluetoothError(BluetoothCommon.msg_peripheral_not_connected, {
2035+
method: parentMethodName,
2036+
arguments: args,
2037+
});
2038+
}
2039+
return stateObject.device;
2040+
}
2041+
2042+
@prepareArgs
2043+
public async readRssi(args: ReadRSSIOptions) {
2044+
const methodName = 'readRssi';
2045+
const gatt = await this.getGattDevice(args, methodName);
2046+
if (Trace.isEnabled()) {
2047+
CLog(CLogTypes.info, methodName, args);
2048+
}
2049+
const pUUID = args.peripheralUUID;
2050+
return this.addToGattQueue(
2051+
() =>
2052+
new Promise((resolve, reject) => {
2053+
if (Trace.isEnabled()) {
2054+
CLog(CLogTypes.info, methodName, '---- peripheral:', pUUID);
2055+
}
2056+
this.attachSubDelegate(
2057+
{methodName, args, resolve, reject},
2058+
(clearListeners, onError) => ({
2059+
onReadRemoteRssi: (gatt: android.bluetooth.BluetoothGatt, rssi: number, status: number) => {
2060+
const device = gatt.getDevice();
2061+
let UUID: string = null;
2062+
if (device == null) {
2063+
// happens some time, why ... ?
2064+
} else {
2065+
UUID = device.getAddress();
2066+
}
2067+
if (UUID === pUUID) {
2068+
if (status === GATT_SUCCESS) {
2069+
resolve(rssi);
2070+
clearListeners();
2071+
} else {
2072+
onError(new BluetoothError(BluetoothCommon.msg_error_function_call, {
2073+
method: methodName,
2074+
arguments: args,
2075+
status,
2076+
}));
2077+
}
2078+
}
2079+
},
2080+
}),
2081+
(onError) => {
2082+
if (!gatt.readRemoteRssi()) {
2083+
onError(
2084+
new BluetoothError(BluetoothCommon.msg_error_function_call, {
2085+
method: methodName,
2086+
arguments: args,
2087+
})
2088+
);
2089+
}
2090+
},
2091+
);
2092+
})
2093+
);
2094+
}
2095+
20172096
@prepareArgs
20182097
public write(args: WriteOptions) {
20192098
const methodName = 'write';

src/bluetooth.common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ export interface MtuOptions {
502502
peripheralUUID: string;
503503
}
504504

505+
export interface ReadRSSIOptions {
506+
peripheralUUID: string;
507+
}
508+
505509
export interface DiscoverOptions {
506510
peripheralUUID: string;
507511
}

src/bluetooth.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
MtuOptions,
1616
Peripheral,
1717
ReadOptions,
18+
ReadRSSIOptions,
1819
ReadResult,
1920
ScanMode,
2021
Service,
@@ -131,6 +132,8 @@ export class Bluetooth extends BluetoothCommon {
131132
// on android an mtu request is done. The returned value is the confirmed mtu value by the device (can be lower)
132133
requestMtu(options: MtuOptions): Promise<number>;
133134

135+
public readRssi(args: ReadRSSIOptions): Promise<number>;
136+
134137
public discoverServices(args: DiscoverServicesOptions): Promise<{ services: Service[] }>;
135138
public discoverCharacteristics(args: DiscoverCharacteristicsOptions): Promise<{ characteristics: Characteristic[] }>;
136139

src/bluetooth.ios.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
MtuOptions,
1212
Peripheral,
1313
ReadOptions,
14+
ReadRSSIOptions,
1415
ReadResult,
1516
Service,
1617
StartNotifyingOptions,
@@ -1257,6 +1258,59 @@ export class Bluetooth extends BluetoothCommon {
12571258
);
12581259
}
12591260
@prepareArgs
1261+
public readRssi(args: ReadRSSIOptions) {
1262+
const methodName = 'readRssi';
1263+
return this._getWrapper(args, CBCharacteristicProperties.PropertyWrite).then(
1264+
(wrapper) =>
1265+
new Promise<number>((resolve, reject) => {
1266+
if (Trace.isEnabled()) {
1267+
CLog(CLogTypes.info, methodName, `---- peripheralUUID:${args.peripheralUUID}`);
1268+
}
1269+
const pUUID = args.peripheralUUID;
1270+
const p = wrapper.peripheral;
1271+
const subD = {
1272+
peripheralDidReadRSSIError: (peripheral: CBPeripheral, rssi: number, error?: NSError) => {
1273+
1274+
if (Trace.isEnabled()) {
1275+
CLog(CLogTypes.info, methodName, '---- peripheralDidWriteValueForCharacteristicError', error);
1276+
}
1277+
const UUID = NSUUIDToString(peripheral.identifier);
1278+
if (UUID === pUUID) {
1279+
if (error) {
1280+
reject(
1281+
new BluetoothError(error.localizedDescription, {
1282+
method: methodName,
1283+
status: error.code,
1284+
})
1285+
);
1286+
} else {
1287+
resolve(rssi);
1288+
}
1289+
p.delegate.removeSubDelegate(subD);
1290+
}
1291+
},
1292+
};
1293+
p.delegate.addSubDelegate(subD);
1294+
try {
1295+
p.readRSSI();
1296+
} catch (ex) {
1297+
if (Trace.isEnabled()) {
1298+
CLog(CLogTypes.error, methodName, '---- error:', ex);
1299+
}
1300+
p.delegate.removeSubDelegate(subD);
1301+
return reject(
1302+
new BluetoothError(ex.message, {
1303+
stack: ex.stack,
1304+
nativeException: ex.nativeException,
1305+
method: methodName,
1306+
arguments: args,
1307+
})
1308+
);
1309+
}
1310+
})
1311+
);
1312+
}
1313+
@prepareArgs
12601314
public write(args: WriteOptions) {
12611315
const methodName = 'write';
12621316
if (!args.value) {

0 commit comments

Comments
 (0)