sandeepmistry / node-bleacon

A Node.js library for creating, discovering, and configuring iBeacons
MIT License
498 stars 88 forks source link

startScanning() only works in test.js #72

Closed ethicaldesign closed 7 years ago

ethicaldesign commented 7 years ago

I have my doorbeacon.js file in the same directory as my cloned node-bleacon. When I run test.js, it finds my beacons. When I copy the exact contents of tets.js into my node js file, it skips right over it. I believe it is actually running the service because it renders BLE useless unless I reboot. Not even HCI resets or up/down commands fixes it. I ran a debug and here is what I have (when it gets to "onoff mode" that's where my js code continues):

`login as: pi pi@192.168.1.2's password:

The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sun Mar 19 02:46:16 2017

SSH is enabled and the default password for the 'pi' user has not been changed. This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

pi@raspberrypi:~ $ cd /home/pi/DoorBeacon/node-bleacon/ pi@raspberrypi:~/DoorBeacon/node-bleacon $ sudo DEBUG=* node doorlock.js hci setting filter to: 1600000020c10000000000400000 +0ms hci set event mask - writing: 01010c08fffffbff07f8bf3d +41ms hci set le event mask - writing: 010120081f00000000000000 +10ms hci read local version - writing: 01011000 +4ms hci write LE host supported - writing: 016d0c020100 +3ms hci read LE host supported - writing: 016c0c00 +3ms hci read bd addr - writing: 01091000 +4ms gatt handles = [ gatt {}, gatt { gatt "type": "service", gatt "uuid": "1800", gatt "startHandle": 1, gatt "endHandle": 5 gatt }, gatt { gatt "type": "characteristic", gatt "uuid": "2a00", gatt "properties": 2, gatt "secure": 0, gatt "startHandle": 2, gatt "valueHandle": 3 gatt }, gatt { gatt "type": "characteristicValue", gatt "handle": 3, gatt "value": "Buffer('7261737062657272797069', 'hex')" gatt }, gatt { gatt "type": "characteristic", gatt "uuid": "2a01", gatt "properties": 2, gatt "secure": 0, gatt "startHandle": 4, gatt "valueHandle": 5 gatt }, gatt { gatt "type": "characteristicValue", gatt "handle": 5, gatt "value": "Buffer('8000', 'hex')" gatt }, gatt { gatt "type": "service", gatt "uuid": "1801", gatt "startHandle": 6, gatt "endHandle": 9 gatt }, gatt { gatt "type": "characteristic", gatt "uuid": "2a05", gatt "properties": 32, gatt "secure": 0, gatt "startHandle": 7, gatt "valueHandle": 8 gatt }, gatt { gatt "type": "characteristicValue", gatt "handle": 8, gatt "value": "Buffer('00000000', 'hex')" gatt }, gatt { gatt "type": "descriptor", gatt "handle": 9, gatt "uuid": "2902", gatt "properties": 14, gatt "secure": 0, gatt "value": "Buffer('0000', 'hex')" gatt } gatt ] +0ms bleno platform linux +48ms hci setting filter to: 1400000020c10000000000400000 +13ms hci set event mask - writing: 01010c08fffffbff07f8bf3d +4ms hci set le event mask - writing: 010120081f00000000000000 +4ms hci read local version - writing: 01011000 +3ms hci write LE host supported - writing: 016d0c020100 +4ms hci read LE host supported - writing: 016c0c00 +4ms hci read bd addr - writing: 01091000 +4ms estimote-sticker NOBLE_REPORT_ALL_HCI_EVENTS env variable has to be set to 1 + 322ms bleacon startAdvertising: uuid = e2c56db5dffb48d2b060d0f5a71096e0, major = 0, minor = 0, measuredPower = -59 +9ms scanning... bleacon startScanning: uuid = undefined, major = undefined, minor = undefined +38ms OnOff mode Connecting to: blynk-cloud.com 8441 locking door hci onSocketData: 040e0401010c00 +2s hci event type = 4 +5ms hci sub event type = 14 +2ms hci cmd = 3073 +7ms hci status = 0 +3ms hci result = +2ms hci onSocketData: 040e0401010c00 +784ms hci event type = 4 +2ms hci sub event type = 14 +2ms hci cmd = 3073 +3ms hci status = 0 +2ms hci result = +3ms hci onSocketData: 040e0401012000 +24ms hci event type = 4 +2ms hci sub event type = 14 +2ms hci cmd = 8193 +2ms hci status = 0 +3ms hci result = +2ms hci onSocketData: 040e0401012000 +20ms hci event type = 4 +2ms hci sub event type = 14 +2ms hci cmd = 8193 +2ms hci status = 0 +3ms hci result = +2ms hci onSocketData: 040e0c0101100007b600070f000922 +17ms hci event type = 4 +2ms hci sub event type = 14 +2ms hci cmd = 4097 +2ms hci status = 0 +2ms hci result = 07b600070f000922 +2ms hci set scan enabled - writing: 010c20020001 +5ms hci set scan parameters - writing: 010b200701100010000000 +3ms hci onSocketData: 040e0c0101100007b600070f000922 +24ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 4097 +2ms hci status = 0 +3ms hci result = 07b600070f000922 +2ms hci set advertise enable - writing: 010a200100 +4ms hci set advertisement parameters - writing: 0106200fa000a000000000000000000000 0700 +5ms hci onSocketData: 040e04016d0c00 +26ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 3181 +1ms hci status = 0 +2ms hci result = +2ms hci onSocketData: 040e04016d0c00 +14ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 3181 +2ms hci status = 0 +2ms hci result = +2ms hci onSocketData: 040e06016c0c000100 +13ms hci event type = 4 +2ms hci sub event type = 14 +2ms hci cmd = 3180 +2ms hci status = 0 +2ms hci result = 0100 +2ms hci le = 1 +2ms hci simul = 0 +2ms hci onSocketData: 040e06016c0c000100 +19ms hci event type = 4 +1ms hci sub event type = 14 +35ms hci cmd = 3180 +2ms hci status = 0 +2ms hci result = 0100 +2ms hci le = 1 +2ms hci simul = 0 +2ms hci onSocketData: 040e0a01091000cbdbddd076e0 +52ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 4105 +2ms hci status = 0 +1ms hci result = cbdbddd076e0 +2ms hci address = e0:76:d0:dd:db:cb +7ms noble addressChange e0:76:d0:dd:db:cb +1ms hci onSocketData: 040e0a01091000cbdbddd076e0 +23ms hci event type = 4 +2ms hci sub event type = 14 +1ms hci cmd = 4105 +2ms hci status = 0 +2ms hci result = cbdbddd076e0 +2ms hci address = e0:76:d0:dd:db:cb +3ms bleno addressChange e0:76:d0:dd:db:cb +2ms hci onSocketData: 01010c08fffffbff07f8bf3d +20ms hci event type = 1 +2ms hci cmd = 3073 +2ms hci data len = 8 +1ms hci onSocketData: 040e0401010c00 +3ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 3073 +1ms hci status = 0 +3ms hci result = +2ms hci onSocketData: 010120081f00000000000000 +4ms hci event type = 1 +1ms hci cmd = 8193 +2ms hci data len = 8 +2ms hci onSocketData: 040e0401012000 +3ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 8193 +2ms hci status = 0 +1ms hci result = +2ms hci onSocketData: 01011000 +3ms hci event type = 1 +1ms hci cmd = 4097 +2ms hci data len = 0 +2ms hci onSocketData: 040e0c0101100007b600070f000922 +3ms hci event type = 4 +1ms hci sub event type = 14 +1ms hci cmd = 4097 +2ms hci status = 0 +2ms hci result = 07b600070f000922 +2ms hci set scan enabled - writing: 010c20020001 +3ms hci set scan parameters - writing: 010b200701100010000000 +2ms hci onSocketData: 016d0c020100 +3ms hci event type = 1 +2ms hci cmd = 3181 +2ms hci data len = 2 +1ms hci onSocketData: 040e04016d0c00 +3ms hci event type = 4 +1ms hci sub event type = 14 +2ms hci cmd = 3181 +2ms hci status = 0 +1ms hci result = +2ms hci onSocketData: 016c0c00 +3ms hci event type = 1 +2ms hci cmd = 3180 +1ms hci data len = 0 +2ms hci onSocketData: 040e06016c0c000100 +2ms hci event type = 4 +2ms hci sub event type = 14 +1ms hci cmd = 3180 +2ms hci status = 0 +2ms hci result = 0100 +2ms hci le = 1 +2ms hci simul = 0 +1ms hci onSocketData: 01091000 +3ms hci event type = 1 +2ms hci cmd = 4105 +1ms hci data len = 0 +1ms hci onSocketData: 040e0a01091000cbdbddd076e0 +2ms hci event type = 4 +2ms hci sub event type = 14 +1ms hci cmd = 4105 +1ms hci status = 0 +1ms hci result = cbdbddd076e0 +2ms hci address = e0:76:d0:dd:db:cb +2ms noble addressChange e0:76:d0:dd:db:cb +1ms hci onSocketData: 010a200100 +4s hci event type = 1 +2ms hci cmd = 8202 +2ms hci data len = 1 +3ms SSL authorization... Connected Authorized Blynk ready. hci onSocketData: 0106200fa000a0000000000000000000000700 +2s hci event type = 1 +1ms hci cmd = 8198 +2ms hci data len = 15 +3ms ^C2017-03-19 02:50:55 sigHandler: Unhandled signal 2, terminating

pi@raspberrypi:~/DoorBeacon/node-bleacon $ `

ethicaldesign commented 7 years ago

`var motorPin = 18; var buttonPin = 4; var ledPin = 17;

var blynkToken = '191d2e5c8f754fad9af08a3b9cc81eaa';

var Bleacon = require('./index');

Bleacon.startAdvertising('e2c56db5dffb48d2b060d0f5a71096e0', 0, 0, -59);

Bleacon.on('discover', function(bleacon) { console.log('bleacon found: ' + JSON.stringify(bleacon)); });

console.log("scanning..."); Bleacon.startScanning(/'e2c56db5dffb48d2b060d0f5a71096e0', 0, 0/);

// Start code //

var locked = true;

//Setup servo ....more code here `

ethicaldesign commented 7 years ago

I have been able to get it to work in my node js file. Turns out the process runs and then enters another loop where it waits for my input and that was blocking it from reentering the bleacon.on('discover'...) event. So now my question is more of a general coding question... How do I keep this continuous while at the same time running another process?

sandeepmistry commented 7 years ago

@ethicaldesign you should make your other code non-blocking, so it doesn't block the Node.js event loop and block bleacon/noble.