-
Notifications
You must be signed in to change notification settings - Fork 670
/
bluetoothSerial.js
169 lines (163 loc) · 5.9 KB
/
bluetoothSerial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This implementation is for testing in the brower. Future implemetations *might* be able to use
// Chrome Bluetooth APIs https://developer.chrome.com/apps/bluetooth or Web Bluetooth.
// Code from PhracturedBlue https://github.com/don/BluetoothSerial/issues/115
// When using the browser, it is necessary to have a processing function that
// can emulate the bluetooth device.
// Use the 'register' function as follows:
//
// bluetoothSerial.register(function(buf) {
// //buf.input is the data that was received via bluetoothSerial.write
// //buf.output is data that will be transmitted via a bluetoothSerial.read or subscribe
// //Do processing here
// buf.input = ""
// });
// Function emulates a Bluetooth device echoing back input
//
// var echoProxy = function(buf) {
// if (buf && buf.input) {
// console.log("Received: " + buf.input);
// buf.output = buf.input + "\n";
// buf.input = ""; // clear input
// }
// return buf;
// }
// bluetoothSerial.register(echoProxy);
module.exports = (function() {
var connected = false;
var enabled = true;
var buf = {
input: "",
output: "",
};
var subscribe_cb = function(value) {};
var raw_cb = false;
var subscribe_delim = false;
var interval;
var process_cb;
var btlog = function(str) { console.log(str); }
var timer_cb = function() {
if(process_cb) {
process_cb(buf);
}
if (buf.output.length) {
if(subscribe_delim) {
var index = buf.output.indexOf(subscribe_delim);
if (index > -1) {
var data = buf.output.substr(0, index+subscribe_delim.length);
buf.output = buf.output.substr(index+subscribe_delim.length);
subscribe_cb(data);
}
}
}
};
return {
connect : function(mac, success_cb, fail_cb) {
btlog("bluetoothSerial.connect: " + mac);
connected = true;
if (success_cb) { success_cb(); }
},
register : function(data_cb) {
interval = window.setInterval(timer_cb, 100);
process_cb = data_cb;
},
disconnect : function(success_cb, fail_cb) {
btlog("bluetoothSerial.disconnect");
connected = false;
window.clearInterval(interval);
if (success_cb) { success_cb(); }
},
write : function(data, success_cb, fail_cb) {
btlog("bluetoothSerial.write: " + data);
buf.input += data;
if(success_cb) { success_cb(); }
},
available : function(success_cb, fail_cb) {
btlog("bluetoothSerial.available");
success_cb(buf.output.length);
},
read : function(success_cb, fail_cb) {
btlog("bluetoothSerial.read: " + buf.output);
var data = buf.output;
buf.output = "";
success_cb(data);
},
readUntil : function(delimiter, success_cb, fail_cb) {
btlog("bluetoothSerial.readUntil");
var index = buf.output.indexOf(delimiter);
if (index == -1) {
success_cb("");
} else {
var data = buf.output.substr(0, index+subscribe_delim.length);
buf.output = buf.output.substr(index+subscribe_delim.length);
success_cb(data);
}
},
subscribe : function(delimiter, success_cb, fail_cb) {
btlog("bluetoothSerial.subscribe '"+delimiter+"'");
subscribe_cb = success_cb;
subscribe_delim = delimiter;
},
unsubscribe : function(success_cb, fail_cb) {
btlog("bluetoothSerial.unsubscribe");
subscribe_delim = false;
if(success_cb) { success_cb(); }
},
subscribeRawData : function(success_cb, fail_cb) {
btlog("bluetoothSerial.subscribeRawData");
raw_cb = success_cb;
},
unsubscribeRawData : function(success_cb, fail_cb) {
btlog("bluetoothSerial.unsubscribeRawData");
raw_cb = false;
if(success_cb) { success_cb(); }
},
clear : function(success_cb, fail_cb) {
btlog("bluetoothSerial.clear");
buf.output = "";
if(success_cb) { success_cb(); }
},
list : function(success_cb, fail_cb) {
var devices = [{
"class": 276,
"id": "10:BF:48:CB:00:00",
"address": "10:BF:48:CB:00:00",
"name": "Nexus 7"
}, {
"class": 7936,
"id": "00:06:66:4D:00:00",
"address": "00:06:66:4D:00:00",
"name": "RN42"
}]
success_cb(devices); },
isConnected : function(success_cb, fail_cb) {
btlog("bluetoothSerial.isConnected: " + connected);
if(connected) {
if(success_cb) { success_cb(); }
} else {
if(fail_cb) { fail_cb(); }
}
},
isEnabled : function(success_cb, fail_cb) {
btlog("bluetoothSerial.isEnabled: " + enabled);
if(enabled) {
if(success_cb) { success_cb(); }
} else {
if(fail_cb) { fail_cb(); }
}
},
readRSSI : function(success_cb, fail_cb) {
alert("bluetoothSerial.readRSSI is not implemented");
},
showBluetoothSettings : function(success_cb, fail_cb) {
alert("bluetoothSerial.showBluetoothSettings is not implemented");
},
enable : function(success_cb, fail_cb) {
btlog("bluetoothSerial.enable");
enable = true;
if(success_cb) { success_cb(); }
},
discoverUnpaired : function(success_cb, fail_cb) {
alert("bluetoothSerial.discoverUnpaired is not implemented");
},
}
})();