├── .gitignore ├── README.md ├── index.js ├── mp_nrf52.gif └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## NUS console 2 | 3 | This will scan and connect to the first BLE peripheral advertising the nordic UART service (`6e400001b5a3f393e0a9e50e24dcca9e`). 4 | 5 | ![nrf52 console image](mp_nrf52.gif) 6 | 7 | 8 | ### noble issue with Node 10 9 | 10 | Noble depends on `bluetooth-hci-socket` which (as of today) will [not build on node 10](https://github.com/noble/node-bluetooth-hci-socket/pull/91). The quick fix is to `npm link` the patched version. 11 | 12 | git clone -b fix-builds-for-node-10 https://github.com/jrobeson/node-bluetooth-hci-socket.git 13 | cd node-bluetooth-hci-socket 14 | npm link 15 | 16 | go into nus_console folder 17 | 18 | npm link bluetooth-hci-socket 19 | npm install 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const noble = require('noble'); 3 | 4 | const NUS_SERVICE_ID = '6e400001b5a3f393e0a9e50e24dcca9e'; 5 | const NUS_RX_CHAR = '6e400003b5a3f393e0a9e50e24dcca9e'; 6 | const NUS_TX_CHAR = '6e400002b5a3f393e0a9e50e24dcca9e'; 7 | 8 | var tx_char = null; 9 | 10 | noble.on('discover', (peripheral) => { 11 | noble.stopScanning(); 12 | console.log('Found device with local name: ' + peripheral.advertisement.localName); 13 | console.log('advertising the following service uuid\'s: ' + peripheral.advertisement.serviceUuids); 14 | console.log(); 15 | 16 | peripheral.connect((error) => { 17 | if (error) { 18 | console.log("connect", error); 19 | process.exit(0); 20 | } 21 | 22 | peripheral.on('disconnect', () => { 23 | process.exit(0); 24 | }); 25 | 26 | peripheral.discoverServices([], (error, services) => { 27 | for (let i = 0; i < services.length; i++) { 28 | if (services[i].uuid != NUS_SERVICE_ID) 29 | continue; 30 | services[i].discoverCharacteristics([], (error, characteristics) => { 31 | for (let i = 0; i < characteristics.length; i++) { 32 | let char = characteristics[i]; 33 | if (char.uuid == NUS_RX_CHAR) { 34 | char.on('data', (data, isNotification) => { 35 | process.stdout.write(data.toString('utf8')); 36 | }); 37 | char.subscribe(); 38 | } else if (char.uuid == NUS_TX_CHAR) { 39 | tx_char = char; 40 | } 41 | } 42 | }); 43 | } 44 | }); 45 | }); 46 | }); 47 | 48 | noble.on('stateChange', (state) => { 49 | if (state == "poweredOn") 50 | noble.startScanning(NUS_SERVICE_ID); 51 | else 52 | noble.stopScanning(); 53 | }); 54 | 55 | process.stdin.setRawMode(true); 56 | 57 | process.stdin.on('readable', () => { 58 | var chunk = process.stdin.read(); 59 | if ((chunk !== null) && (tx_char !== null)) { 60 | tx_char.write(Buffer.from(chunk), true); 61 | } 62 | }); 63 | -------------------------------------------------------------------------------- /mp_nrf52.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tralamazza/nus_console/59bc967be259cd81592ef05bd377cfbfe73168da/mp_nrf52.gif -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nus_console", 3 | "version": "1.0.0", 4 | "description": "Nordic UART Service console", 5 | "main": "index.js", 6 | "dependencies": { 7 | "noble": "^1.6.0" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "author": "Daniel Tralamazza", 14 | "license": "MIT" 15 | } 16 | --------------------------------------------------------------------------------