-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
113 lines (99 loc) · 3.13 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
<button id="read">Connect with BLE device</button>
<button id="start" disabled>Start</button>
<button id="stop" disabled>Stop</button>
<script>
var deviceName = 'BLE Battery'
var bleService = 'battery_service'
var bleCharacteristic = 'battery_level'
var bluetoothDeviceDetected
var gattCharacteristic
document.querySelector('#read').addEventListener('click', function() {
if (isWebBluetoothEnabled()) { read() }
})
document.querySelector('#start').addEventListener('click', function(event) {
if (isWebBluetoothEnabled()) { start() }
})
document.querySelector('#stop').addEventListener('click', function(event) {
if (isWebBluetoothEnabled()) { stop() }
})
function isWebBluetoothEnabled() {
if (!navigator.bluetooth) {
console.log('Web Bluetooth API is not available in this browser!')
return false
}
return true
}
function getDeviceInfo() {
let options = {
optionalServices: [bleService],
filters: [
{ "name": deviceName }
]
}
console.log('Requesting any Bluetooth Device...')
return navigator.bluetooth.requestDevice(options).then(device => {
bluetoothDeviceDetected = device
}).catch(error => {
console.log('Argh! ' + error)
})
}
function read() {
return (bluetoothDeviceDetected ? Promise.resolve() : getDeviceInfo())
.then(connectGATT)
.then(_ => {
console.log('Reading UV Index...')
return gattCharacteristic.readValue()
})
.catch(error => {
console.log('Waiting to start reading: ' + error)
})
}
function connectGATT() {
if (bluetoothDeviceDetected.gatt.connected && gattCharacteristic) {
return Promise.resolve()
}
return bluetoothDeviceDetected.gatt.connect()
.then(server => {
console.log('Getting GATT Service...')
return server.getPrimaryService(bleService)
})
.then(service => {
console.log('Getting GATT Characteristic...')
return service.getCharacteristic(bleCharacteristic)
})
.then(characteristic => {
gattCharacteristic = characteristic
gattCharacteristic.addEventListener('characteristicvaluechanged',
handleChangedValue)
document.querySelector('#start').disabled = false
document.querySelector('#stop').disabled = true
})
}
function handleChangedValue(event) {
let value = event.target.value.getUint8(0)
var now = new Date()
console.log('> ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + ' UV Index is ' + value)
}
function start() {
gattCharacteristic.startNotifications()
.then(_ => {
console.log('Start reading...')
document.querySelector('#start').disabled = true
document.querySelector('#stop').disabled = false
})
.catch(error => {
console.log('[ERROR] Start: ' + error)
})
}
function stop() {
gattCharacteristic.stopNotifications()
.then(_ => {
console.log('Stop reading...')
document.querySelector('#start').disabled = false
document.querySelector('#stop').disabled = true
})
.catch(error => {
console.log('[ERROR] Stop: ' + error)
})
}
</script>