manniru
5/31/2016 - 2:58 AM

app.js

<!doctype html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<button id="requestButton">requestDevice</button>
<script src="app.js"></script>
</body>
</html>
let printCharacteristic;

sendTextData = () => {
    let encoder = new TextEncoder('utf-8');
};

document.querySelector("#requestButton").addEventListener("click", () => {
    navigator.bluetooth.requestDevice({
        filters: [{
            services: ['000018f0-0000-1000-8000-00805f9b34fb']
        }]
    })
        .then(device => {
            console.log('> Found ' + device.name);
            console.log('Connecting to GATT Server...');
            return device.gatt.connect();
        })
        .then(server => server.getPrimaryService("000018f0-0000-1000-8000-00805f9b34fb"))
        .then(service => service.getCharacteristic("00002af1-0000-1000-8000-00805f9b34fb"))
        .then(characteristic => {
            printCharacteristic = characteristic;
            console.log(printCharacteristic); // printCharacteristic.writeValueで書き込めるっぽい
        })
        .catch(error => console.log(error));
}, false);