본문 바로가기

시작하기

GitHub

AI-Assisted Setup으로 플러그인을 설치할 수 있습니다. 다음 명령어로 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-energy
npx cap sync
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();

자주 하는 실수

“흔한 실수” 섹션
  • 권한: 호출 checkPermissions() 그다음 requestPermissions() 필요할 때.
  • 리스너: 화면이 언마운트될 때 핸들을 제거합니다.
  • 한 번만 초기화: 앱 부트스트랩 시 setup을 실행하세요.

BLE 플러그인을 초기화합니다. 다른 모든 메서드보다 먼저 호출해야 합니다. 단, 예외는 addListener(). Register adapterStateChanged 호출하기 전에 initialize() 초기 iOS 상태 이벤트를 놓치지 않도록 하기 위함입니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });

Capacitor Web Bluetooth shim 설치 navigator.bluetooth. Capacitor 네이티브 앱에서 Web Bluetooth API를 사용하기 전에 이 메서드를 수동으로 호출하세요.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();

기기에서 Bluetooth를 사용할 수 있는지 확인합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();

기기의 Bluetooth 활성화 여부를 확인합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();

위치 서비스 활성화 여부를 확인합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();

앱 설정 페이지를 엽니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();

Bluetooth 설정 페이지를 엽니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openBluetoothSettings();

위치 설정 페이지를 엽니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openLocationSettings();

현재 권한 상태를 확인합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.checkPermissions();

Bluetooth 권한을 요청합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bluetooth, location } = await BluetoothLowEnergy.requestPermissions();

BLE 디바이스 스캔 시작.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startScan({
services: ['180D'], // Heart Rate Service
timeout: 10000
});

BLE 디바이스 스캔 중지.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopScan();

BLE 디바이스에 연결.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.connect({ deviceId: 'AA:BB:CC:DD:EE:FF' });

BLE 디바이스 연결 해제.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.disconnect({ deviceId: 'AA:BB:CC:DD:EE:FF' });

BLE 기기와 본딩합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.createBond({ deviceId: 'AA:BB:CC:DD:EE:FF' });

기기가 본딩되었는지 확인합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { bonded } = await BluetoothLowEnergy.isBonded({ deviceId: 'AA:BB:CC:DD:EE:FF' });

연결된 기기에서 서비스를 검색합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.discoverServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });

기기에서 검색된 서비스 목록을 가져옵니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { services } = await BluetoothLowEnergy.getServices({ deviceId: 'AA:BB:CC:DD:EE:FF' });

연결된 디바이스 목록을 가져옵니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { devices } = await BluetoothLowEnergy.getConnectedDevices();

Characteristic 값을 읽습니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { value } = await BluetoothLowEnergy.readCharacteristic({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37'
});

Characteristic에 값을 씁니다.

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” 섹션

Characteristic 알림을 시작합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startCharacteristicNotifications({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37'
});

stopCharacteristicNotifications

“stopCharacteristicNotifications” 섹션

characteristic의 알림을 중지합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopCharacteristicNotifications({
deviceId: 'AA:BB:CC:DD:EE:FF',
service: '180D',
characteristic: '2A37'
});

descriptor 값을 읽습니다.

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'
});

descriptor에 값을 씁니다.

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]
});

연결된 장치의 RSSI(신호 강도)를 읽습니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { rssi } = await BluetoothLowEnergy.readRssi({ deviceId: 'AA:BB:CC:DD:EE:FF' });

MTU 크기 변경을 요청합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { mtu } = await BluetoothLowEnergy.requestMtu({
deviceId: 'AA:BB:CC:DD:EE:FF',
mtu: 512
});

연결 우선순위를 요청합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.requestConnectionPriority({
deviceId: 'AA:BB:CC:DD:EE:FF',
priority: 'high'
});

Peripheral(BLE server)로 advertising을 시작합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startAdvertising({
name: 'MyDevice',
services: ['180D']
});

Advertising을 중지합니다.

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopAdvertising();

백그라운드에서 BLE 연결을 유지하기 위해 포그라운드 서비스를 시작합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.startForegroundService({
title: 'BLE Connection',
body: 'Maintaining connection...'
});

포그라운드 서비스를 중지합니다 (Android 전용).

import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.stopForegroundService();

addListener('adapterStateChanged')

addListener('adapterStateChanged') 섹션

