スタートガイド
インストール手順と、このプラグインの完全なMarkdownガイドを含むセットアッププロンプトをコピーします。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-bluetooth-low-energy`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/bluetooth-low-energy/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
インストール
「インストール」セクションAIアシストによるセットアップでプラグインをインストールできます。以下のコマンドを使用して、お使いのAIツールに Capgo のスキルを追加してください:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins次に、以下のプロンプトを使用します:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-bluetooth-low-energy` plugin in my project.手動セットアップを希望する場合は、以下のコマンドを実行してプラグインをインストールし、下記のプラットフォーム別の手順に従ってください:
bun add @capgo/capacitor-bluetooth-low-energynpx cap syncインポート
「インポート」セクションimport { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';クイックスタート
「クイックスタート」セクションimport { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });例: shimWebBluetooth
「例: shimWebBluetooth」というセクションimport { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();例: isAvailable
「例: isAvailable」というセクションimport { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();よくある落とし穴
「よくある落とし穴」セクション- 権限:呼び出し
checkPermissions()その後requestPermissions()必要に応じて。 - リスナー: 画面がアンマウントされたらハンドルを削除します。
- 一度だけ初期化: アプリの起動時にセットアップを実行します。
API の概要
「API 概要」セクションinitialize
「initialize」セクションBLE プラグインを初期化します。
他のメソッドを呼び出す前に呼び出す必要があります(例外は addListener()しません。 adapterStateChanged 呼び出す前に initialize() 初期の iOS 状態イベントを見逃さないようにするためです。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });shimWebBluetooth
「shimWebBluetooth」セクションCapacitor Web Bluetooth shimをインストールします navigator.bluetooth.
Capacitor ネイティブアプリから Web Bluetooth API を使用する前に、このメソッドを手動で呼び出してください。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();isAvailable
「isAvailable」セクションデバイスでBluetoothが利用可能か確認します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();isEnabled
「isEnabled」セクションデバイスでBluetoothが有効か確認します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();isLocationEnabled
「isLocationEnabled」セクション位置情報サービスが有効か確認します(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();openAppSettings
「openAppSettings」セクションアプリの設定ページを開きます。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();openBluetoothSettings
セクション「openBluetoothSettings」Bluetooth設定画面を開きます(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openBluetoothSettings();openLocationSettings
セクション「openLocationSettings」位置情報設定画面を開きます(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openLocationSettings();checkPermissions
「checkPermissions」セクション現在の権限ステータスを確認します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.checkPermissions();requestPermissions
「requestPermissions」セクションBluetoothの権限をリクエストします。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.requestPermissions();startScan
「startScan」セクションBLEデバイスのスキャンを開始します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startScan({ services: ['180D'], // Heart Rate Service timeout: 10000});stopScan
セクション「stopScan」BLEデバイスのスキャンを停止します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopScan();connect
「connect」セクションBLEデバイスに接続します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.connect({ deviceId: 'AA:BB:CC:DD:EE:FF' });disconnect
「disconnect」セクションBLEデバイスから切断します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.disconnect({ deviceId: 'AA:BB:CC:DD:EE:FF' });createBond
「createBond」セクションBLEデバイスとボンディングします(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.createBond({ deviceId: 'AA:BB:CC:DD:EE:FF' });isBonded
「isBonded」セクションデバイスがボンディング済みか確認します(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bonded } = await BluetoothLowEnergy.isBonded({ deviceId: 'AA:BB:CC:DD:EE:FF' });discoverServices
「discoverServices」セクション接続済みデバイスのサービスを検出します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.discoverServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });getServices
「getServices」セクションデバイスの検出済みサービスを取得します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { services } = await BluetoothLowEnergy.getServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });getConnectedDevices
「getConnectedDevices」セクション接続済みデバイスの一覧を取得します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { devices } = await BluetoothLowEnergy.getConnectedDevices();readCharacteristic
「readCharacteristic」セクションキャラクタリスティックの値を読み取ります。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { value } = await BluetoothLowEnergy.readCharacteristic({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37'});writeCharacteristic
「writeCharacteristic」セクションキャラクタリスティックに値を書き込みます。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.writeCharacteristic({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A39', value: [0x01]});startCharacteristicNotifications
セクション「startCharacteristicNotifications」キャラクタリスティックの通知を開始します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startCharacteristicNotifications({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37'});stopCharacteristicNotifications
「stopCharacteristicNotifications」セクションキャラクタリスティックの通知を停止します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopCharacteristicNotifications({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37'});readDescriptor
「readDescriptor」セクションディスクリプタの値を読み取ります。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { value } = await BluetoothLowEnergy.readDescriptor({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37', descriptor: '2902'});writeDescriptor
「writeDescriptor」セクションディスクリプタに値を書き込みます。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.writeDescriptor({ deviceId: 'AA:BB:CC:DD:EE:FF', service: '180D', characteristic: '2A37', descriptor: '2902', value: [0x01, 0x00]});readRssi
「readRssi」セクション接続済みデバイスのRSSI(信号強度)を読み取ります。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { rssi } = await BluetoothLowEnergy.readRssi({ deviceId: 'AA:BB:CC:DD:EE:FF' });requestMtu
「requestMtu」セクションMTUサイズの変更をリクエストします(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { mtu } = await BluetoothLowEnergy.requestMtu({ deviceId: 'AA:BB:CC:DD:EE:FF', mtu: 512});requestConnectionPriority
「requestConnectionPriority」セクション接続優先度の変更をリクエストします(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.requestConnectionPriority({ deviceId: 'AA:BB:CC:DD:EE:FF', priority: 'high'});startAdvertising
「startAdvertising」セクションペリフェラル(BLEサーバー)としてアドバタイズを開始します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startAdvertising({ name: 'MyDevice', services: ['180D']});stopAdvertising
「stopAdvertising」セクションアドバタイズを停止します。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopAdvertising();startForegroundService
「startForegroundService」セクションフォアグラウンドサービスを開始して、バックグラウンドでもBLE接続を維持します(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startForegroundService({ title: 'BLE Connection', body: 'Maintaining connection...'});stopForegroundService
「stopForegroundService」セクションフォアグラウンドサービスを停止します(Androidのみ)。
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopForegroundService();addListener('adapterStateChanged')
セクション「addListener('adapterStateChanged')」Bluetooth アダプターの電源状態の変化を監視します。このイベントは iOS と Android で発火します。Web では発火しません。8.2.11 以降で利用可能。
import { BluetoothLowEnergy, AdapterStateChangedEvent } from '@capgo/capacitor-bluetooth-low-energy';
const handle = await BluetoothLowEnergy.addListener( 'adapterStateChanged', (event: AdapterStateChangedEvent) => { console.log('Bluetooth enabled:', event.enabled); },);
// later:await handle.remove();ペイロードは { enabled: boolean } に限定してください。 enabled が true アダプターがオンになった場合、 false オフの場合は。プラグインは他のフィールドを追加しません。
発火タイミング:
- Android: システムが
BluetoothAdapter.STATE_OFF(enabled: false) またはSTATE_ON(enabled: true) を報告した場合。中間状態(オン/オフの遷移中)ではこのイベントは発火しません。現在の状態はinitialize()時点では送信されず、アダプターが後でオフまたはオンに達したときにのみ送信されます。 - iOS: 現在のモードの Core Bluetooth マネージャーが新しい状態を報告した場合、かつ
enabledが前回の発火から変更された場合のみ。enabledがtrueそのマネージャーの状態が.poweredOnの場合のみ。他の状態(電源オフ、リセット中、未承認など)はenabled: false。centralとして報告されます。peripheralモードではソースは central manager です。initialize()モードではソースは peripheral manager です(central manager のコールバックは発火しません)。initialize()中にマネージャーを作成すると、現在の状態も報告されます。この初期状態のコールバックはinitialize()が解決する前に発火する可能性があり、プラグインはイベントを保持しないため、後から登録されたリスナーはこれを逃し、後の変更のみを受信する可能性があります。enabled: false次に、enabled: trueを呼び出す前にリスナーを登録してください。iOS は、マネージャーが.poweredOn以外の状態で開始し、後に電源オンになった場合にinitialize()を発火する可能性があります。isEnabled()が解決した後、
を呼び出して現在のアダプター状態を読み取ってください。 centralDisconnected Bluetooth がオフになると、プラグインは保持していたローカルの GATT サービスと特性レコードを破棄し、追跡中の各 central に対して adapterStateChanged を使用して enabled: false。Android では GATT サーバーも閉じ、アドバタイズコールバックもクリアされます。プラグインはサービスやアドバタイズの復元を自動で行いません。
アプリがペリフェラルの場合、 adapterStateChanged を使用して enabled: true、その後呼び出す addGattService を各サービスに対して再度実行し、 startAdvertising を再度実行してください。プラグインはローカルレコードをクリアするため、同じサービス UUID を追加しても「Service already exists」エラーで拒否されず、受け入れられます。Android では、アダプターが復帰した際に、スキャナー(セントラルモード)またはアドバタイザー(ペリフェラルモード)が再取得されます。ただし、サービスの追加とアドバタイズの開始は引き続き手動で行う必要があります。
型リファレンス
「型リファレンス」セクションInitializeOptions
「InitializeOptions」セクションプラグインの初期化オプション。
export interface InitializeOptions { /** * The mode to initialize the plugin in. * - 'central': Act as a BLE central (client) * - 'peripheral': Act as a BLE peripheral (server) * * @default 'central' * @since 1.0.0 */ mode?: 'central' | 'peripheral';}IsAvailableResult
「IsAvailableResult」セクションisAvailable メソッドの結果。
export interface IsAvailableResult { /** * Whether Bluetooth is available on the device. * * @since 1.0.0 */ available: boolean;}IsEnabledResult
「IsEnabledResult」セクションisEnabled メソッドの結果。
export interface IsEnabledResult { /** * Whether Bluetooth is enabled on the device. * * @since 1.0.0 */ enabled: boolean;}IsLocationEnabledResult
セクション「IsLocationEnabledResult」isLocationEnabled メソッドの結果。
export interface IsLocationEnabledResult { /** * Whether location services are enabled on the device. * * @since 1.0.0 */ enabled: boolean;}PermissionStatus
「PermissionStatus」セクションBluetooth と位置情報の権限ステータス。
export interface PermissionStatus { /** * Bluetooth permission status. * * @since 1.0.0 */ bluetooth: PermissionState;
/** * Location permission status (Android only). * * @since 1.0.0 */ location: PermissionState;}StartScanOptions
「StartScanOptions」セクションスキャンを開始するためのオプション。
export interface StartScanOptions { /** * List of service UUIDs to filter by. * Only devices advertising these services will be returned. * * @since 1.0.0 */ services?: string[];
/** * Scan timeout in milliseconds. * Set to 0 for no timeout. * * @default 0 * @since 1.0.0 */ timeout?: number;
/** * Whether to allow duplicate scan results. * * @default false * @since 1.0.0 */ allowDuplicates?: boolean;}ConnectOptions
「ConnectOptions」セクションデバイスに接続するためのオプション。
export interface ConnectOptions { /** * The device ID (MAC address on Android, UUID on iOS). * * @since 1.0.0 */ deviceId: string;
/** * Whether to automatically connect when the device becomes available. * * @default false * @since 1.0.0 */ autoConnect?: boolean;}DisconnectOptions
「DisconnectOptions」セクションデバイスから切断するためのオプション。
export interface DisconnectOptions { /** * The device ID to disconnect from. * * @since 1.0.0 */ deviceId: string;}CreateBondOptions
「CreateBondOptions」セクションボンドを作成するためのオプション。
export interface CreateBondOptions { /** * The device ID to bond with. * * @since 1.0.0 */ deviceId: string;}IsBondedOptions
セクション「IsBondedOptions」ボンディング状態を確認するためのオプション。
export interface IsBondedOptions { /** * The device ID to check. * * @since 1.0.0 */ deviceId: string;}IsBondedResult
セクション「IsBondedResult」isBonded メソッドの結果。
export interface IsBondedResult { /** * Whether the device is bonded. * * @since 1.0.0 */ bonded: boolean;}DiscoverServicesOptions
セクション「DiscoverServicesOptions」サービスの検出に関するオプション。
export interface DiscoverServicesOptions { /** * The device ID to discover services on. * * @since 1.0.0 */ deviceId: string;}AdapterStateChangedEvent
セクション「AdapterStateChangedEvent」Bluetooth アダプターがオンまたはオフに切り替わったときに発火するイベント。8.2.11 以降。
export interface AdapterStateChangedEvent { /** * Whether Bluetooth is enabled (adapter powered on). * * @since 8.2.11 */ enabled: boolean;}Source of Truth
「Source Of Truth」セクションこのページはプラグインの src/definitions.tsから生成されています。upstream で公開 API が変更されたら、再度 sync を実行してください。
Getting Startedの続きを進める
「Getting Started からの続き」セクション使用している場合 スタートガイド ダッシュボードと API の運用を計画するには、これを次と連携させます @capgo/capacitor-bluetooth-low-energy の使い方 @capgo/capacitor-bluetooth-low-energy でネイティブ機能を使うには、 API の概要 「API の概要」の実装の詳細については、 はじめに 「はじめに」における実装の詳細については、 API キー および「API Keys」の実装の詳細については、 デバイス 実装の詳細については Devices を参照してください。