@@ -1289,56 +1289,53 @@ export class Bluetooth extends BluetoothCommon {
12891289 ) ;
12901290 }
12911291 @prepareArgs
1292- public readRssi ( args : ReadRSSIOptions ) {
1292+ public async readRssi ( args : ReadRSSIOptions ) {
12931293 const methodName = 'readRssi' ;
1294- return this . _getWrapper ( args , CBCharacteristicProperties . PropertyWrite ) . then (
1295- ( wrapper ) =>
1296- new Promise < number > ( ( resolve , reject ) => {
1294+ const p = await this . _getDeviceWrapper ( args ) ;
1295+ return new Promise < number > ( ( resolve , reject ) => {
1296+ if ( Trace . isEnabled ( ) ) {
1297+ CLog ( CLogTypes . info , methodName , `---- peripheralUUID:${ args . peripheralUUID } ` ) ;
1298+ }
1299+ const pUUID = args . peripheralUUID ;
1300+ const subD = {
1301+ peripheralDidReadRSSIError : ( peripheral : CBPeripheral , rssi : number , error ?: NSError ) => {
12971302 if ( Trace . isEnabled ( ) ) {
1298- CLog ( CLogTypes . info , methodName , ` ---- peripheralUUID: ${ args . peripheralUUID } ` ) ;
1303+ CLog ( CLogTypes . info , methodName , ' ---- peripheralDidWriteValueForCharacteristicError' , error ) ;
12991304 }
1300- const pUUID = args . peripheralUUID ;
1301- const p = wrapper . peripheral ;
1302- const subD = {
1303- peripheralDidReadRSSIError : ( peripheral : CBPeripheral , rssi : number , error ?: NSError ) => {
1304- if ( Trace . isEnabled ( ) ) {
1305- CLog ( CLogTypes . info , methodName , '---- peripheralDidWriteValueForCharacteristicError' , error ) ;
1306- }
1307- const UUID = NSUUIDToString ( peripheral . identifier ) ;
1308- if ( UUID === pUUID ) {
1309- if ( error ) {
1310- reject (
1311- new BluetoothError ( error . localizedDescription , {
1312- method : methodName ,
1313- status : error . code
1314- } )
1315- ) ;
1316- } else {
1317- resolve ( rssi ) ;
1318- }
1319- p . delegate . removeSubDelegate ( subD ) ;
1320- }
1321- }
1322- } ;
1323- p . delegate . addSubDelegate ( subD ) ;
1324- try {
1325- p . readRSSI ( ) ;
1326- } catch ( ex ) {
1327- if ( Trace . isEnabled ( ) ) {
1328- CLog ( CLogTypes . error , methodName , '---- error:' , ex ) ;
1305+ const UUID = NSUUIDToString ( peripheral . identifier ) ;
1306+ if ( UUID === pUUID ) {
1307+ if ( error ) {
1308+ reject (
1309+ new BluetoothError ( error . localizedDescription , {
1310+ method : methodName ,
1311+ status : error . code
1312+ } )
1313+ ) ;
1314+ } else {
1315+ resolve ( rssi ) ;
13291316 }
13301317 p . delegate . removeSubDelegate ( subD ) ;
1331- return reject (
1332- new BluetoothError ( ex . message , {
1333- stack : ex . stack ,
1334- nativeException : ex . nativeException ,
1335- method : methodName ,
1336- arguments : args
1337- } )
1338- ) ;
13391318 }
1340- } )
1341- ) ;
1319+ }
1320+ } ;
1321+ p . delegate . addSubDelegate ( subD ) ;
1322+ try {
1323+ p . readRSSI ( ) ;
1324+ } catch ( ex ) {
1325+ if ( Trace . isEnabled ( ) ) {
1326+ CLog ( CLogTypes . error , methodName , '---- error:' , ex ) ;
1327+ }
1328+ p . delegate . removeSubDelegate ( subD ) ;
1329+ reject (
1330+ new BluetoothError ( ex . message , {
1331+ stack : ex . stack ,
1332+ nativeException : ex . nativeException ,
1333+ method : methodName ,
1334+ arguments : args
1335+ } )
1336+ ) ;
1337+ }
1338+ } ) ;
13421339 }
13431340 @prepareArgs
13441341 public write ( args : WriteOptions ) {
@@ -1744,52 +1741,56 @@ export class Bluetooth extends BluetoothCommon {
17441741 }
17451742
17461743 @bluetoothEnabled
1747- private _getWrapper ( args , property : CBCharacteristicProperties ) {
1744+ private async _getDeviceWrapper ( args ) {
17481745 // prepareArgs should be called before hand
17491746 if ( ! args . peripheralUUID ) {
1750- return Promise . reject (
1751- new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1752- type : BluetoothCommon . peripheralUUIDKey ,
1753- arguments : args
1754- } )
1755- ) ;
1756- }
1757- if ( ! args . serviceUUID ) {
1758- return Promise . reject (
1759- new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1760- type : BluetoothCommon . serviceUUIDKey ,
1761- arguments : args
1762- } )
1763- ) ;
1764- }
1765- if ( ! args . characteristicUUID ) {
1766- return Promise . reject (
1767- new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1768- type : BluetoothCommon . characteristicUUIDKey ,
1769- arguments : args
1770- } )
1771- ) ;
1747+ throw new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1748+ type : BluetoothCommon . peripheralUUIDKey ,
1749+ arguments : args
1750+ } ) ;
17721751 }
17731752 const peripheral = this . findPeripheral ( args . peripheralUUID ) ;
17741753 if ( ! peripheral ) {
1775- return Promise . reject ( new BluetoothError ( BluetoothCommon . msg_no_peripheral , { arguments : args } ) ) ;
1754+ throw new BluetoothError ( BluetoothCommon . msg_no_peripheral , { arguments : args } ) ;
17761755 }
17771756
17781757 if ( peripheral . state !== CBPeripheralState . Connected ) {
1779- const error = new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
1758+ throw new BluetoothError ( BluetoothCommon . msg_peripheral_not_connected , {
17801759 arguments : args
17811760 } ) ;
1782- return Promise . reject ( error ) ;
17831761 }
1762+ // with that all being checked, let's return a wrapper object containing all the stuff we found here
1763+ return peripheral ;
1764+ }
1765+ @bluetoothEnabled
1766+ private async _getWrapper ( args , property : CBCharacteristicProperties ) {
1767+ // prepareArgs should be called before hand
1768+ if ( ! args . peripheralUUID ) {
1769+ throw new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1770+ type : BluetoothCommon . peripheralUUIDKey ,
1771+ arguments : args
1772+ } ) ;
1773+ }
1774+ if ( ! args . serviceUUID ) {
1775+ throw new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1776+ type : BluetoothCommon . serviceUUIDKey ,
1777+ arguments : args
1778+ } ) ;
1779+ }
1780+ if ( ! args . characteristicUUID ) {
1781+ throw new BluetoothError ( BluetoothCommon . msg_missing_parameter , {
1782+ type : BluetoothCommon . characteristicUUIDKey ,
1783+ arguments : args
1784+ } ) ;
1785+ }
1786+ const peripheral = await this . _getDeviceWrapper ( args ) ;
17841787
17851788 const serviceUUID = CBUUID . UUIDWithString ( args . serviceUUID ) ;
17861789 const service = this . _findService ( serviceUUID , peripheral ) ;
17871790 if ( ! service ) {
1788- return Promise . reject (
1789- new BluetoothError ( BluetoothCommon . msg_no_service , {
1790- arguments : args
1791- } )
1792- ) ;
1791+ throw new BluetoothError ( BluetoothCommon . msg_no_service , {
1792+ arguments : args
1793+ } ) ;
17931794 }
17941795
17951796 const characteristicUUID = CBUUID . UUIDWithString ( args . characteristicUUID ) ;
@@ -1808,18 +1809,16 @@ export class Bluetooth extends BluetoothCommon {
18081809 }
18091810
18101811 if ( ! characteristic ) {
1811- return Promise . reject (
1812- new BluetoothError ( BluetoothCommon . msg_no_characteristic , {
1813- arguments : args
1814- } )
1815- ) ;
1812+ throw new BluetoothError ( BluetoothCommon . msg_no_characteristic , {
1813+ arguments : args
1814+ } ) ;
18161815 }
18171816
18181817 // with that all being checked, let's return a wrapper object containing all the stuff we found here
1819- return Promise . resolve ( {
1818+ return {
18201819 peripheral,
18211820 service,
18221821 characteristic
1823- } ) ;
1822+ } ;
18241823 }
18251824}
0 commit comments