The Complete Magazine on Open Source

Talk to your Bluetooth device using Node.js

5.56K 0

Node.js is an event-driven I/O server-side JavaScript environment based on Google’s Chrome V8. It is a powerful runtime environment for developing varied tools and applications. This article talks about interfacing Node.js with Bluetooth devices.

Change in the world of technology is constant. This requires every one of us to upgrade ourselves with new technologies and adapt to them. With the Internet of Things (IoT) emerging as a new technology, companies are building new ecosystems for ‘things’ so that they too can have a piece of cake. Open source frameworks facilitated by the Linux Foundation are adding a dimension to the open things ecosystem.

Embedded computing, which once used to be closer to hardware languages like C, is slowly moving away to high level languages like Java. The reasons for such movement could be multi-fold, ranging from high power hardware that can still provide a better user experience, to the scalability of new generation applications. Such movement necessitates newer frameworks for the high level languages to be able to adapt to the new ecosystem. Node.js is one such framework that is getting adopted to develop a wide range of solutions.

An overview of Node.js
Node.js is an open source JavaScript runtime framework built on Chrome’s V8 JavaScript engine. The community activity is governed by the Node.js Foundation and the framework is available from The Node.js framework has the largest ecosystem of open source libraries, which extend its functionality considerably. These packages are available at The implementation of the framework can be explored at Having understood in brief how the framework works, let’s explore it in detail and understand the internal architecture.
The Node.js framework is part of the server side JavaScript, with the goal of offering a scalable framework to develop high performance network applications. This goal is realised by providing a single-threaded, event driven, non-blocking I/O framework. The framework is implemented in C/C++ with interface APIs in JavaScript. The block diagram in Figure 1 illustrates the overall structure of the Node.js framework.

The major blocks that the Node.js framework consists of are the interface APIs, a thin core and low lying libraries for event management, the HTTP server, security and the V8 engine. The framework also provides for scaling up the functionality as add-ons, as detailed in the next section.

Figure 1: Blocks of the Node.js framework

Figure 2: Illustrative representation of an event loop

Event loop
In an embedded firmware solution that has no scheduler to manage its different activities, a design involving interrupts and an infinite loop is used. The system’s activities, triggered by external interrupts or internal timer interrupts, are created as events and managed in the infinite loop outside the interrupt context. The way the loop manages the events is based on the application and the needs of the embedded solution.

On similar principles, the Node.js framework uses the event loop to manage different activities of the framework. This implementation is done in the lower library libuv available in Libuv is a cross-platform library aimed at providing a framework for event-driven asynchronous I/O. Figure 2 illustrates the event loop of the Node.js framework.

A detailed explanation of how the event loop is implemented within the Node.js framework is available at

Figure 3: A representation of non-blocking I/O

Non-blocking I/O
Asynchronous I/O operations using call back form the core part of the non-blocking I/O operation of the Node.js framework, as illustrated in Figure 3.

To provide a non-blocking design, the framework offloads I/O operations to the underlying operating system and is managed within the event loop. Once the operation is completed, the operating system will get back to the Node.js framework. This control back to the framework is via the call back that is passed while calling the I/O operation.

Figure 4: A representation of the Node.js HTTP server in the Linux Bluetooth ecosystem

Figure 5: Node.js HTTP server response on a browser

Talking to your Bluetooth device over Node.js
To scale up, Node.js extends the functionality by installing add-ons as packages. In this following example, let us explore how to interface Node.js with the Linux Bluetooth framework. The set-up is illustrated in Figure 4.

This section assumes that the reader has prior understanding of the BlueZ Linux driver and the DBus framework.

For example, the application that runs on the Node.js framework is a simple Web server that discovers Bluetooth devices in the vicinity and lists them on the browser. Let’s assume that a Linux system with a BlueZ stack is running a Node.js Web server with the package dbus-native. Given below is the code snippet of the application that creates a Web server on port ‘8080’ and starts discovering Bluetooth devices.

var dbus = require(‘dbus-native’);
var sBus = dbus.systemBus();

var systemBus = dbus.systemBus();
var btService = systemBus.getService(‘org.bluez’);



var http = require(“http”);

const server = http.createServer((request,response)=>{

response.statusCode = 200;
response.setHeader(‘Content-Type’, ‘text/plain’);
response.write(‘New Device Path : ‘+devname);

console.log(‘Server started’);

After running the application on the Node.js framework, open a browser and listen to port 8080 of the Web server. The browser will start listing the Bluetooth devices in the vicinity, as illustrated in Figure 5.