Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
private init(completeFn: () => any): void {
if (this.initialised) return completeFn();
noble.on("discover", deviceInfo => {
if (this.discoverFn) this.discoverFn(deviceInfo);
});
this.initialised = true;
completeFn();
}
constructor() {
super();
this.enabled = this.state;
noble.on("stateChange", () => {
if (this.enabled !== this.state) {
this.enabled = this.state;
this.emit(NobleAdapter.EVENT_ENABLED, this.enabled);
}
});
}
async init() {
const self = this;
noble.on('stateChange', async (state) => {
logger('ble state changed: ' + state);
if (state === 'poweredOn') {
await this.scan();
}
});
noble.on('discover', (peripheral) => {
logger('found ' + peripheral.advertisement.localName + ' with addr ' + peripheral.address);
if (peripheral.advertisement.localName === 'P mesh') {
self.peripherals.push(peripheral);
}
});
noble.on('disconnect', async () => {
if (self.peripherals.length) {
logger('peripherals already scanned.');
this.connectedIndex = 0;
await self.connect();
}
});
}
const manualIdsWereSpecified = idsToConnectTo.length !== 0;
if (!manualIdsWereSpecified) {
let message = 'No device names supplied, ';
if (argv.expectedDevices) {
message += 'will stop scanning after ' + argv.expectedDevices + ' device(s) connect';
} else {
message += 'will stop scanning after ' + argv.discoveryTimeout + ' seconds';
}
log(message);
} else {
argv.expectedDevices = idsToConnectTo.length;
}
let devices = {};
noble.on('stateChange', (state) => {
if(state === 'poweredOn') {
noble.startScanning();
if (!manualIdsWereSpecified && !argv.expectedDevices) {
//discover until timeout
setTimeout(() => {
log('stopping scan after timeout');
noble.stopScanning();
if (Object.keys(devices).length === 0) {
log('No devices found, exiting');
process.exit(-2);
}
Object.values(devices).forEach((device) => {device.connect();});
}, 1000 * argv.discoveryTimeout);
}
async started() {
this.updateRequiredModes();
noble.on('stateChange', this.handleScanStateChange);
bleno.on('stateChange', this.handleAdvertiseStateChange);
noble.on('discover', this.handleDiscovery);
}
};
async started() {
this.updateRequiredModes();
noble.on('stateChange', this.handleScanStateChange);
bleno.on('stateChange', this.handleAdvertiseStateChange);
noble.on('discover', this.handleDiscovery);
}
};
function shouldConnect(peripheral) {
if (peripheral.advertisement !== undefined && peripheral.advertisement.localName !== undefined) {
let localName = peripheral.advertisement.localName;
if (localName == 'S' || localName.startsWith('RISE') || idsToConnectTo.indexOf(localName) !== -1) {
return true;
}
}
if (peripheral.id !== undefined && idsToConnectTo.indexOf(peripheral.id.toLowerCase()) !== -1) {
return true;
}
return false;
}
noble.on('discover', peripheral => {
if (shouldConnect(peripheral)) {
let address = peripheral.address !== undefined ? peripheral.address.replace(/:/g, '') : undefined;
let localName = peripheral.advertisement !== undefined ? peripheral.advertisement.localName : undefined;
let id;
if (localName !== undefined && localName.startsWith('RISE')) {
id = localName;
} else {
id = address;
}
if (idsToIgnore.indexOf(id) !== -1) {
debugLog('Found %s but will not connect as it is in the ignored ID list', id);
return;
}
if (idsToConnectTo.length !== 0 && (idsToConnectTo.indexOf(id) === -1 && idsToConnectTo.indexOf(address) === -1)) {