Giter Site home page Giter Site logo

noble-winrt's Introduction

Noble (Node.js Bluetooth LE) for Windows 10

This is a rewrite of noble-uwp using the C++/WinRT API.

System Requirements

  • Node.js v6.14.2 or later.
  • Windows 10 build 10.0.15063 or later
  • Windows 10 SDK build 10.0.17134.0

Usage

Simply require noble-winrt instead of noble:

const noble = require('noble-winrt');

On non-Windows platforms or Windows versions lower than 10.0.15063 this will use the regular noble implementation and on Windows version 10.0.15063 or later it will use the native binding using the C++/WinRT API.

Implementation Status

Everything should work that also works with the regular noble bindings except:

  • Writing/Reading to descriptor handles is not supported
  • Broadcast is not supported

noble-winrt's People

Contributors

geovie avatar mfpi avatar tony-gutierrez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

noble-winrt's Issues

advertisement updates performance

I was having some issues with noble-uwp being rather unreactive with devices with fast advertisement updates (like say 500-1000ms). Have you tested with such devices? What kind of performance have you seen?

TypeError getting the module root - "path" is undefined

I'm trying to get started with this project but I'm running into this TypeError upon importing the package.

noble error
To me it looks like bindings.js it's trying to get the module root directory, but exports.getFileName() returns undefined. Not sure if this is a mistake on my end or a bug.

I double checked my system requirements, even tried downgrading Windows SDK. This package seems like the only option for noble with Windows 10 bluetooth. I'm using electron v10.1.4 and multiple issues seem claim that noble-uwp doesn't work with electron 8 & up. Any suggestions would be greatly appreciated! ๐Ÿ™

EDIT: I started a fresh electron project to test and I found that this package does work on electron v10.1.4 and electron v12.0.1. Something about my electron project must be causing this error, but I still don't know why.

Issues subscribing and receiving data

Hi!
It seems to me that BLE support on windows is somewhat limited and UWP-centric. This module allows development of electron apps. Thanks for all of your work!! :)
Having said that, I'm having issues subscribing a service characteristic with the "notify" attribute: it doesn't seem to trigger the data event. Perhaps I'm doing something wrong:

uartCharacteristic.subscribe(function(error) {
     console.log('battery level notification on');
});
uartCharacteristic.on('data', function(data, isNotification){
	console.log(data);
});

Also, i read that

Writing/Reading to descriptor handles is not supported
Broadcast is not supported

Does this mean subscribing to a service characteristic with the "notify" attribute is not supported yet?
Thanks again!

Improve error handling

Currently we only log error in the noble-winrt package as noble has a rather bad error handling story (noble/noble#830). The problem is that it only emits a warning if e.g. a peripheral is not found and there is no way for bindings to return an error.

A proper solution would be to add a error argument in the bindings interface and the noble interface to let bindings error bubble up to the user.

Noble.prototype.onDisconnect = function(peripheralUuid, error) {
  var peripheral = this._peripherals[peripheralUuid];
  if (peripheral) {
    peripheral.emit('disconnect', error);
  } else {
    this.emit('warning', 'unknown peripheral ' + peripheralUuid + ' disconnected!');
  }
};

Fails to discover characteristics for service

No matter what I put in for the array of characteristic UUIDs it fails to discover characteristics and never calls the callback. The same code discovers all characteristics in noble-uwp. (The service is successfully discovered)

GetCharacteristicsForUuidAsync: no characteristic with given id
BLEManager::Notify::<lambda_a1184b3e8bf00761754d30ee7a9f05b8>::operator (): GetCharacteristic error

server.discoverServices(serviceFilter, function (err, services) {
    logger.info("Discovering services");
    if (err) {
        logger.error('Service discovery error: ' + err);
        bt.properties.bluetoothDevice.disconnect();
        return;
    }
    console.log(services[0]);
    services[0].discoverCharacteristics([], function (err, characteristics) {
        if (err) {
            logger.error('Characteristic discovery error: ' + err);
            bt.properties.bluetoothDevice.disconnect();
            return;
        }
        console.log(characteristics);
    });
});

Binding does not respect list of service ids in DiscoverServices, and instead tells windows to discover all (slow).)

This function simply requests all services from windows, and then filters the list, instead of requesting specific services from windows. For some devices with many services, this is slower than necessary.

bool BLEManager::DiscoverServices(const std::string& uuid,
                                  const std::vector<winrt::guid>& serviceUUIDs)
{
    CHECK_DEVICE();
    IFDEVICE(device, uuid)
    {
        auto completed = bind2(this, &BLEManager::OnServicesDiscovered, uuid, serviceUUIDs);
        device.GetGattServicesAsync(BluetoothCacheMode::Uncached).Completed(completed);
        return true;
    }
}

Problems with npm install

I am trying to use noble-winrt in my electron app.
I added the entry for "noble-winrt": "Timeular/noble-winrt#master" in my package.json
When I try to run npm install, I get

fatal error C1083: Cannot open include file: 'winrt/Windows.Devices.Enumeration.h': No such file or directory (compiling source file ..\src\radio_watcher.cc)

Similar errors are seen for Blutetooth.GenericAttributeProfile.h and Bluetooth.Advertisement.h

I also tried running npm install in a directory different from my electron app (package.json entry was same as mentioned above). Same errors were seen.

I am running node v10.15.3
Windows 10.0.17763
Windows 10 SDK build 10.0.18362.0

Is it possible to manually add a service/char to the winrt cache?

Noble has a caching mechanism for services/chars. See the examples folder for cache-gatt.
For a binding to support this, it needs to have an addService and addCharacteristic method.

Do we know if it would be possible to create a windows GattDeviceService or GattCharacteristic manually, and inject it into cachedServices/Chars?

I looked at service, and the noble representation is

 {
      "startHandle": 13,
      "endHandle": 21,
      "uuid": "a3e68a834c4d4778bd0a829fb434a7a1"
}

I was unable to determine how I would create this manually from looking at: https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.genericattributeprofile.gattdeviceservice?view=winrt-19041

I assume you would need to call some GattDeviceService constructor, and then somehow set the uuid and handles? Sorry, C++ is not my forte.

turning windows BT off and on results in no scanning.

workaround is to add a setTimeout delay like this in your JS code:

noble.on('stateChange', (state) => {
  console.log('BLE stateChange:', state);
  if (state === 'poweredOn') {
    setTimeout(() => {
      //start scanning here. 
    }, 300);
  }
  if (state === 'poweredOff') {
    // stop scanning, etc
  }
});

Problems with Electron compilation

As the object, i downloaded the module via github and i place the folder into the node_modules and renamed it in noble-winrt so i tried to compile all with:
npm rebuild --runtime=electron --target=3.0.8 --arch=x64 --rebuild --disturl=https://atom.io/download/electron --build_from_source=true

and then i get these errors:

> [email protected] install D:\Progetti\Domo\ui\node_modules\noble-winrt
> node-pre-gyp install --fallback-to-build

node-pre-gyp WARN Using request for node-pre-gyp https download
module.js:549
    throw err;
    ^

Error: Cannot find module 'napi-thread-safe-callback'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at [eval]:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at Object.runInThisContext (vm.js:139:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:652:30)
    at evalScript (bootstrap_node.js:466:27)
gyp: Call to 'node -p "require('napi-thread-safe-callback').include"' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (C:\Users\fedyf\AppData\Roaming\nvm\v8.11.2\node_modules\npm\node_modules\node-gyp\lib\configure.js:336:16)
gyp ERR! stack     at emitTwo (events.js:126:13)
gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\fedyf\\AppData\\Roaming\\nvm\\v8.11.2\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure" "--fallback-to-build" "--module=D:\\Progetti\\Domo\\ui\\node_modules\\noble-winrt\\native\\noble_winrt.node" "--module_name=noble_winrt" "--module_path=D:\\Progetti\\Domo\\ui\\node_modules\\noble-winrt\\native" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=electron-v3.0" "--python=C:\\Python27\\python.exe"
gyp ERR! cwd D:\Progetti\Domo\ui\node_modules\noble-winrt
gyp ERR! node -v v8.11.2
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute 'C:\Program Files\nodejs\node.exe C:\Users\fedyf\AppData\Roaming\nvm\v8.11.2\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=D:\Progetti\Domo\ui\node_modules\noble-winrt\native\noble_winrt.node --module_name=noble_winrt --module_path=D:\Progetti\Domo\ui\node_modules\noble-winrt\native --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=electron-v3.0 --python=C:\Python27\python.exe' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (D:\Progetti\Domo\ui\node_modules\node-pre-gyp\lib\util\compile.js:83:29)
node-pre-gyp ERR! stack     at emitTwo (events.js:126:13)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:214:7)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:925:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
node-pre-gyp ERR! System Windows_NT 10.0.17134
node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\Progetti\\Domo\\ui\\node_modules\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd D:\Progetti\Domo\ui\node_modules\noble-winrt
node-pre-gyp ERR! node -v v8.11.2
node-pre-gyp ERR! node-pre-gyp -v v0.11.0
node-pre-gyp ERR! not ok
Failed to execute 'C:\Program Files\nodejs\node.exe C:\Users\fedyf\AppData\Roaming\nvm\v8.11.2\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=D:\Progetti\Domo\ui\node_modules\noble-winrt\native\noble_winrt.node --module_name=noble_winrt --module_path=D:\Progetti\Domo\ui\node_modules\noble-winrt\native --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=electron-v3.0 --python=C:\Python27\python.exe' (1)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

UPDATE

i run the npm install command into the module folder and the i run the rebuild command from root of my project and then i get A LOT of errors such as:
c:\program files (x86)\windows kits\10\include\10.0.17763.0\cppwinrt\winrt\windows.devices.enumeration.h(2484): error C2913

Any tips for rebuilding noble-winrt?

Thank you and Timeular for working on noble-winrt, I'm very hopeful this gets merged into noble soon.

I'm facing similar struggles to issue #11, with the exception that I'm trying to build for nw.js. Rather than having you specify every possible version of electron and nw.js in appveyor, do you have any tips for the correct dev setup for building noble-winrt?

I have Visual Studio 2017 installed along with all of the build tools but am facing tons of errors when trying to rebuild (most along the lines of \noble-winrt\src\napi_winrt.h(7): error C2065: 'UUID': undeclared identifier)

I've tried running:

  • prebuild -t 0.34.5 -r node-webkit
  • nw-gyp rebuild --target=0.34.5 --arch=ia32
  • node-gyp rebuild
  • prebuild -t 6.14.4 -t 8.12.0 -r node

All producing the same errors (error_log.log)

manufacturerData is null

Hi,

I'm trying your take on noble because I had some annoying delays with the noble-uwp library.
I'm working with RFDuino's en they can contain some extra data in the manufacturerData. However, using your library, manufacturerData is null:

{ localName: 'SMASHY',
  txPowerLevel: 0,
  manufacturerData: null,
  serviceUuids: [ '00002220-0000-1000-8000-00805f9b34fb' ],
  serviceData: [] }

With noble-uwp, it's:

{ localName: 'SMASHY',
  txPowerLevel: 4,
  manufacturerData: <Buffer 00 00 67 72 65 65 6e>,
  serviceUuids: [ '2220' ],
  serviceData: [ { uuid: '2220' } ] }

If I can be of any help to debug this, let me know.

Is there a need for the local cache?

Windows has its own cache of services and chars, so do we really need the local cache that winrt currently has?

The local cache is not currently populated by DiscoverServices, so subsequent calls for a single service or char will hit windows again, windows will serve from its ble cache, and then we add to local cache.

It seems like the local cache could be eliminated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.