Last active
February 5, 2021 16:02
-
-
Save sander/93879f82d6dbd0667298 to your computer and use it in GitHub Desktop.
BLE to stdin/stdout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bleancs = require('ble-ancs') | |
var t = require('transit-js') | |
var writer = t.writer('json') | |
var reader = t.reader('json') | |
function status(v) { | |
return t.map([t.keyword('type'), t.keyword('status'), | |
t.keyword('value'), t.keyword(v)])} | |
function notif(v) { | |
return t.map([t.keyword('type'), t.keyword('notification'), | |
t.keyword('detail'), JSON.parse(v.toString())])} | |
function out(v) { | |
console.log(reader.read(writer.write(v)))} | |
var ancs = new bleancs() | |
.on('notification', function(n) { | |
n.readTitle(function() { | |
n.readMessage(function() { | |
out(notif(n))})})}) | |
._able | |
.on('advertisingStart', function() { | |
out(status('advertising'))}) | |
.on('accept', function() { | |
out(status('connecting'))}) | |
.on('encryptChange', function() { | |
out(status('connected'))}) | |
.on('disconnect', function() { | |
out(status('disconnected'))}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BleAncs = require('./ble-ancs'); | |
var Able = require('./ble-ancs/lib/able'); | |
var SERVICE = '6e400001b5a3f393e0a9e50e24dcca9e'; | |
var TX = '6e400002b5a3f393e0a9e50e24dcca9e'; | |
var RX = '6e400003b5a3f393e0a9e50e24dcca9e'; | |
var state = {peripheral: null, tx: null}; | |
function onRead(v) { | |
console.log('Read:', v.toString())} | |
var other = new Able() | |
.on('stateChange', function(state) { | |
if (state == 'poweredOn') { other.startScanning() } | |
else { other.stopScanning() }}) | |
.on('discover', function(p) { | |
console.log('peripheral', p.uuid); | |
if (p.advertisement.serviceUuids.indexOf(SERVICE) != -1) { | |
console.log('trying to connect!!'); | |
other.stopScanning(); | |
setTimeout(function(){ | |
console.log('really'); | |
p.connect(function(err) { | |
console.log('CONNECTED!!'); | |
p.findService(SERVICE, function(err, services) { | |
services[0].discoverCharacteristics([], function(err, cs) { | |
cs.forEach(function(c) { | |
if (c.uuid == TX) { | |
state.tx = c} | |
else if (c.uuid == RX) { | |
c.notify(true) | |
c.on('read', onRead)}})})})}) | |
state.peripheral = p}, 1000)}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment