ガイド
CapacitorでBluetooth Low Energyを使用する方法
@capgo/capacitor-bluetooth-low-energyを使用する方法
CapacitorのBluetooth Low EnergyプラグインはBLE通信に使用されます。
インストール
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 シムをインストールします。 navigator.bluetoothWeb ブルートゥース API を使用する前に、Web ブルートゥース Capacitor を手動で呼び出してください。 |
isAvailable |
__CAPGO_KEEP_1__ |
isEnabled |
Bluetooth が利用可能かどうかを確認します。 |
isLocationEnabled |
Bluetooth が有効かどうかを確認します。 |
openAppSettings |
位置情報サービスが有効かどうか (Android 限定) を確認します。 |
openBluetoothSettings |
アプリの設定画面を開きます。 |
openLocationSettings |
Android用の場所の設定ページを開きます。 |
checkPermissions |
現在の許可状況を確認してください。 |
requestPermissions |
Bluetoothの許可を求めます。 |
startScan |
BLEデバイスのスキャンを開始します。 |
stopScan |
BLEデバイスのスキャンを停止します。 |
connect |
BLEデバイスに接続します。 |
disconnect |
BLEデバイスから切断します。 |
createBond |
Android用のBLEデバイスとバンドを形成します。 |
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 |
Bluetooth Low Energyプラグイン |
setGattCharacteristicValue |
Bluetooth Low Energyプラグイン |
notifyGattCharacteristicChanged |
Bluetooth Low Energyプラグイン |
startForegroundService |
Androidのみで、バックグラウンドでBLE接続を維持するために、前景サービスを開始します。 |
stopForegroundService |
Androidのみで、前景サービスを停止します。 |
例
initialize()
BLEプラグインを初期化します。どの他のメソッドよりも前に呼び出す必要があります。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
shimWebBluetooth()
Web Bluetooth shimをCapacitorにインストールします。 navigator.bluetoothCapacitorネイティブアプリから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を継続して使用
Capacitor Bluetooth Low Energyを使用している場合 @capgo/capacitor-Bluetooth低消耗エネルギー Capacitor Bluetooth Low Energyを接続 @capgo/capacitor-bluetooth-low-energy @capgo/capacitor-Bluetooth低エネルギー Capacitor Bluetooth Low Energyの実装詳細については、Capacitor Bluetooth Low EnergyのGetting Startedを参照してください Capacitor プラグインディレクトリ Capgo プラグインディレクトリ for the product workflow in Capgo Plugin Directory, Capacitor Capgo プラグイン Capacitor Bluetooth Low Energy の実装詳細については、Capacitor プラグインによって Capgo によって。 Capacitor Bluetooth Low Energyの実装詳細についてはCapacitor Bluetooth Low EnergyのGetting Startedを参照してください Capacitor Bluetooth Low Energy プラグインの使用方法