ガイド
Capacitor で Bluetooth Low Energy を使用する方法
@capgo/capacitor-bluetooth-low-energy の使い方
BLE 通信用の Capacitor Bluetooth Low Energy Plugin
インストール
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync
bunx cap sync プラグインのネイティブcodeをネイティブプロジェクトにコピーします。プラグインをアップグレードするたびに再度実行してください。
インポート
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
API の概要
| メソッド | 説明 |
|---|---|
initialize |
BLEプラグインを初期化します。他のメソッドを呼び出す前に必ず実行してください。 |
shimWebBluetooth |
Capacitor Web Bluetooth shimをインストールします navigator.bluetooth. Capacitor ネイティブアプリから Web Bluetooth API を使用する前に、手動でこれを呼び出してください。 |
isAvailable |
デバイスでBluetoothが利用可能か確認します。 |
isEnabled |
デバイスでBluetoothが有効か確認します。 |
isLocationEnabled |
位置情報サービスが有効か確認します(Androidのみ)。 |
openAppSettings |
アプリの設定ページを開きます。 |
openBluetoothSettings |
Bluetooth設定画面を開きます(Androidのみ)。 |
openLocationSettings |
位置情報設定画面を開きます(Androidのみ)。 |
checkPermissions |
現在の権限ステータスを確認します。 |
requestPermissions |
Bluetoothの権限をリクエストします。 |
startScan |
BLEデバイスのスキャンを開始します。 |
stopScan |
BLEデバイスのスキャンを停止します。 |
connect |
BLEデバイスに接続します。 |
disconnect |
BLEデバイスから切断します。 |
createBond |
BLEデバイスとボンディングします(Androidのみ)。 |
isBonded |
デバイスがボンディング済みか確認します(Androidのみ)。 |
discoverServices |
接続済みデバイスのサービスを検出します。 |
getServices |
デバイスの検出済みサービスを取得します。 |
getConnectedDevices |
接続済みデバイスの一覧を取得します。 |
readCharacteristic |
キャラクタリスティックの値を読み取ります。 |
writeCharacteristic |
キャラクタリスティックに値を書き込みます。 |
startCharacteristicNotifications |
キャラクタリスティックの通知を開始します。 |
stopCharacteristicNotifications |
キャラクタリスティックの通知を停止します。 |
readDescriptor |
ディスクリプタの値を読み取ります。 |
writeDescriptor |
ディスクリプタに値を書き込みます。 |
readRssi |
接続済みデバイスのRSSI(信号強度)を読み取ります。 |
requestMtu |
MTUサイズの変更をリクエストします(Androidのみ)。 |
requestConnectionPriority |
接続優先度の変更をリクエストします(Androidのみ)。 |
startAdvertising |
ペリフェラル(BLEサーバー)としてアドバタイズを開始します。 |
stopAdvertising |
アドバタイズを停止します。 |
addGattService |
ローカル GATT サーバーに、特性を持つ GATT サービスを追加します。アドバタイジングを開始する前に、ペリフェラルモードで呼び出す必要があります。 |
removeGattService |
ローカルGATTサーバーからGATTサービスを取り外します。 |
setGattCharacteristicValue |
ローカルGATT特性の値を設定します。 |
notifyGattCharacteristicChanged |
ローカルGATT特性の値が変更されたことを、接続中のセントラルに通知します。 |
startForegroundService |
フォアグラウンドサービスを開始して、バックグラウンドでもBLE接続を維持します(Androidのみ)。 |
stopForegroundService |
フォアグラウンドサービスを停止します(Androidのみ)。 |
例
initialize()
BLEプラグインを初期化します。他のメソッドを呼び出す前に必ず実行してください。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
shimWebBluetooth()
Capacitor Web Bluetooth shimをインストールします navigator.bluetooth. Capacitor ネイティブアプリから Web Bluetooth API を使用する前に、手動でこれを呼び出してください。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();
isAvailable()
デバイスでBluetoothが利用可能か確認します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();
isEnabled()
デバイスでBluetoothが有効か確認します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();
isLocationEnabled()
位置情報サービスが有効か確認します(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();
openAppSettings()
アプリの設定ページを開きます。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();
上記の表は36個のコアメソッドを一覧にしています。リスナーとバージョンヘルパー、および各メソッドの完全なコントラクトは、次で説明しています: GitHub リポジトリ.
イベントをリッスンする
addListener ハンドルを返します。画面がアンマウントされるときに handle.remove() 画面がアンマウントされるとき、または BluetoothLowEnergy.removeAllListeners() すべてのリスナーを解除します。
adapterStateChanged iOS と Android で Bluetooth の電源状態の変化を報告します(Web には非対応)。ペイロードは { enabled: boolean } のみです。 enabled に変わります true後、周辺機器アプリは addGattService を再度呼び出し、 startAdvertising を再度呼び出します。Bluetooth がオフになるとプラグインはローカルの GATT レコードをクリアするため、同じサービス UUID を再度追加できます。 スタートガイド 各プラットフォームでイベントが発火するタイミングについては、
import { BluetoothLowEnergy, AdapterStateChangedEvent } from '@capgo/capacitor-bluetooth-low-energy';
const handle = await BluetoothLowEnergy.addListener(
'adapterStateChanged',
async (event: AdapterStateChangedEvent) => {
if (event.enabled) {
// Re-add services and start advertising again in peripheral mode.
}
},
);
完全なリファレンス
@capgo/capacitor-bluetooth-low-energy の使用から続ける
使用している場合 @capgo/capacitor-bluetooth-low-energy の使い方 ネイティブプラグインの作業を計画するには、次と連携してください: @capgo/capacitor-bluetooth-low-energy @capgo/capacitor-bluetooth-low-energy の実装の詳細については、 スタートガイド スタートガイドの実装詳細については、 Capgo プラグインディレクトリ Capgo プラグインディレクトリのプロダクトワークフローについては、 Capgo による Capacitor プラグイン Capgo による Capacitor Plugins の実装詳細、および プラグインの追加または更新 プラグインの追加または更新に関する実装詳細についてはこちら。