-
Notifications
You must be signed in to change notification settings - Fork 0
/
ble_nus.js
156 lines (145 loc) · 4.64 KB
/
ble_nus.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
'use strict';
const bleNusServiceUUID = '6e400001-b5a3-f393-e0a9-e50e24dcca9e';
const bleNusCharRXUUID = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
const bleNusCharTXUUID = '6e400003-b5a3-f393-e0a9-e50e24dcca9e';
const MTU = 20;
var bleDevice;
var nusService;
var rxCharacteristic;
var txCharacteristic;
var connected = false;
var bleBusy = false;
function connectionToggle() {
if (connected) {
disconnect();
} else {
connect();
}
}
// Sets button to either Connect or Disconnect
function changeConnectionState(state) {
connected = state;
let button = document.getElementById("clientConnectButton")
if (state) {
button.innerHTML = "Disconnect";
button.classList.add("connected");
openFullscreen(document.body);
} else {
button.innerHTML = "Connect";
button.classList.remove("connected");
document.exitFullscreen();
}
}
function connect() {
if (!checkBluetooth()) {
terminal_writeln('WebBluetooth API is not available on your browser.\r\n' +
'Please make sure the Web Bluetooth flag is enabled.');
return;
}
terminal_writeln('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice({
filters: [{services: [bleNusServiceUUID]}]
})
.then(device => {
bleDevice = device;
terminal_writeln('Found ' + device.name);
terminal_writeln('Connecting to GATT Server...');
bleDevice.addEventListener('gattserverdisconnected', onDisconnected);
return device.gatt.connect();
})
.then(server => {
terminal_writeln('Locate NUS service');
return server.getPrimaryService(bleNusServiceUUID);
}).then(service => {
nusService = service;
terminal_writeln('Found NUS service: ' + service.uuid);
})
.then(() => {
terminal_writeln('Locate RX characteristic');
return nusService.getCharacteristic(bleNusCharRXUUID);
})
.then(characteristic => {
rxCharacteristic = characteristic;
terminal_writeln('Found RX characteristic');
})
.then(() => {
terminal_writeln('Locate TX characteristic');
return nusService.getCharacteristic(bleNusCharTXUUID);
})
.then(characteristic => {
txCharacteristic = characteristic;
terminal_writeln('Found TX characteristic');
})
.then(() => {
terminal_writeln('Enable notifications');
return txCharacteristic.startNotifications();
})
.then(() => {
terminal_writeln('Notifications started');
txCharacteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
changeConnectionState(true);
terminal_writeln('\r\n' + bleDevice.name + ' Connected.');
})
.catch(error => {
terminal_writeln('' + error);
if(bleDevice && bleDevice.gatt.connected)
{
bleDevice.gatt.disconnect();
}
});
}
function disconnect() {
if (!bleDevice) {
terminal_writeln('No Bluetooth Device connected...');
return;
}
terminal_writeln('Disconnecting from Bluetooth Device...');
if (bleDevice.gatt.connected) {
bleDevice.gatt.disconnect();
changeConnectionState(false);
terminal_writeln('Bluetooth Device connected: ' + bleDevice.gatt.connected);
} else {
terminal_writeln('> Bluetooth Device is already disconnected');
}
}
function onDisconnected() {
changeConnectionState(false);
terminal_writeln('\r\n' + bleDevice.name + ' Disconnected.');
}
function handleNotifications(event) {
terminal_write('notification');
let value = event.target.value;
// Convert raw data bytes to character values and use these to
// construct a string.
let str = "";
for (let i = 0; i < value.byteLength; i++) {
str += String.fromCharCode(value.getUint8(i));
}
terminal_writeln(str);
}
function nusSendString(s, log=true) {
if (bleDevice && bleDevice.gatt.connected) {
if (log) terminal_writeln("send: " + s);
let val_arr = new Uint8Array(s.length)
for (let i = 0; i < s.length; i++) {
let val = s[i].charCodeAt(0);
val_arr[i] = val;
}
sendNextChunk(val_arr);
} else {
terminal_writeln('Not connected to a device yet.');
}
}
function sendNextChunk(a) {
let chunk = a.slice(0, MTU);
rxCharacteristic.writeValue(chunk)
.then(function() {
if (a.length > MTU) {
bleBusy = true;
sendNextChunk(a.slice(MTU));
} else {
bleBusy = false;
}
});
}