aboutsummaryrefslogtreecommitdiffstats
path: root/tests/html/bluetooth-permission.html
blob: ebfe40e8e78d6e6a98cc2230b492aed1dbc524a0 (plain) (blame)
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
<!DOCTYPE html>
<html>
<title>Bluetooth Permission Test</title>
<body>
    <button type="button" onclick="onRequestButtonClick()">request</button>
    <button type="button" onclick="onQueryButtonClick()">query</button>
    <pre id="log"></pre>
    <script>
    if (window.testRunner)
        window.testRunner.setBluetoothMockDataSet('HeartRateAdapter');

    function onRequestButtonClick() {
        clear();
        window.navigator.permissions.request({
            name: 'bluetooth',
            filters: [{
                services: ['heart_rate'],
            }],
        })
        .then(r => {
            log("Result is instance of PermissionStatus: " + (r instanceof PermissionStatus));
            log("Result is instance of BluetoothPermissionResult subclass: " + (r instanceof BluetoothPermissionResult));
            log("State of result: " + r.state);
            let device = r.devices()[0];
            if (device) {
                log("Result contains device: " + device.name);
                sessionStorage.lastDevice = device.id;
                log("Device id stored: " + device.id);
            } else {
                log("No device found!");
            }
        })
        .catch(err => log(err));
    }

    function onQueryButtonClick() {
        clear();
        if (!sessionStorage.lastDevice)
            return log("No stored device id!");
        window.navigator.permissions.query({
            name: "bluetooth",
            deviceId: sessionStorage.lastDevice,
        })
        .then(r => {
            log("Result is instance of PermissionStatus: " + (r instanceof PermissionStatus));
            log("Result is instance of BluetoothPermissionResult subclass: " + (r instanceof BluetoothPermissionResult));
            log("State of result: " + r.state);
            log("Stored Device id: " + sessionStorage.lastDevice);
            let device = r.devices()[0];
            if (device) {
                log("Result contains device: " + device.name);
                log("Device id: " + device.id);
            } else {
                log("No device found!");
            }
        })
        .catch(err => log(err));
    }

    function log(line) {
        document.getElementById("log").textContent += line + '\n';
    }

    function clear() {
        document.getElementById("log").textContent = "";
    }
    </script>
</body>
</html>