-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
117 lines (105 loc) · 3.54 KB
/
index.html
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
<html>
<script>
var myCharacteristic;
var deviceName;
function connect() {
let serviceUuid = "0000ffe0-0000-1000-8000-00805f9b34fb";
let characteristicUuid = "0000ffe1-0000-1000-8000-00805f9b34fb";
navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => {
log('Connecting...');
deviceName = device.name;
return device.gatt.connect();
})
.then(server => {
console.log('Getting Service...');
return server.getPrimaryService(serviceUuid);
})
.then(service => {
console.log('Getting Characteristic...');
return service.getCharacteristic(characteristicUuid);
})
.then(characteristic => {
myCharacteristic = characteristic;
return myCharacteristic.startNotifications().then(_ => {
console.log('> Notifications started');
log("Connected to: " + deviceName);
myCharacteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
});
})
.catch(error => {
console.log('Argh! ' + error);
});
}
function disconnect() {
if (myCharacteristic) {
myCharacteristic.stopNotifications()
.then(_ => {
console.log('> Notifications stopped');
log("Disconnected")
myCharacteristic.removeEventListener('characteristicvaluechanged',
handleNotifications);
})
.catch(error => {
console.log('Argh! ' + error);
});
}
}
function handleNotifications(event) {
let value = event.target.value;
log(deviceName + "> " + new TextDecoder().decode(value));
console.log('> Received: ' + new TextDecoder().decode(value));
}
function send() {
var data = document.getElementById("input").value;
log("You> " + data);
myCharacteristic.writeValue(str2ab(data+"\n"))
document.getElementById("input").value = "";
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function log(str) {
document.getElementById("term").value += str+"\n";
}
</script>
<head>
<title>Web-Bluetooth Terminal</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="header">WEB-BLUETOOTH TERMINAL</div>
<div class="terminal"><textarea id="term" readonly class="terminal"></textarea></div>
<div class="footer">
<div class="inputfield">
<input id ="input" type="text" class="input" placeholder="Write something to send here...">
</div>
<div class="button" style="background-color: black;">
<button onclick="send()">
<i class="fa fa-paper-plane"></i>
</button>
</div>
<div id="connect" class="button" style="background-color: green;" >
<button onclick="connect()">
<i class="fa fa-plug"></i>
</button>
</div>
<div class="button" style="background-color: red;">
<button onclick="disconnect()">
<i class="fa fa-times"></i>
</button>
</div>
</div>
</body>
</html>