LoginSignup
66
56

More than 5 years have passed since last update.

2019年2月版 JavaScript/Node.jsでBluetoothを触れるライブラリやAPIをまとめてみた #html5j #iotlt #linethings

Last updated at Posted at 2019-02-19
1 / 19

この資料について


自己紹介


今日の内容

  • JavaScriptでやれるBluetooth周りの話

モチベなど

  • こんな事案
    • Webやってる人に「IoTやろうよ〜」みたいな事を言う
    • だいたいの人が「興味はあるけど、モノがない」と返答する
  • よくある学習サービスがやりやすい理由
    • -> PCだけあればやれる
  • IoTとかモノづくりってそもそも始めにくい
    • -> デバイス用意が必要
      • ここに圧倒的な入門障壁があると思ってます

Q.IoTやモノづくり領域ってデバイスないと"全く"やれない?

A. そんなことはないはず


PCとスマホがあればやりはじめが出来る内容を検討したい

  • Web Bluetooth
  • WebRTC
  • WebMIDI
  • WebAudio
  • その他, ちょっとデバイス必要かも

JSで扱えるBluetooth周りのAPIたち

ブラウザ - WebBluetooth


Node.js (今回のメインこの辺)

  • 1. noble
  • 2. bleno
  • 3. node-bluetooth
  • 4. LINE Simple Becon
  • 5. CYLON

noble


bleno


最近のMacでBlenoが使えない(2019/2/19)


nobleやbleno使うときはBluebirdでプロミス化が良さそう

noble.on('discover', function(peripheral) {
    peripheral.connect(function(err) {
        peripheral.discoverServices([pizzaServiceUuid], function(err, services) {
            services.forEach(function(service) {
                service.discoverCharacteristics([], function(err, characteristics) {
                    characteristics.forEach(function(characteristic) {
                        CrustCharacteristic.write(crust, false, function(err) {
                            //ここでやっと書き込みができる
                        }
                    }
                }
            }
        }
    }
}
const discovered = async (peripheral) => {
    const p = Promise.promisifyAll(peripheral); //Promisify
    await p.connectAsync();
    const services = await p.discoverSomeServicesAndCharacteristicsAsync(serviceUUIDs,characteristicUUIDs);
    const characteristic = services.reduce((pre, current) => (current.uuid === serviceUUIDs[0]))
                            .characteristics
                            .reduce((pre, current) => (current.uuid === characteristicUUIDs[0]));
    const c = Promise.promisifyAll(characteristic); //Promisify

    //書き込み
    await c.writeAsync(new Buffer.from([state]), true);
}

node-bluetooth

  • BLEではなくBluetoothシリアルを読み込んだりするライブラリ
  • いわゆるペアリングなどが発生するデバイス(Bluetooth v3まで?)はこちらで一覧が見れる
const bluetooth = require('node-bluetooth');
const device = new bluetooth.DeviceINQ();
device.listPairedDevices(console.log);
$ node app.js
[ { name: 'u-sen-lan',
    address: 'xx-xx-xx-xx-xx-xx',
    services:
     [ [Object],
       [Object] ] },
  { name: 'Joy-Con (L)',
    address: 'xx-xx-xx-xx-xx-xx',
    services: [ [Object], [Object], [Object] ] },
  { name: 'Nexus 5',
    address: 'xx-xx-xx-xx-xx-xx',
    services:
     [ [Object],
       [Object] ] },
  { name: 'AB Shutter 3',
    address: 'xx-xx-xx-xx-xx-xx',
    services: [ [Object], [Object], [Object] ] } ]

↑僕のMacで連携中のBluetoothデバイスの一覧が出ます。

一番最後の「AB Shutter 3」は買ったばかりのBluetooth シャッター

https://www.instagram.com/p/BuD9tqYDCwU/


LINE Simple Becon


CYLON


デバイス用意しないでどうやってやりはじめる?

  • スマホアプリでデバイスのエミュレートが出来ます。
  • スマホ <-> PCで連携してプログラムを試したり出来ます。

その他、面白いAPI見つけ方

  • chrome://flags/をチェック

  • navigator.~をチェック

  • 「Web x 既存の通信技術名」で検索してみると意外とヒットするものもある(かも)

    • WebNFCなど
  • npmを探ってみる

    • npm内で「bluetooth」「usb」などで検索してみると面白い

皆さんも見つけたらシェアしましょう〜
そして毎月開催している「IoTLT」で話しにきてね☆ (番宣)


まとめ

  • JavaScript/Node.jsとか使うとBluetoothけっこうやれるのでみんな楽しんでいきましょ。
  • Webエンジニアがこっち側きてくれるようになるといいな
66
56
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
66
56