-
• #2
So, then I tried the compact example shown below. There is a pause, then the WEB IDE shows "Disconnected". Note: I'm using 1v92.21.
var gatt; NRF.requestDevice({ timeout:2000, filters: [{ services: '0xbbc21523-008e-fe6b-28ee-d6a44f9cb5da' }]}).then( device => device.gatt.connect()).then( g => (gatt=g).getPrimaryService("0xbbc21523-008e-fe6b-28ee-d6a44f9cb5da")).then( service => service.getCharacteristic("0xbbc21525-008e-fe6b-28ee-d6a44f9cb5da")).then( characteristic => characteristic.writeValue(oxff)).then( () => { gatt.disconnect(); console.log("Done!"); } );
-
• #3
in your first post, change:
NRF.findDevices(function(d) {
to
NRF.findDevices(function(devices) {
-
• #4
Thanks @Wilberforce! The code now works (thanks for helping this NOOB), but now I'm into the second issue: it hangs and disconnects from the IDE...
-
• #5
I'll fix that example code...
Are you using up to date firmware? You can check
process.version
and see if it's 1v92 or not. That may help with disconnect?The one thing I notice you're doing differently is your characteristic is
0xbbc21525-008e-fe6b-28ee-d6a44f9cb5da
and notbbc21525-008e-fe6b-28ee-d6a44f9cb5da
- so you prepended a0x
to it.That shouldn't cause a crash (I'll check) but it's unlikely to work :)
edit: just tested and
"0x...."
will give you aError: UUID not the right length (16 or 2 bytes)
message, not a crash -
• #6
Also, where did you get the second bit of code from? Did you write it yourself in ES6 syntax?
I just noticed you've written
oxff
an not0xff
, so that wouldn't work either. It shouldn't crash, but would throw aReferenceError
when it executed that bit -
• #7
Thanks @Gordon. It appears that the 0x prefix was causing the crash. I'm using 1v92.21.
Now I'm on to another issue; my device doesn't advertise services since it is a connectable iBeacon, so I'm going to have to find it using the beacon info, connect and then check for services (I believe), right?
-
• #8
Certainly not good enough to get the ES6 syntax. I lifted it from the NRF.findDevices documentation :-}
-
• #9
And it was a crash caused by
oxff
that left the Puck unconnectable? Seems really strange!If your beacon named? You can use http://www.espruino.com/Reference#l_NRF_requestDevice to connect to a device with a certain name or start of name (namePrefix).
Otherwise you're better off just using
NRF.connect
and putting in the address manually I guess. -
• #10
Thanks @Gordon -- I don't think the crash was the oxff issue, it seems to have been related to the 0x prefix on the UUID's. I've actually now had the code work -- once only. It must have been in some sort of state from my trying other things, but I haven't repeated it. I'll spend some more time early next week and try to track it down better, as well as try other methods to reliably connect. The device is a connectable beacon, transmitting every 1.35 seconds, so I imagine I'm just not synching up. I'm trying extending the scan time, but so far it isn't working reliably.
-
• #11
You'll find that you won't be able to reliably connect at anything over about 300ms advertising interval (it depends on your connecting computer) - so it's entirely likely Puck.js was working just fine, but the advertising interval meant you couldn't connect and assumed it had crashed?
-
• #13
Ok, so yeah - if it disconnects something's gone wrong.
Advertising doesn't have a
timeout
parameter? It hasinterval
, which is the time between advertising packets being sent. Most devices only listen for 1 second by default, and it alternates advertising over 3 different frequencies - so if you have a particularly dumb central then it'll only listen on one frequency for 1 second - hence you need <300ms interval to be connectable reliably. -
• #14
I've confused you @Gordon, sorry. Here's the scenario: I have my own peripheral device (eNote). It is a connectable beacon, advertising at the iBeacon maximum rate. I'm trying to scan and connect to it. I haven't yet tried it, but I believe that I can use the scan response BluetoothDevice "data" field to match its UUID and could then could connect to its "id", but I was trying scanning for its services first. I know this is unusual (connectable beacon), but there are certainly other devices like it on the market.
I'm using the code example from the Bluetooth LE page, modified for my device:
I'm getting an error above that I think is a simple coding error, but I copied it directly. My goof, or documentation error?