LoginSignup
1
0

Node.js のパッケージ「Node Web Bluetooth(webbluetooth)」の情報を見てみたり toio との組みわせを試して見る

Posted at

以下の記事を書いている際に見かけた Node.js のパッケージ、「Node Web Bluetooth(webbluetooth)」が気になったので、まずは情報を見てみたという話です。

●Mac で noble を使って BLE対応のデバイスをスキャンする+α(2024年5月版) - Qiita
 https://qiita.com/youtoy/items/44216ea20c6506d80ed8

同じような名称のもので、ブラウザで BLE を扱う Web Bluetooth API がありますが、これと同じ使い方ができる Node.js のパッケージというもののようです。

●thegecko/webbluetooth: Node.js implementation of the Web Bluetooth Specification
 https://github.com/thegecko/webbluetooth

image.png

そして直近では、2024年の 2月にアップデートが行われている状況のようです。

image.png

「Node Web Bluetooth(webbluetooth)」の情報を詳しく見ていく

それでは、「Node Web Bluetooth(webbluetooth)」の情報を詳しく見ていきます。

上で GitHub のリポジトリのページを掲載しましたが、そこに以下のページの情報が掲載されていました。

●Node Web Bluetooth
 https://thegecko.github.io/webbluetooth/

使い方

使い方については、以下を見るとブラウザ向けの Web Bluetooth API と同じになるようです。

image.png

またその下を見ていくと、そのブラウザ向けの Web Bluetooth API の仕様にある、どの仕組みが実装されているかも掲載されています。

ざっと、以下に掲載してみます。

以下のカテゴリは、基本的なものはだいたい使えるようです。
image.png

以下も、基本的なものは問題なく使えそうです。

image.png

image.png

image.png

読み書きと通知の 3種類、全てが問題なく利用できそうです。

さらに見ていきます。

以下は半々くらいのようです。

image.png

とりあえず、接続して読み書き・通知を使うのは大丈夫そうな感じがします。

軽く使ってみる

ブラウザ用の Web Bluetooth API は、過去にいろいろ試してきました。

それを、わりとそのまま流用できそうなので、以下の記事で書いた内容をベースに、簡単なお試しをやってみます。

●Web Bluetooth API で Characteristic UUID の一覧を取得してみる(toio を使い p5.js Web Editor上で試す) - Qiita
 https://qiita.com/youtoy/items/1c80dcca6a8834cb6588

上記の記事の内容から、ポイントになる部分の処理を取り出してみます。

async function connectAndGetServicesAndCharacteristics() {
  const TOIO_SERVICE_UUID = "10b20100-5b3b-4571-9508-cf3efcd7bbae";
  try {
    const device = await navigator.bluetooth.requestDevice({
      filters: [{ services: [TOIO_SERVICE_UUID] }],
    });
    const server = await device.gatt.connect();
    const services = await server.getPrimaryServices();
    console.log("Found services:", services);

    for (const service of services) {
      const characteristics = await service.getCharacteristics();
      console.log(`Service: ${service.uuid}`);
      for (const characteristic of characteristics) {
        console.log(`  Characteristic: ${characteristic.uuid}`);
      }
    }
  } catch (error) {
    console.error("Error:", error);
  }
}

Node Web Bluetooth用の処理

これに少し手を加えて、Node Web Bluetooth用の処理は以下としてみました。

const bluetooth = require("webbluetooth").bluetooth;

const TOIO_SERVICE_UUID = "10b20100-5b3b-4571-9508-cf3efcd7bbae";

async function connectAndGetServicesAndCharacteristics() {
  try {
    const device = await bluetooth.requestDevice({
      filters: [{ services: [TOIO_SERVICE_UUID] }],
    });
    const server = await device.gatt.connect();
    const services = await server.getPrimaryServices();
    console.log("Found services:", services);

    for (const service of services) {
      const characteristics = await service.getCharacteristics();
      console.log(`Service: ${service.uuid}`);
      for (const characteristic of characteristics) {
        console.log(`  Characteristic: ${characteristic.uuid}`);
      }
    }
  } catch (error) {
    console.error("Error:", error);
  }
}

connectAndGetServicesAndCharacteristics();

処理を実行してみる

処理を実行してみます。 npm i webbluetooth を実行して、パッケージのインストールはすませておいてください。

その後、toio を 1台近くで起動して、上記を nodeコマンドで実行してみます。その結果、以下の出力が得られて、無事に処理が行えることが確認できました。

image.png

image.png

ちなみに、今回利用した過去の記事で、ブラウザ上で同様の処理を行った時の出力は以下です。今回と同じ内容が得られているのが確認できました。

image.png

1
0
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
1
0