블루투스 어댑터 전원 변경을 수신합니다. 이 이벤트는 iOS와 Android에서 발생하며, 웹에서는 발생하지 않습니다. 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 모드에서 소스는 central manager입니다. peripheral 모드에서 소스는 peripheral manager입니다(central manager 콜백은 발생하지 않음). initialize() 중 매니저를 생성하면 현재 상태도 보고합니다. 이 초기 상태 콜백은 initialize() 가 해결되기 전에 발생할 수 있으며, 플러그인은 이벤트를 유지하지 않으므로 나중에 등록한 리스너는 이를 놓치고 이후 변경 사항만 수신할 수 있습니다. initialize()호출 전에 리스너를 등록하세요. iOS는 매니저가 enabled: false 그다음, enabled: true 이 아닌 다른 상태로 시작하여 나중에 켜진 상태가 되면 .poweredOn 을 발생시킬 수 있습니다. initialize() 가 해결된 후 isEnabled() 를 호출하여 현재 어댑터 상태를 읽으세요.

블루투스가 꺼지면 플러그인은 보유 중이던 로컬 GATT 서비스 및 특성 레코드를 해제하고, 각 추적 중인 central에 대해 centralDisconnected 을 발생시킨 다음 adapterStateChanged 포함 enabled: false. Android에서는 GATT 서버도 닫고 advertise 콜백을 초기화합니다. 이 플러그인은 서비스나 광고를 자동으로 복원하지 않습니다.

앱이 peripheral인 경우, 다음을 기다리세요 adapterStateChanged 포함 enabled: true, 그런 다음 호출 addGattService 각 서비스에 대해 다시 실행하고 startAdvertising 다시 실행하세요. 플러그인이 로컬 기록을 초기화했기 때문에, 동일한 서비스 UUID를 추가해도 “Service already exists” 오류로 거부되지 않고 허용됩니다. Android에서는 어댑터가 복구되면 scanner(central 모드) 또는 advertiser(peripheral 모드)가 자동으로 다시 확보됩니다. 서비스 추가와 광고 시작은 여전히 직접 수행해야 합니다.

타입 레퍼런스

“타입 참조” 섹션

플러그인 초기화 옵션입니다.

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';
}

isAvailable 메서드의 결과값입니다.

export interface IsAvailableResult {
/**
* Whether Bluetooth is available on the device.
*
* @since 1.0.0
*/
available: boolean;
}

isEnabled 메서드의 결과값입니다.

export interface IsEnabledResult {
/**
* Whether Bluetooth is enabled on the device.
*
* @since 1.0.0
*/
enabled: boolean;
}

isLocationEnabled 메서드의 결과값입니다.

export interface IsLocationEnabledResult {
/**
* Whether location services are enabled on the device.
*
* @since 1.0.0
*/
enabled: boolean;
}

Bluetooth 및 위치 권한 상태입니다.

export interface PermissionStatus {
/**
* Bluetooth permission status.
*
* @since 1.0.0
*/
bluetooth: PermissionState;
/**
* Location permission status (Android only).
*
* @since 1.0.0
*/
location: PermissionState;
}

스캔 시작 시 사용하는 옵션입니다.

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;
}

디바이스 연결 시 사용하는 옵션입니다.

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;
}

디바이스 연결 해제 시 사용하는 옵션입니다.

export interface DisconnectOptions {
/**
* The device ID to disconnect from.
*
* @since 1.0.0
*/
deviceId: string;
}

본딩 생성 시 사용하는 옵션입니다.

export interface CreateBondOptions {
/**
* The device ID to bond with.
*
* @since 1.0.0
*/
deviceId: string;
}

본딩 상태를 확인하는 옵션입니다.

export interface IsBondedOptions {
/**
* The device ID to check.
*
* @since 1.0.0
*/
deviceId: string;
}

isBonded 메서드의 결과값입니다.

export interface IsBondedResult {
/**
* Whether the device is bonded.
*
* @since 1.0.0
*/
bonded: boolean;
}

서비스 검색 옵션입니다.

export interface DiscoverServicesOptions {
/**
* The device ID to discover services on.
*
* @since 1.0.0
*/
deviceId: string;
}

블루투스 어댑터가 켜지거나 꺼질 때 발생하는 이벤트입니다. 8.2.11부터 지원됩니다.

export interface AdapterStateChangedEvent {
/**
* Whether Bluetooth is enabled (adapter powered on).
*
* @since 8.2.11
*/
enabled: boolean;
}

이 페이지는 플러그인의 src/definitions.tsupstream에서 public API가 변경되면 sync를 다시 실행하세요.

사용 중이라면 시작하기 대시보드 및 API 운영을 계획하려면, 이를 다음과 연결하세요 @capgo/capacitor-bluetooth-low-energy 사용하기 @capgo/capacitor-bluetooth-low-energy 사용하기의 네이티브 기능을 위한, API 개요 구현 세부 사항은 API 개요에서 소개 구현 세부 사항은 소개에서 API 키 API Keys의 구현 세부 사항은, 기기 Devices의 구현 세부 사항은.