メインコンテンツへスキップ
@capgo/capacitor-bluetooth-low-energy デバイス API オープンソース

Bluetooth Low Energy (BLE) Capacitor プラグイン

Bluetooth デバイスのスキャン、接続、読み取り、書き込み、通知受信が可能なフル機能の BLE プラグイン

インストール

bun add @capgo/capacitor-bluetooth-low-energy bunx cap sync
npm、pnpm、または yarn
  • npm install @capgo/capacitor-bluetooth-low-energy
  • pnpm add @capgo/capacitor-bluetooth-low-energy
  • yarn add @capgo/capacitor-bluetooth-low-energy

ガイド

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 の実装詳細、および プラグインの追加または更新 プラグインの追加または更新に関する実装詳細についてはこちら。

よくある質問

Bluetooth Low Energy プラグイン FAQ

Capacitor アプリに Bluetooth Low Energy プラグインをインストールするにはどうすればよいですか?

"bun add @capgo/capacitor-bluetooth-low-energy"(または "npm install @capgo/capacitor-bluetooth-low-energy")を実行した後、"bunx cap sync" を実行して、iOS および Android プロジェクトにネイティブの code を反映させます。その後、アプリの code から "@capgo/capacitor-bluetooth-low-energy" をインポートしてください。

@capgo/capacitor-bluetooth-low-energy は React、Vue、Angular で動作しますか?

はい。Bluetooth Low Energy は Capacitor パッケージであるため、Ionic、React、Vue、Angular、Svelte、およびプレーンな JavaScript を含む、Capacitor 内で動作するあらゆる Web フレームワークで使用できます。

@capgo/capacitor-bluetooth-low-energy はどの Capacitor バージョンをサポートしていますか?

Capgo プラグインは Capacitor のメジャーバージョンに合わせています。使用している Capacitor のメジャーバージョンに一致するプラグインのメジャーバージョンを使ってください (例: Capacitor 8 ならプラグイン v8)。GitHub の README にある互換性テーブルに、メンテナンス中のバージョンが記載されています。

@capgo/capacitor-bluetooth-low-energy は無料のオープンソースですか?

はい。code のソースコードは GitHub の https://github.com/Cap-go/capacitor-bluetooth-low-energy/ で公開されており、パッケージは npm から無料でインストールできます。バグ報告やプルリクエストも歓迎しています。

App Store の審査なしで、Bluetooth Low Energy を使用している code をアップデートできますか?

プラグインのインストールやアップグレードはネイティブの code を変更するため、新しいストアビルドが必要です。その後は、プラグインを呼び出す JavaScript、HTML、CSS の変更を Capgo Live Updates で即座に配信できます。

アプリの審査を待たずに Bluetooth Low Energy の変更をリリース

プラグインをストア用ビルドに組み込めば、Capgo live updates で JavaScript、HTML、CSS の変更を数分でユーザーに配信できます

Capgo から始める