From 29b86bd8cfa2df1fe43b8f18ef97e4f8b6ee7edf Mon Sep 17 00:00:00 2001 From: Karl Obermiller Date: Fri, 30 Mar 2018 15:53:13 -0400 Subject: [PATCH 1/3] Added promisify function to clean things up --- bt.js | 21 ++++++++++++++++++--- package.json | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/bt.js b/bt.js index bd3fff5..61c8d74 100644 --- a/bt.js +++ b/bt.js @@ -12,9 +12,10 @@ const RadioState = Radios.RadioState; var Bluetooth = {}; Bluetooth.isSupported = isSupported = async function () { - let radios = await new Promise((res, rej) => { - Radio.getRadiosAsync(_promiseWrapperCB(res, rej)); - }); + let radios = await _promisify(Radio.getRadiosAsync)(); + // let radios = await new Promise((res, rej) => { + // Radio.getRadiosAsync(_promiseWrapperCB(res, rej)); + // }); radios = radios.first(); while (radios.hasCurrent) { let radio = radios.current; @@ -178,6 +179,20 @@ function _promiseWrapperCB(res, rej) { } } +function _promisify(func) { + return function(...args) { + return new Promise((res, rej) => { + func(...args, (err, data) => { + if (err) { + rej(err); + } else { + res(data); + } + }); + }); + } +} + Bluetooth.listUnpaired = listUnpaired = async function() { let unpaired = []; // let results = deasync(DevInfo.findAllAsync)(unpairedQuery); diff --git a/package.json b/package.json index edf3f3e..1704167 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "win-bt", - "version": "0.1.2", + "version": "0.1.3", "description": "", "main": "bt.js", "bundleDependencies": [ From cdb3521c42baf8a6b17fe4aeeeaead9611d8921e Mon Sep 17 00:00:00 2001 From: Karl Obermiller Date: Fri, 30 Mar 2018 15:53:13 -0400 Subject: [PATCH 2/3] Added promisify function to clean things up --- bt.js | 25 ++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/bt.js b/bt.js index 2f5152d..0363e8a 100644 --- a/bt.js +++ b/bt.js @@ -17,9 +17,10 @@ var Bluetooth = {}; * @returns {boolean} whether or not bluetooth is supported. */ Bluetooth.isSupported = isSupported = async function () { - let radios = await new Promise((res, rej) => { - Radio.getRadiosAsync(_promiseWrapperCB(res, rej)); - }); + let radios = await _promisify(Radio.getRadiosAsync)(); + // let radios = await new Promise((res, rej) => { + // Radio.getRadiosAsync(_promiseWrapperCB(res, rej)); + // }); radios = radios.first(); while (radios.hasCurrent) { let radio = radios.current; @@ -229,6 +230,24 @@ function _promiseWrapperCB(res, rej) { } } +/** + * Turns the given function that takes a callback as the last argument + * into a function that returns a promise. + */ +function _promisify(func) { + return function(...args) { + return new Promise((res, rej) => { + func(...args, (err, data) => { + if (err) { + rej(err); + } else { + res(data); + } + }); + }); + } +} + /** * Initiates a scan for unpaired bluetooth devices that are turned on within range. * @returns {Promise} Resolves an array of bluetooth device objects that are not diff --git a/package.json b/package.json index edf3f3e..1704167 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "win-bt", - "version": "0.1.2", + "version": "0.1.3", "description": "", "main": "bt.js", "bundleDependencies": [ From c3d85d53583a3b7e7a2f55d623a72368a27a3135 Mon Sep 17 00:00:00 2001 From: Karl Obermiller Date: Fri, 30 Mar 2018 23:54:10 -0400 Subject: [PATCH 3/3] Migrated everything to promisify --- bt.js | 44 +++++++++----------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/bt.js b/bt.js index 0363e8a..c6a955f 100644 --- a/bt.js +++ b/bt.js @@ -18,9 +18,6 @@ var Bluetooth = {}; */ Bluetooth.isSupported = isSupported = async function () { let radios = await _promisify(Radio.getRadiosAsync)(); - // let radios = await new Promise((res, rej) => { - // Radio.getRadiosAsync(_promiseWrapperCB(res, rej)); - // }); radios = radios.first(); while (radios.hasCurrent) { let radio = radios.current; @@ -38,9 +35,7 @@ Bluetooth.isSupported = isSupported = async function () { * @returns {boolean} whether or not bluetooth is enabled. */ Bluetooth.isEnabled = isEnabled = async function () { - let radios = await new Promise((res, rej) => { - Radio.getRadiosAsync(_promiseWrapperCB(res, rej)); - }); + let radios = await _promisify(Radio.getRadiosAsync)(); radios = radios.first(); while (radios.hasCurrent) { let radio = radios.current; @@ -151,10 +146,7 @@ function _BTAddressToInt(address) { async function _parseBTDevice(devInfo) { let btd = devInfo; if (devInfo instanceof DevInfo) { - // btd = deasync(BTDevice.fromIdAsync)(devInfo.id); - btd = await new Promise((res, rej) => { - BTDevice.fromIdAsync(devInfo.id, _promiseWrapperCB(res, rej)); - }); + btd = await _promisify(BTDevice.fromIdAsync)(devInfo.id); } else if (devInfo instanceof BTDevice) { devInfo = btd.deviceInformation; } else { @@ -255,10 +247,7 @@ function _promisify(func) { */ Bluetooth.listUnpaired = listUnpaired = async function() { let unpaired = []; - // let results = deasync(DevInfo.findAllAsync)(unpairedQuery); - let results = await new Promise((res, rej) => { - DevInfo.findAllAsync(unpairedQuery, _promiseWrapperCB(res, rej)); - }); + let results = await _promisify(DevInfo.findAllAsync)(unpairedQuery); results = results.first(); while(results.hasCurrent) { let device = await _parseBTDevice(results.current); @@ -275,10 +264,7 @@ Bluetooth.listUnpaired = listUnpaired = async function() { */ Bluetooth.listPaired = listPaired = async function() { let paired = []; - // let results = deasync(DevInfo.findAllAsync)(pairedQuery); - let results = await new Promise((res, rej) => { - DevInfo.findAllAsync(pairedQuery, _promiseWrapperCB(res, rej)); - }); + let results = await _promisify(DevInfo.findAllAsync)(pairedQuery); results = results.first(); while(results.hasCurrent) { let device = await _parseBTDevice(results.current); @@ -306,9 +292,7 @@ Bluetooth.listAll = listAll = async function() { */ Bluetooth.fromAddress = fromAddress = async function(address) { address = _treatAddress(address); - let btd = await new Promise((res, rej) => { - BTDevice.fromBluetoothAddressAsync(address, _promiseWrapperCB(res, rej)); - }); + let btd = await _promisify(BTDevice.fromBluetoothAddressAsync)(address); return await _parseBTDevice(btd); } @@ -320,10 +304,7 @@ Bluetooth.fromAddress = fromAddress = async function(address) { * if the pairing fails. */ Bluetooth.pair = pair = async function(address) { - // let btd = deasync(BTDevice.fromBluetoothAddressAsync)(_treatAddress(address)).deviceInformation; - let btd = (await new Promise((res, rej) => { - BTDevice.fromBluetoothAddressAsync(_treatAddress(address), _promiseWrapperCB(res, rej)); - })).deviceInformation; + let btd = (await _promisify(BTDevice.fromBluetoothAddressAsync)(_treatAddress(address))).deviceInformation; let pairing = btd.pairing.custom; pairing.on('pairingRequested', (custom, request) => { @@ -333,9 +314,7 @@ Bluetooth.pair = pair = async function(address) { pairingKinds = pairingKinds.displayPin; // Only one that seems to work at all reliably from library. // pairingKinds = pairingKinds.confirmOnly | pairingKinds.confirmPinMatch | pairingKinds.displayPin | pairingKinds.providePin; - let result = await new Promise((res, rej) => { - pairing.pairAsync(pairingKinds, btd.pairing.protectionLevel, _promiseWrapperCB(res, rej)); - }); + let result = _promisify(pairing.pairAsync)(pairingKinds, btd.pairing.protectionLevel); let status = _parseEnumValue(result.status, DevEnum.DevicePairingResultStatus); switch(status) { @@ -358,15 +337,10 @@ Bluetooth.pair = pair = async function(address) { * if the unpairing fails. */ Bluetooth.unpair = unpair = async function(address) { - // let btd = deasync(BTDevice.fromBluetoothAddressAsync)(_treatAddress(address)).deviceInformation; - let btd = (await new Promise((res, rej) => { - BTDevice.fromBluetoothAddressAsync(_treatAddress(address), _promiseWrapperCB(res, rej)); - })).deviceInformation; + let btd = (await _promisify(BTDevice.fromBluetoothAddressAsync)(_treatAddress(address))).deviceInformation; let pairing = btd.pairing; - let result = await new Promise((res, rej) => { - pairing.unpairAsync(_promiseWrapperCB(res, rej)); - }); + let result = await _promisify(pairing.unpairAsync); let status = _parseEnumValue(result.status, DevEnum.DeviceUnpairingResultStatus); switch(status) {