{"id":5462,"date":"2017-08-21T11:33:35","date_gmt":"2017-08-21T02:33:35","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=5462"},"modified":"2017-09-21T10:43:22","modified_gmt":"2017-09-21T01:43:22","slug":"microbit-web-bluetooth-api-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%82%b9%e3%83%a9%e3%82%a4%e3%83%89%e3%82%92%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b%e3%80%82","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/microbit\/5462","title":{"rendered":"Micro:bit + Web Bluetooth API \u3092\u4f7f\u3063\u3066\u30b9\u30e9\u30a4\u30c9\u3092\u64cd\u4f5c\u3059\u308b\u3002"},"content":{"rendered":"<blockquote class=\"twitter-tweet\" data-width=\"550\" data-dnt=\"true\">\n<p lang=\"en\" dir=\"ltr\">Web Bluetooth API + reveal.js<a href=\"https:\/\/twitter.com\/hashtag\/microbit?src=hash&amp;ref_src=twsrc%5Etfw\">#microbit<\/a> <a href=\"https:\/\/twitter.com\/hashtag\/chibibit?src=hash&amp;ref_src=twsrc%5Etfw\">#chibibit<\/a> <a href=\"https:\/\/t.co\/kz7GLorrh3\">pic.twitter.com\/kz7GLorrh3<\/a><\/p>\n<p>&mdash; \u6e05\u6c34\u6b63\u884c (@_shimizu) <a href=\"https:\/\/twitter.com\/_shimizu\/status\/896309841412542464?ref_src=twsrc%5Etfw\">August 12, 2017<\/a><\/p><\/blockquote>\n<p><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n<p><a href=\"https:\/\/shimz.me\/example\/WebBluetooth\/slide\/#\/\">example<\/a><\/p>\n<h2>\u6982\u8981<\/h2>\n<p>\u4ee5\u524d\u3001<a href=\"https:\/\/gunmagisgeek.com\/blog\/microbit\/5346\">node.js + reval.js\u3067\u4f5c\u3063\u305f\u3082\u306e<\/a>\u306eWeb Bluetooth\u7248\u3002<br \/>\n\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u304c\u5fc5\u8981\u306a\u304f\u306a\u3063\u305f\u306e\u3067\u3001\u3088\u308a\u624b\u8efd\u306b\u6271\u3048\u308b\u3002<\/p>\n<h2>\u4e8b\u524d\u6e96\u5099<\/h2>\n<ul>\n<li>\u30da\u30a2\u30ea\u30f3\u30b0\u306e\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3092\u89e3\u9664\u3057\u3001\u3059\u3079\u3066\u306eBLE\u30b5\u30fc\u30d3\u30b9\u3092\u6709\u52b9\u306b\u3059\u308b\u305f\u3081\u306b\u4ee5\u4e0b\u306e\u4f5c\u696d\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308b\u3002\u53c2\u8003\uff1a<a href=\"https:\/\/github.com\/sandeepmistry\/node-bbc-microbit#flashing-microbit-firmware\">Flashing micro:bit firmware<\/a>\n<ol>\n<li><a href=\"https:\/\/github.com\/sandeepmistry\/node-bbc-microbit\/tree\/master\/firmware\">hex\u30d5\u30a1\u30a4\u30eb<\/a>\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3002<\/li>\n<li>USB\u30b1\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u3066\u30de\u30a4\u30af\u30ed\u30d3\u30c3\u30c8\u3092PC\u306b\u63a5\u7d9a\u3002<\/li>\n<li>hex\u30d5\u30a1\u30a4\u30eb\u3092micro\uff1abit\u30c7\u30a3\u30b9\u30af\u30c9\u30e9\u30a4\u30d6\u306b\u30b3\u30d4\u30fc\u3059\u308b\u3002<\/li>\n<li>micro:bit\u3092\u518d\u8d77\u52d5\u3059\u308b\u3002<\/li>\n<li>LED\u306b\u30c9\u30c3\u30c8\u304c\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u3001\u50be\u3051\u3066\u5186\u3092\u63cf\u304f(\u30ad\u30e3\u30ea\u30d6\u30ec\u30fc\u30b7\u30e7\u30f3)<\/li>\n<\/ol>\n<\/li>\n<li>\u30b5\u30fc\u30d3\u30b9\uff0f\u30ad\u30e3\u30e9\u30af\u30bf\u30ea\u30b9\u30c6\u30a3\u30c3\u30af\u306eUUID\u3092\u8abf\u3079\u3066\u304a\u304f\u3002<br \/><a href=\"https:\/\/lancaster-university.github.io\/microbit-docs\/resources\/bluetooth\/bluetooth_profile.html\">\u3053\u3053<\/a>\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<pre class=\"lang:js decode:true \" >\/\/ reveal\u521d\u671f\u8a2d\u5b9a\r\nReveal.initialize({\r\n\thistory: true,\r\n});\r\n\t\t\r\n\t\t\r\nvar bluetoothDevice;\r\n\r\n\/\/chibi:bit BLE UUID\r\nvar BUTTON_SERVICE_UUID          = 'e95d9882-251d-470a-a062-fa1922dfa9a8';\r\nvar BUTTON_A_CHARACTERISTIC_UUID = 'e95dda90-251d-470a-a062-fa1922dfa9a8';\r\nvar BUTTON_B_CHARACTERISTIC_UUID = 'e95dda91-251d-470a-a062-fa1922dfa9a8';\r\n\r\n\r\n\r\n\r\n\/\/\u30dc\u30bf\u30f3\u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc\r\nd3.select(\"#connect\").on(\"click\", connect);\r\nd3.select(\"#disconnect\").on(\"click\", disconnect);    \r\n\r\n\r\n\r\n\/\/chibi:bit\u306b\u63a5\u7d9a\u3059\u308b\r\nfunction connect() {\r\n  let options = {};\r\n\r\n  \r\n  \/\/options.acceptAllDevices = true;\r\n  \r\n  options.filters = [\r\n    {services: [BUTTON_SERVICE_UUID]},\r\n    {name: \"BBC micro:bit [vaget]\"}\r\n  ];\r\n  \r\n\r\n  navigator.bluetooth.requestDevice(options)\r\n  .then(device =&gt; {\r\n    bluetoothDevice = device;\r\n    return device.gatt.connect();\r\n  })\r\n  .then(server =&gt;{\r\n    return server.getPrimaryService(BUTTON_SERVICE_UUID);\r\n  })\r\n  .then(service =&gt; {\r\n    return Promise.all([\r\n        service.getCharacteristic(BUTTON_A_CHARACTERISTIC_UUID),\r\n        service.getCharacteristic(BUTTON_B_CHARACTERISTIC_UUID)\r\n    ]);    \r\n  })\r\n  .then(chara =&gt; {\r\n    alert(\"BLE\u63a5\u7d9a\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002\");\r\n    \r\n\t\/\/\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u969b\u306e\u901a\u77e5\u3092\u6709\u52b9\u5316\u3059\u308b\r\n    chara[0].startNotifications();\r\n    chara[1].startNotifications();\r\n    \r\n\t\/\/\u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af\u30a4\u30d9\u30f3\u30c8\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u8a2d\u5b9a\r\n    chara[0].oncharacteristicvaluechanged = changeABtnEvent;\r\n    chara[1].oncharacteristicvaluechanged = changeBBtnEvent;\r\n\r\n    return chara;\r\n\r\n  })  \r\n  .catch(error =&gt; {\r\n    console.log(error);\r\n  });    \r\n}\r\n\r\n\/\/A\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u969b\u306e\u51e6\u7406\r\nfunction changeABtnEvent(event) {\r\n  var value = event.currentTarget.value.getInt8(0);\r\n  if (value) Reveal.prev();\r\n}\r\n\/\/B\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u969b\u306e\u51e6\u7406\r\nfunction changeBBtnEvent(evnet) {\r\n  var value = event.currentTarget.value.getInt8(0);\r\n  if (value) Reveal.next(); \r\n}\r\n\r\n\/\/BEL\u5207\u65ad\u51e6\u7406\r\nfunction disconnect() {\r\n  if (!bluetoothDevice || !bluetoothDevice.gatt.connected) return ;\r\n  bluetoothDevice.gatt.disconnect();\r\n  alert(\"BLE\u63a5\u7d9a\u3092\u5207\u65ad\u3057\u307e\u3057\u305f\u3002\")\r\n}\r\n\t\t\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Web Bluetooth API + revea&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5463,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-5462","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microbit","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/5462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=5462"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/5462\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/5463"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=5462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=5462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=5462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}