Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including bglib with all npm packages installed. Try it out:

var bglib = require("bglib")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

bglib v0.0.82

Create and parse packets for the BlueGiga BLE112 and BLE113

##BGLib BGLib is a Node library for sending and receiving packets from BlueGiga BLE devices (currently supports ble112 and ble113). It is still under heavy development and will likely not work as intended for a while. It is currently being developed by Technical Machine to support our Tessel BLE module.

I still need to replace bitwise functions with Buffers once those are fully functional on Tessel.

##Install

npm install bglib

##Examples

###Creating a packet

var bglib = require('bglib');

bglib.getPacket(bglib.api.systemHello); // Returns [0, 0, 0, 1]

// Use Packet Mode if you don't have flow control
// It adds a length byte to the front of every packet
bglib.setPacketMode(true);
bglib.getPacket(bglib.api.systemHello); // Returns [4, 0, 0, 0, 1]

bglib.getPacket(bglib.api.gapDiscover, [1, 0], function(err, response) {
    console.log("Packet: ", response);
})

###Parsing Incoming Data

var bglib = require('bglib');

var incomingBytes = [ 128, 38, 6, 0, 189, 0, 108, 189, 40, 93, 28, 216, 1, 255, 27, 2, 1, 6, 17, 6, 186, 86, 137, 166, 250, 191, 162, 189, 1, 70, 125, 110, 56, 88, 171, 173, 5, 22, 10, 24, 7, 4];

bglib.parseIncoming(incomingBytes, function(err, parsedPackets) {
    console.log("Parsed packets: ", parsedPackets);

    // BGLib will automatically parse each packet into the values stated by the datasheet
    // depending on what type of event/response it is. Below is an example for the discovered
    // peripheral event that was passed in:
    console.log(parsedPackets[0].packet_type) // 0
    console.log(parsedPackets[0].sender) // [ 108, 189, 40, 93, 28, 216 ]
    console.log(parsedPackets[0].address_type) // 1
    console.log(parsedPackets[0].bond) // 255
    console.log(parsedPackets[0].data) // [ 27, 2, 1, 6, 17, 6, 186, 86, 137, 166, 250, 191, 162, 189, 1, 70, 125, 110, 56, 88, 171, 173, 5, 22, 10, 24, 7, 4 ]
    console.log(parsedPackets[0].rssi) // -100
});

The library will hold the current state of the packets so you can pass in pieces of a packet and it will return the entire parsed packet when it has all been passed in.

###Thanks JRowberg deserves a special shout-out. Some of the code is modeled after his Arduino bglib work.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free