noble.state is set to "poweredOff" immediately after running the program. (Node.js Noble) - node.js

as the title suggests, I made a simple program made with Node.js and Noble to scan for devices with Bluetooth LE. My end goal here is to connect to my daydream view controller and receive information from it.
My problem is that whenever I run the file the state is set to "poweredOff" even though I set it to "poweredOn". When the state is set to "poweredOff" it stops scanning so I am never able to find devices.
Here is my code:
const noble = require('noble')
noble.on('stateChange', function(state) {
console.log("[STATE] State changed to: ", state)
if (state === 'poweredOn') {
console.log("[STATE] Powered on, now scanning")
noble.startScanning();
} else {
console.log("[STATE] Powered off, stopped scanning")
noble.stopScanning();
}
})
noble.state = "poweredOn"
// we found something
noble.on("discover", function(peripheral){
console.log(peripheral)
})
Here is the output I get after running that:
[STATE] State changed to: poweredOff
[STATE] Powered off, stopped scanning
What have I tried?
I have checked that I have all the prerequisites for Noble.
I have tried to run the examples provided by Noble. (same thing happens)
And I have tried moving noble.state = "poweredOn" above and below the noble.on('stateChange') event
I also figured out the problem is not in the daydream controller because I downloaded the LightBlue app on my phone and it detects and connects to the daydream controller just fine. I get no errors or anything at all. This is very strange to me and I hope that anyone could help me.
Thanks in advance.

Related

Clean way to real, always working, auto-reconnect

