'Noble BLE in Node.js - sometimes works, sometimes doesn't
I have the following code I'm trying to run and I'm getting different results every time I run it. Sometimes I just get "Powered Off". Sometimes I get that plus "Powered on". Sometimes I get all of the above plus "Scanning started". And sometimes it runs all the way thru and I get a bunch of "still searching..." and it eventually hits the BLE device and returns the advertisement.
I have no idea why this is working sometimes and not others.
var async = require('async');
var noble = require('../index');
var peripheralIdOrAddress = "8cf681a590f3";
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning();
console.log("Powered On")
} else {
noble.stopScanning();
console.log("Powered Off")
}
});
noble.on('scanStart', ()=>console.log("Scanning started"))
noble.on('scanStop', ()=>console.log("Scanning stopped"))
noble.on('discover', function(peripheral) {
console.log("Discovered something!");
if (peripheral.id === peripheralIdOrAddress || peripheral.address === peripheralIdOrAddress)
{
noble.stopScanning();
console.log('peripheral with ID ' + peripheral.id + ' found');
var advertisement = peripheral.advertisement;
console.log(JSON.stringify(advertisement));
peripheral.on('disconnect', function() {
console.log("Peripheral disconnected");
process.exit(0);
});
peripheral.on('connect', ()=>console.log("Peripheral connected"));
peripheral.connect(function(error) {
peripheral.discoverServices("a0521000-2a63-479e-9d0a-09dfa7c8fd98",()=>console.log("Service discovered"));
});
}
else{console.log("Not the one. still searching...")}
});
Solution 1:[1]
Crazy addition to this question. I tried a lot of things with turning stuff off and unplugging things.
In the end I found out it was the power port. If I'm plugged into wall power, I get about 1 out of ever 10 runs that works. If I unplug wall power and go off battery, it works every time.
So bizarre.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris Fawcett |
