Click here to Skip to main content
14,160,721 members
Click here to Skip to main content
Add your own
alternative version

Stats

11.2K views
1 bookmarked
Posted 20 Aug 2015
Licenced CPOL

Working with Bluetooth using Node.js and node-red from Intel Edison

, 20 Aug 2015
Rate this:
Please Sign up or sign in to vote.
Workaround for Sending Bluetooth command from Android Phone to Javascript based programs in Intel Edison

1. Background

If you are working with Intel Edison then you must have already discovered that getting bluetooth to work in any sensible way in Edison is just a dead end. More so, if you are looking for a workaround with Node.js or node-red.

After hours of trail and errors, I could get it to work. So I am crating this page. Hope it saves someone's precisous time!

I would tell you the logic first. Intel Edison Bluetooth does not have a Serial port service which is important for phone-Edison communication. Intel has a Python script that provides Serial port service. A mod of this python script was written to write the data to a named pipe which could then be read from programs like Arduino.

Bad news is that javascript does not and will probably never support named pipes. So we write a bash script to read data from named pipe. Now using a command line websocket we publish the data being received by this shell script.

Now we install websocket and ws ( a client) with npm to enable Node.js to work with websockets. Using websocket client we listen to our deamon server.

Android Mobile--> Serial Port--> Python Background Script--> Named Pipe---> Bash Script--> Websocket-->Javascript.

Don't get afraid. Steps aren't too complicated. It works like a charm.

1) Unblock Bluetooth:

rfkill unblock bluetooth

2) Login to Bluetooth Console

bluetoothctl

3) Make discoverable

discoverable on

4) scan for the bluetooth device:

scan on


once the device is seen in list, ctrl+c


6) Pair 

pair <DEv_ID>


7) Connect to Bluetooth Device

connect <DEv_ID>


8) Trust the Device 

trust <DEv_ID>


9) Download Android bluetooth SPP app

 Android Bluetooth SPP Pro ( Free)

10) Now we need to follow This Intel Tutorial Here

Quote:


a) download :https://software.intel.com/sites/default/files/managed/6c/16/bluetooth-service.tar.gz
b) using WinScp to upload the file to /home/root
c) untar the package

mkdir /home/root/bluetooth
cd /home/root/bluetooth
mv /home/root/bluetooth-service.tar.gz ./
tar -xvf bluetooth-service.tar.gz

 

d) Prepare for Bluetooth at startup

cp bluetooth-spp-pin.service /lib/systemd/system

 systemctl enable bluetooth-spp-pin

<span style="margin: 0px; padding: 0px; border: 0px; color: rgb(17, 17, 17); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; background-color: rgb(238, 238, 238);">reboot</span>

e) after login back:

systemctl status bluetooth-spp-pin

 

11) This package has a file by name bluetooth-spp-service.py which runs serial port service and release them in a named pipe: /tmp/arduino_pipe_out
So now we need to write a bash script that can read from this named pipe

12) vi readPipe.sh

#!/bin/bash

pipe=/tmp/arduino_pipe_out

trap "rm -f $pipe" EXIT

if [[ ! -p $pipe ]]; then
    mkfifo $pipe
fi

while true
do
    if read line <$pipe; then
        if [[ "$line" == 'quit' ]]; then
            break
        fi
        echo $line
    fi
done

echo "Reader exiting"
​

esc :wq to come out from editor saving the file.


13) Make it Executable

chmod 755 readFile.sh

14) Execute it to test that you are able to fetch command being sent through bluetooth

./readFile.sh 

It will now wait for a bluetooth command. From your app, generate bluetooth command

You will see that is appearing in the command prompt

15) Websocket

javascript does not support pipes. So we need to release it using websocket.  Download websocketd  a command based websocket deamon. Unzip. copy the file websocketd to /home/root
from /home/root

chmod 755 websocketd


16) run the deamon  server

./websocketd --port=8080 ./readPipe.sh

 

16)Test with node-red

Now go to your node red.from input palatte take Websocket

change:
Type: Listen On
Path: /ws://localhost:8080


Connect it to debugger. Deploy. Now generate command from your android app. You will be able see the result in node-red

17) Test With Node.js

Install websocket first.

npm install websocket

Install ws, a lightweight websocket client

npm install ws

Write Node.js simple client to test it. say myWsClient.js

 

var WebSocket = require('ws')
  , ws = new WebSocket('ws://localhost:8080');
ws.on('open', function() {
    ws.send('something');
});
ws.on('message', function(message) {
    console.log('received: %s', message);
});

Run it with 

node myWsClient

That's all. Now you can receive commands from your Bluetooth enabled Android mobile to Intel Edison.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Grasshopper.iics
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.

This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.

Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.

He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
Group type: Organisation

116 members


You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190518.1 | Last Updated 20 Aug 2015
Article Copyright 2015 by Grasshopper.iics
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid