ガイド
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サーバーに、特性(characteristics)を持つ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() すべてのリスナーを解除します。
完全なリファレンス
@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 の実装詳細、および プラグインの追加または更新 プラグインの追加または更新に関する実装詳細についてはこちら。