Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.init(() => {
this.deviceHandles = {};
function stateCB() {
if (this.state === true) {
// Noble doesn't correctly match short and canonical UUIDs on Linux, so we need to check ourselves
// Continually scan to pick up all advertised UUIDs
noble.startScanning([], true, this.checkForError(errorFn, completeFn));
} else {
errorFn("adapter not enabled");
}
}
if (noble.state === "unknown" || noble.state === "poweredOff") {
// tslint:disable-next-line:no-string-literal
noble["once"]("stateChange", stateCB.bind(this));
} else {
stateCB.call(this);
}
});
}
peripheral.once('disconnect', () => {
console.log('disconnected');
// Clean noble's peripheral cache
noble._peripherals = {};
connectionListener(false);
scanButton();
});
}
this.init(() => {
this.deviceHandles = {};
function stateCB() {
if (this.state === true) {
// Noble doesn't correctly match short and canonical UUIDs on Linux, so we need to check ourselves
// Continually scan to pick up all advertised UUIDs
noble.startScanning([], true, this.checkForError(errorFn, completeFn));
} else {
errorFn("adapter not enabled");
}
}
if (noble.state === "unknown" || noble.state === "poweredOff") {
// tslint:disable-next-line:no-string-literal
noble["once"]("stateChange", stateCB.bind(this));
} else {
stateCB.call(this);
}
});
}
async disconnect() {
logger('disconnecting from Plejd');
if (this.isConnected) {
clearInterval(this.pingRef);
if (this.peripheral) {
try {
// disconnect
await this.peripheral.disconnect();
// we need to reset the ble adapter too
noble._bindings._hci.reset();
// wait 200 ms for reset command to take effect :)
sleep.msleep(200);
// now we're ready to connect again
}
catch (error) {
console.log('error: unable to disconnect from Plejd: ' + error);
return Promise.resolve(false);
}
this.isConnected = false;
logger('disconnected from Plejd');
return Promise.resolve(true);
}
function stateCB() {
if (this.state === true) {
// Noble doesn't correctly match short and canonical UUIDs on Linux, so we need to check ourselves
// Continually scan to pick up all advertised UUIDs
noble.startScanning([], true, this.checkForError(errorFn, completeFn));
} else {
errorFn("adapter not enabled");
}
}
export const availability: Observable = Observable.create(observer => {
const onAvailabilityChanged = e => {
observer.next(e === POWERED_ON);
};
noble.addListener("stateChanged", onAvailabilityChanged); // events lib?
observer.next(noble.state === POWERED_ON);
return () => {
noble.removeListener("stateChanged", onAvailabilityChanged);
};
});
export const availability: Observable = Observable.create(observer => {
const onAvailabilityChanged = e => {
observer.next(e === POWERED_ON);
};
noble.addListener("stateChanged", onAvailabilityChanged); // events lib?
observer.next(noble.state === POWERED_ON);
return () => {
noble.removeListener("stateChanged", onAvailabilityChanged);
};
});
public getEnabled(completeFn: (enabled: boolean) => void) {
function stateCB() {
completeFn(this.state);
}
if (noble.state === "unknown" || noble.state === "poweredOff") {
// tslint:disable-next-line:no-string-literal
noble["once"]("stateChange", stateCB.bind(this));
} else {
stateCB.call(this);
}
}
public getEnabled(completeFn: (enabled: boolean) => void) {
function stateCB() {
completeFn(this.state);
}
if (noble.state === "unknown" || noble.state === "poweredOff") {
// tslint:disable-next-line:no-string-literal
noble["once"]("stateChange", stateCB.bind(this));
} else {
stateCB.call(this);
}
}
function init(clickListener, connectionListener = () => 0) {
noble.on('stateChange', (state) => {
if (state === 'poweredOn') {
scanButton();
} else {
noble.stopScanning();
}
});
noble.on('discover', (peripheral) => {
if (peripheral.advertisement.localName === buttonName) {
noble.stopScanning();
peripheral.connect((error) => {
if (error) {
connectionListener(false);
scanButton();
console.error(error);
return;