Web Bluetooth example to control konashi2.0

by Takashi Toyoshima

HTML

<button id="find">Find Konashi</button>

JavaScript

// [References]
// - https://webbluetoothcg.github.io/web-bluetooth
// - https://github.com/WebBluetoothCG/web-bluetooth/blob/gh-pages/implementation-status.md
// - https://googlechrome.github.io/samples/web-bluetooth/device-info.html
// - http://konashi.ux-xu.com/documents/#core-ble
// - https://github.com/YUKAI/konashi-js-sdk
// - https://github.com/YUKAI/konashi-ios-sdk/

function run (characteristics) {
    pioSetting = characteristics[0];
    pioOutput = characteristics[1];
    console.log(pioSetting);
    console.log(pioOutput);
    var data = new Uint8Array(1);
    data[0] = 1 << 1;
    pioSetting.writeValue(data);
    setInterval(function () {
        data[0] ^= (1 << 1);
    	pioOutput.writeValue(data);
    }, 500);
    pioOutput.writeValue(data);
}

function service (service) {
    console.log(service);
    Promise.all([
    	service.getCharacteristic('229b3000-03fb-40da-98a7-b0def65c2d4b'),
    	service.getCharacteristic('229b3002-03fb-40da-98a7-b0def65c2d4b')
    ]).then(run, error);
}

function gatt (server) {
    console.log(server);
    server.getPrimaryService('229bff00-03fb-40da-98a7-b0def65c2d4b').then(service, error);
}

function found (device) {
    console.log(device);
    device.connectGATT().then(gatt, error);
}

function error (e) {
    console.log(e);
}

function find () {
	var options = {
	    filters: [{
//	        services: ['229bff00-03fb-40da-98a7-b0def65c2d4b']
	        namePrefix: 'konashi'
	    }]
	};
    navigator.bluetooth.requestDevice(options).then(found, error);
}

document.getElementById("find").addEventListener("click", find, false);