7 Replies Latest reply on May 29, 2015 5:06 PM by CMata_Intel

    noble

    a_Monteiro

      Hi,

       

      I am having trouble using noble from node.js to connect to a BLE device to I can receive data from it.

       

      Has anyone had some experience with noble ?

       

      I would greatly appreciate the help.

       

      Thank you !

        • 1. Re: noble
          rosek

          Yes, noble works well for us.

           

          Regards,

          Krzysztof

          • 2. Re: noble
            a_Monteiro

            ok, thats great to hear. I have ran into several issues. I used the example of pizza to connect to a BLE device.

            1. I have ran the following code to look for a BLE device which is powered on : (issues written after code!)

            console.log('start');
            var noble = require('../index');


            var pizzaServiceUuid =  'fe1a56c98efa';
            //var pizzaServiceUuid = 'FE:1A:56:C9:8E:FA';
            //var pizzaServiceUuid = '0BA470A79B9D65E1B6C0EA132B77F93A';
            //---------------------------------------------------------------------
            var pizzaToppingsCharacteristicUuid = '0BA470A79B9D65E1B6C0EA132B772A37';
            var pizzaBakeCharacteristicUuid = '0BA470A79B9D65E1B6C0EA132B772A38';
            var pizzaCrustCharacteristicUuid = '0BA470A79B9D65E1B6C0EA132B772A39';

            noble.on('stateChange', function(state) {

              console.log('power');
              if (state === 'poweredOn') {

              // Once the BLE radio has been powered on, it is possible
              // to begin scanning for services. Pass an empty array to
              // scan for all services (uses more time and power)
              //console.log('scanning... for : ', pizzaServiceUuid);
              console.log('scanning...');
              noble.startScanning([pizzaServiceUuid], false);
              //noble.start/tScanning();
              /*for (var num = 0 ; num < 5000; num = num + 1000){
              setTimeout(function() {
              console.log('Seconds elapsed', num/1000);
              }, num);
              }*/
              }

              else {

              noble.stopScanning();
              console.log('halt Scanning');
              }

            })

            // dont know why yet
            //*********************************************************************
            var pizzaService = null;
            var pizzaCrustCharacteristic = null;
            var pizzaToppingsCharacteristic = null;
            var pizzaBakeCharacteristic = null;

            noble.on('discover', function(peripheral) {

              //
              // The advertisment data contains a name, power level (if available),
              // certain advertised service uuids, as well as manufacturer data,
              // which could be formatted as an iBeacon.
              //
              console.log('found peripheral:', peripheral.advertisement);
              //
              // Once the peripheral has been discovered, then connect to it.
              // It can also be constructed if the uuid is already known.
              ///
              peripheral.connect(function(err){

              console.log('Right before characteristics -> Lets see!');
              //
              // Once the peripheral has been connected, then discover the
              // services and characteristics of interest.
              //
              peripheral.discoverServices([pizzaServiceUuid], function(err, services) {

              services.forEach(function(service) {

              //
              // This must be the service we were looking for.
              //
              console.log('found service:', service);

              //
              // So, discover its characteristics.
              //
              console.log('Right before characteristics -> Lets see!');
              service.discoverCharacteristics([], function(err, characteristics) {

              console.log('Right before characteristics -> No error!');
              characteristics.forEach(function(characteristic) {

              //
              // Loop through each characteristic and match them to the
              // UUIDs that we know about.
              console.log('characteristics -> copying over!');
              console.log('found characteristic:', characteristic);

              if (pizzaCrustCharacteristicUuid == characteristic.uuid) {

              pizzaCrustCharacteristic = characteristic;
              }

              else if (pizzaToppingsCharacteristicUuid == characteristic.uuid) {

              pizzaToppingsCharacteristic = characteristic;
              }

              else if (pizzaBakeCharacteristicUuid == characteristic.uuid) {

              pizzaBakeCharacteristic = characteristic;
              }

              })

             

              //
              // Check to see if we found all of our characteristics.
              //
              if (pizzaCrustCharacteristic &&

              pizzaToppingsCharacteristic &&

              pizzaBakeCharacteristic) {

              console.log('all characteristics were found');
              // bakePizza();
              }

              else {

              console.log('missing characteristics');
              }

              })

              })

              })

              })

            })

             

            The result of this code is :

            root@edison:~/node_modules/noble/examples# node bleConnector.js

            start

            power

            halt Scanning

            why is this ?

             

            In another script that I have written to scan for devices, I am successful in discovering the device but I am having trouble connecting to it and serviceData is empty. See following code :

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

            noble.on('stateChange', function(state) {

              if(state === 'poweredOn') {

              noble.startScanning();
              console.log('scanning...');
              } else {

              console.log('not scanning');
              noble.stopScanning();
              }

            });

            noble.on('discover', function(peripheral) {

              console.log('peripheral discovered (' + peripheral.uuid+ '):');
              console.log('\t Name of Bluetooth Device('+ peripheral.advertisement.localName+')');
              console.log('\tcan I interest you in any of the following advertised services:');
              console.log('\t' + JSON.stringify(peripheral.advertisement.serviceUuids));

              console.log('getting Service Data');
              var serviceData = peripheral.advertisement.serviceData;

              console.log(serviceData);

              console.log(' 2');
              if (serviceData && serviceData.length) {

              console.log(' 3');
              console.log('\there is my service data:');
              for (var i in serviceData) {

              console.log(' 4');
              console.log('\t\t' + JSON.stringify(serviceData[i].uuid) + ': ' + JSON.stringify(serviceData[i].data.toString('hex')));
              }

              }

              if (peripheral.advertisement.manufacturerData) {

              console.log(' 5');
              console.log('\there is my manufacturer data:');
              console.log('\t\t' + JSON.stringify(peripheral.advertisement.manufacturerData.toString('hex')));
              }

              if (peripheral.advertisement.txPowerLevel !== undefined) {

              console.log(' 5');
              console.log('\tmy TX power level is:');
              console.log('\t\t' + peripheral.advertisement.txPowerLevel);
              }

             

              console.log(' 6');
            });

             

            Another issue that I have been having is that when i run the above code or the advertisement-discovery example that comes with noble, stateChange goes automatically to noble.stopScanning(); I have to turn off Edison and turn it back on for start scanning to occur. Why is this? is there a way around it ?

             

            Thank you so MUCH !

            • 3. Re: noble
              rosek

              a_Monteiro,

               

              Are you doing 'rfkill unblock bluetooth' before running your scripts? This needs to be executed at least after each OS restart but I've noticed that sometimes bluetooth locks when noble script crashes. That's why we run 'block', 'unblock' commands before starting noble script:


              rfkill block bluetooth

              rfkill unblock bluetooth

               

              Here is link which describes configuration for Edison:

              http://rexstjohn.com/configure-intel-edison-for-bluetooth-le-smart-development/

              • 4. Re: noble
                a_Monteiro

                Hi,

                 

                I realized right after i wrote you that the reason it wasn't working  is that bluetooth was soft blocked and I did rfkill unblock bluetooth to use it. However, noble is still not finding the serviceData of the peripheral as mentioned above in the code and I still can't connect with a specific given uuid of a device that is powered on .

                 

                Thank you for the swift response.

                • 5. Re: noble
                  rosek

                  Hi,

                   

                  We don't use it as this takes quite a lot of space in advertisement packet, there are 4 bytes overhead for each service entry (length, type, uuid). I haven't got chance to try this with noble, are you sure that the service data is set on the other end?

                   

                  You're second script doesn't have code which connect script to the pheripheral, can you describe better your connection issues?

                  • 6. Re: noble
                    a_Monteiro

                    Hello,

                     

                    I used this code to connect to the device and it seems to be working fine :

                    var noble = require('../index');
                    //checking the state of system and scan for BLE devices
                    noble.on('stateChange', function (state) {

                       if (state === 'poweredOn') {

                      noble.startScanning();
                       console.log('scanning...');
                       } else {

                       console.log('not scanning');
                       noble.stopScanning();
                       }

                    });

                    noble.on('discover', function(peripheral) {

                      peripheral.connect(function(error) {

                       var serviceUUIDs = ["fe1a56c98efa"];
                       if (serviceUUIDs == peripheral.uuid) {

                       console.log('Trying to connect ...');
                       console.log('connected to peripheral: ' + peripheral.uuid);
                       }

                     

                      });
                    });

                    OUTPUT :

                    scanning...

                    Trying to connect ...

                    connected to peripheral: fe1a56c98efa

                     

                     

                    However, this device is streaming data, how can i receive this data ? Also, how can i use process.exit(1); to exit the code at this point ? I used exactly process.exit(1); but WebStorm IDE is giving me an error.

                     

                    Thank you 

                    • 7. Re: noble
                      CMata_Intel

                      Hi a_Monteiro

                       

                      Do you have updates in this? Were you able to use noble, and receive data?

                       

                       

                      Regards;

                      CMata