Skip to content

Instantly share code, notes, and snippets.

@openube
Forked from Cinezaster/01 bluetooth webapp
Created April 29, 2017 19:11
Show Gist options
  • Save openube/3069aac1d116b4aa9dd25859e4d05f0c to your computer and use it in GitHub Desktop.
Save openube/3069aac1d116b4aa9dd25859e4d05f0c to your computer and use it in GitHub Desktop.
Node.js Bluetooth integrated web-app
This a bluetooth web-app.
{
"name": "bluetoothApp",
"version": "1.0.0",
"dependencies": {
"jquery" : "~2.0.3"
}
}
<!Doctype htlm>
<html>
<head>
<title>Bluetooth Web-app</title>
<script src="/socket.io/socket.io.js"></script>
<script src="/bower_components/jquery/jquery.js"></script>
</head>
<body>
<h1>Bluetooth Web-app</h1>
<button>Search Bluetooth devices</button>
<ul>
</ul>
<input>
<script type="text/javascript">
var socket = io.connect('http://localhost');
socket.on('search_Bluetooth_stopped', function(){
$('button').html('Search Bluetooth devices');
})
socket.on('connected_Bluetooth', function(data){
var element = $('.connect-device[data-address="'+data.address+'"]')
element.html(element[0].innerText + ' <b class="label label-success">live</b>');
})
socket.on('device', function(data){
$('ul').append('<li><a class="connect-device" data-address ="'+ data.address +'">'+ data.name +'</li>');
$('.connect-device').on('click',function(event){
var address = $(event.target).attr('data-address');
socket.emit('open_Bluetooth_Connection', address);
})
});
$('button').on('click', function(event){
event.preventDefault();
$('button').html('Scanning');
$('.bluetoothList').html('');
socket.emit('search_Bluetooth',"new");
});
$('input').on('submit', function (){
})
socket.emit('search_Bluetooth',"");
</script>
</body>
</html>
{
"name": "BluetoothControl",
"description": "",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"socket.io": "0.9.16"
}
}
var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, BTSP = require('bluetooth-serial-port')
, BTserial = new BTSP.BluetoothSerialPort()
, fs = require('fs')
, BTDevices = [];
server.listen(3000);
app.use(express.static(__dirname));
io.sockets.on('connection', function (socket) {
socket.on('search_Bluetooth', function(data){
if (data == "new") {
BTserial.close();
console.log("search for Bluetooth Device")
BTserial.inquire();
} else {
console.log("return list of Bluetooth Devices");
for (var i = 0; i < BTDevices.length; i++) {
socket.emit('device',{address: TDevices[i].address, name: TDevices[i].name, channel: TDevices[i].channel});
}
}
});
BTserial.on('found', function(address, name) {
BTserial.findSerialPortChannel(address, function(channel) {
socket.emit('device',{address: address, name: name, channel: channel});
BTDevices.push({address: address, name: name, channel: channel});
});
});
BTserial.on('failure',function(err){
console.log('failure: '+ err)
});
BTserial.on('finished',function(){
console.log('finished searching')
socket.emit('search_Bluetooth_stopped',{});
})
socket.on('open_Bluetooth_Connection',function(address){
for (var i = 0; i < BTDevices.length; i++) {
var channel;
if (BTDevices[i].address === address) {
channel = BTDevices[i].channel;
};
};
BTserial.connect(address, channel, function() {
socket.emit('connected_Bluetooth',{address:address});
},function (){
console.log('Cannot connect');
});
});
socket.on('close_Bluetooth_Connection', function(){
BTserial.close();
});
socket.on('send_To_Bluetooth', function(data){
var buffer = new Buffer(data, 'utf8')
BTserial.write(buffer, function (err, bytesWritten){
if (err) {
console.log(err);
}
if (bytesWritten == buffer.length) {
console.log('All bytes are send');
}
});
})
var dataBuffer = "";
BTserial.on('data', function(buffer) {
dataBuffer = dataBuffer + buffer.toString('utf8');
if(dataBuffer.indexOf("\n") != -1){
getFromRobot(dataBuffer.slice(0,1));
dataBuffer = "";
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment