forked from shivendrasaurav/corona-rakshak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-bt.html
53 lines (47 loc) · 1.33 KB
/
web-bt.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
<!DOCTYPE html>
<html>
<head>
<title>Connect BT DEVICES</title>
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0" />
</head>
<form>
<button>Connect BT device</button>
</form>
<script>
// var deviceName = 'If want to connect to specific device, specify name'
function isWebBluetoothEnabled() {
if (!navigator.bluetooth) {
// firefox not supported
console.log('Web Bluetooth API is not available in this browser!')
return false
}
return true
}
function playvib() {
navigator.vibrate([500,110,500,110,450,110,200,110,170,40,450,110,200,110,170,40,500]);
}
function getDeviceInfo() {
let options = {
acceptAllDevices: true // Option to accept all devices
// to call specific device
// "filters": [
// { "name": deviceName }
// ]
}
console.log('Requesting Bluetooth Device...')
navigator.bluetooth.requestDevice(options).then(device => {
console.log('> Name: ' + device.name)
}).catch(error => {
console.log('Argh! ' + error)
})
}
document.querySelector('form').addEventListener('submit', function(event) {
event.stopPropagation()
event.preventDefault()
if (isWebBluetoothEnabled()) {
getDeviceInfo();
playvib();
}
})
</script>
</html>