• I'm using the code example from the Bluetooth LE page, modified for my device:

    var devices;
    
    NRF.findDevices(function(d) {
      if (devices.length < 1) throw new Error("Nothing found!");
      devices[0].gatt.connect().then(function(­g) {
        gatt = g;
        return gatt.getPrimaryService("0xbbc21523-008e-­fe6b-28ee-d6a44f9cb5da");
      }).then(function(service) {
        return service.getCharacteristic("0xbbc21525-00­8e-fe6b-28ee-d6a44f9cb5da");
      }).then(function(characteristic) {
        characteristic.writeValue( 0xff );
      }).then(function() {
        gatt.disconnect();
        console.log("Done!");
      });
    }, 2000);
    
    
    >
    =undefined
    Uncaught Error: Field or method "length" does not already exist, and can't create it on undefined
     at line 1 col 12
    if (devices.length < 1) throw new Error("Nothing found!");
               ^
    in function called from system
    > 
    

    I'm getting an error above that I think is a simple coding error, but I copied it directly. My goof, or documentation error?

  • 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-0­08e-fe6b-28ee-d6a44f9cb5da")).then(
      service => service.getCharacteristic("0xbbc21525-00­8e-fe6b-28ee-d6a44f9cb5da")).then(
      characteristic => characteristic.writeValue(oxff)).then(
      () => { gatt.disconnect(); console.log("Done!"); } );
    
    
  • in your first post, change:

    NRF.findDevices(function(d) {
    to
    NRF.findDevices(function(devices) {

  • 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...

  • 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 not bbc21525-008e-fe6b-28ee-d6a44f9cb5da - so you prepended a 0x 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 a Error: UUID not the right length (16 or 2 bytes) message, not a crash

  • 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 not 0xff, so that wouldn't work either. It shouldn't crash, but would throw a ReferenceError when it executed that bit

  • 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?

  • Certainly not good enough to get the ES6 syntax. I lifted it from the NRF.findDevices documentation :-}

  • 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.

  • 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.

  • 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?

  • @Gordon - The Web IDE running in Chrome gave the 'disconnected' message, so that would depend on whether it has a timeout.

    On the advertising issue: is there any way I can extend that window? I'd assumed that the timeout parameter was a scanning window, but ??

    Thanks!!

  • Ok, so yeah - if it disconnects something's gone wrong.

    Advertising doesn't have a timeout parameter? It has interval, 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.

  • 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.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Trying to get an example working with my device...

Posted by Avatar for billsalt @billsalt

Actions