I hope to open a question that is useful for the users of this shield.
Incipit: WiFi.setAutoReconnect(true); seems that not prevent 100% of disconnections.
I've tested a lot of shields (ESP12F, ESP01) and in some case i noted that the auto-reconnect does not work properly.
Fact:
I was unable to reproduce when the shield is connect to pc/debugger.
When did it happen, i try to execute a task depending on a botton press, eg:
loop(){
if (digitalRead... == HIGH) do_something()
}
And the shield... do something! So, the shield was not frozen.
I try to reset (BOTH via HW and SW) and the shield immediately reconnect.
I read some other source and this behavior is often described (es. https://randomnerdtutorials.com/solved-reconnect-esp32-to-wifi/ ). Brief: try WiFi.reconnect(), if it does not work try ESP.restart().
Then, the question:
Why does this happen? Is there a problem with the arduino libraries, or with the native expressif interface? Or is a well-known hardware problem unsolvable via SW?
If indeed so, what techniques do you use to prevent disconnection? I have set a ticker every 30 minutes, which if it sees the card disconnected for more than a certain time, it restarts it. Eg.
void checkWifi() {
if (lastPing + DELTA < millis()) ESP.restart();
}
ticker.attach(checkWifi, ...)
void loop() {
if WiFi.isConnect() {
lastPing = millis();
...
}
}
If there is nothing to do, what do you think of the restart technique? Is it risky to restart frequently, can it reduce the life of the device?
Thanks to anyone who wants to contribute or exchange impressions!

Using Flutter to connect and write to Bluetooth Devices

I'm new with Flutter and am just trying to make this work.
Am using Flutter Blue https://pub.dartlang.org/packages/flutter_blue
It connects though, just this issue when writing.
But when writing i'm receiving this message. Not sure what I am doing wrong though.
here's my code..
onPressed: () {
print("HEY write pressed");
var fff1 = new Guid("0000fff1-0000-1000-8000-00805f9b34fb");
var fffa = new Guid("0000fffa-0000-1000-8000-00805f9b34fb");
BluetoothCharacteristic characteristic = new BluetoothCharacteristic(uuid: fffa, serviceUuid: fff1, descriptors: null, properties: null);
_writeCharacteristic(characteristic);
},
PlatformException(locateCharacteristic, service could not be located on the device, null)
I've tried following this post.
Flutter Blue Read characteristic UUID
In production I would probably save the UUID as variables, but the effect should be similar..
If anyone has any guidance or tips that would be super welcome.
Your code is right but the device you want to connect to doesnt contain this service :var fff1 = new Guid("0000fff1-0000-1000-8000-00805f9b34fb");
Check which services exist in the device you want to connect to.

Firebase onDisconnect() firing multiple times

Building an app with presence following the firebase docs, is there a scenario where the on-disconnect fires when the app is still connected? We see instances where the presence node shows the app as going offline and then back online within a few seconds when we aren't losing a network connection.
We are seeing on multiple embedded devices installed in the field where presence is set to false and then almost immediately right back to true and it's occurring on all the devices within a few seconds of each other. From the testing we have done and the docs online we know that if we lose internet connection on the device it takes roughly 60 seconds before the timeout on the server fires the onDisconnect() method.
We have since added code in the presence method that allows the device if it sees the presence node be set to false while the app is actually running it will reset the presence back to true. At times when this happens we get a single write back to true and that is the end of it, other times it is like the server and client are fighting each other and the node is reset to true numerous times over the course of 50-200 milliseconds. We monitor this by pushing to another node within the device GUID each time we are forcing presence back to true. This only occurs while the module is running and after it initially establishes presence.
Here is the method that we call from our various modules that are running on the device so that we can monitor the status of each of the modules at any given time.
exports.online = function (program, currentProgram) {
var programPath = process.env.FIREBASE_DEVICES + process.env.GUID + '/status/' + program
var onlinePath = process.env.FIREBASE_DEVICES + process.env.GUID + '/statusOnlineTimes/' + program
var programRef = new firebase(programPath);
var statusRef = new firebase(process.env.FIREBASE_DEVICES + process.env.GUID + '/status/bootup');
var onlineRef = new firebase(onlinePath)
amOnline.on('value', function(snapshot) {
if (snapshot.val()) {
programRef.onDisconnect().set(false);
programRef.set(true);
programRef.on('value', function(snapshot){
if (snapshot.val() == false){
programRef.set(true);
console.log('[NOTICE] Resetting', program, 'module status back to True after Fireabase set to False')
var objectToPush = {
program: program,
time: new Date().toJSON()
}
onlineRef.push(objectToPush)
}
})
if (currentProgram != undefined) {
statusRef.onDisconnect().set('Offline')
statusRef.set(currentProgram)
}
}
});
The question we have is there ever an instance where Firebase is calling the onDisconnect() method even though it really isn't losing its status? We had instances where we would see the device go offline and then back online within 60 seconds before we added the reset code. The reset code was to combat another issue we had in the field where if the power were interrupted to the device and it did not make a clean exit, the device could reboot and and reset the presence with a new UID before the timeout for the prior instance had fired. Then once the timeout fired the device would show as offline even though it was actually online.
So we were able to stop the multiple pushes that were happening when the device reconnected by adding a programRef.off() call directly before the programRef.on(...) call. What we determined to be happening is that anytime the device went online from an offline state and the amOnline.on(...) callback fired it created a new listener.
Now we are able to handle the case where a onDisconnect() fires from a earlier program PID and overwrites the currently active program with a status of offline. This seems to solve the issue we are having with the race condition of the devices in the field able to reboot and regain connection prior to the onDisconnect() firing for the instance that was not cleanly exited.
We are still having an issue where all of the devices are going off and then back online at approximately the same time (within 1-3 seconds of each other). Are there any times where Firebase resets the ./info/connected node? Because we are monitoring presence and actually logging on and off events maybe we are just catching an event that most people don't see? Or is there something that we are doing wrong?

Intel XDK QRCode Scanning Issue

So am trying to use the barcode scanner in XDK and it WORKS except when am scanning a QRCode that contains more than one line of plain text.
For example:
If this is in the QRCode:
hey you
It gets scanned without any issues
BUT if this is in the QRCode:
hey
you
It fails. The app reacts as if nothing was scanned. Nothing is outputted.
My Code:
function scan(){
document.addEventListener("intel.xdk.device.barcode.scan",function(evt){
intel.xdk.notification.beep(1);
if (evt.success === true) {
//successful scan
//console.log(evt.codedata);
document.getElementById("data").innerHTML = String(evt.codedata);
}else {
//failed scan
document.getElementById("data").innerHTML = "Scan Operation Failed";
}
},false);
intel.xdk.device.scanBarcode();
}
I really hope you guys help me. If the XDK's QRCode scanner is just not capable of scanning multi-lines of plain text i will have to stop developing with XDK, which is a bummer.
Thanks in advance.
Just following up. This issue has now been fixed in our build system and the latest version of App Preview for Android, Windows Phone, and iOS. Please try your code again and let us know if you have any issues.

where to put a while loop in a system tray application so that loop starts with the app

I have created a System Tray Application using Windows Forms Template (Visual C++) in Visual Studio 2008. I have used ContextMenuStrip and NotifyIcon. It's a managed code as I have used the form and Drag/Drop.
I want as soon as this System Tray Application starts, it starts polling for any new USB devices (from a specific vendor) connected.
The logic is ready except I don't know "Where to put this while(1) loop?"
It works fine in a console app that I made but now we want it to be integrated to the system tray app.
Here is the code snippet:
int numDevices, n = 0;
while(1)
{
Sleep(5000);
numDevices = usb_find_devices();
if(connectedDevices > numDevices)
{
enumDevices();
connectedDevices++;
}
}
It would really be appreciable if anyone could suggest me some pointers on how to proceed.
Thank you Hans! I added a new "Component Class" with WM_DEVICECHANGE and it is working fine.
Just in case anyone needs this info:
If a function needs to be called as soon as the Windows Forms App starts (Systray app in my case), the respective function can be called after the call to "InitializeComponent()" function. Though it is clearly mentioned "TODO: Add the constructor code here", still a beginner (like me) has inhibitions regarding "Where to put this Function Call??" Hope this helps somebody..

Resources