Skip to content

Commit

Permalink
Merge branch 'enhance/promisify'
Browse files Browse the repository at this point in the history
  • Loading branch information
obermillerk committed Apr 6, 2018
2 parents 227ead7 + 7374d20 commit ee4b56c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
63 changes: 28 additions & 35 deletions bt.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ 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)();
radios = radios.first();
while (radios.hasCurrent) {
let radio = radios.current;
Expand All @@ -37,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;
Expand Down Expand Up @@ -150,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 {
Expand Down Expand Up @@ -229,17 +222,32 @@ 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
* currently paired to this device.
*/
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);
Expand All @@ -256,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);
Expand Down Expand Up @@ -287,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);
}
Expand All @@ -301,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) => {
Expand All @@ -314,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) {
Expand All @@ -339,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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "win-bt",
"version": "0.1.2",
"version": "0.1.3",
"description": "",
"main": "bt.js",
"bundleDependencies": [
Expand Down

0 comments on commit ee4b56c

Please sign in to comment.