가이드
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 |
Characteristic 값을 읽습니다. |
writeCharacteristic |
Characteristic에 값을 씁니다. |
startCharacteristicNotifications |
Characteristic 알림을 시작합니다. |
stopCharacteristicNotifications |
characteristic의 알림을 중지합니다. |
readDescriptor |
descriptor 값을 읽습니다. |
writeDescriptor |
descriptor에 값을 씁니다. |
readRssi |
연결된 장치의 RSSI(신호 강도)를 읽습니다. |
requestMtu |
MTU 크기 변경을 요청합니다 (Android 전용). |
requestConnectionPriority |
연결 우선순위를 요청합니다 (Android 전용). |
startAdvertising |
Peripheral(BLE server)로 advertising을 시작합니다. |
stopAdvertising |
Advertising을 중지합니다. |
addGattService |
로컬 GATT 서버에 특성(characteristic)을 포함한 GATT 서비스를 추가합니다. 광고(advertising)를 시작하기 전, peripheral 모드에서 호출해야 합니다. |
removeGattService |
로컬 GATT 서버에서 GATT 서비스를 제거합니다. |
setGattCharacteristicValue |
로컬 GATT 특성(characteristic)의 값을 설정합니다. |
notifyGattCharacteristicChanged |
로컬 GATT 특성 값이 변경되었음을 연결된 central 기기에 알립니다. |
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.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개의 핵심 메서드가 나열되어 있습니다. Listener와 버전 헬퍼, 각 메서드의 전체 계약은 다음 문서에서 확인할 수 있습니다. 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의 구현 세부 사항은 다음과 같습니다. 시작하기 Getting Started의 구현 상세 내용은 Capgo 플러그인 디렉토리 Capgo Plugin Directory의 제품 워크플로우에 맞춰, Capgo의 Capacitor 플러그인 Capgo의 Capacitor Plugins 구현 세부 사항 및 플러그인 추가 또는 업데이트 플러그인 추가 또는 업데이트의 구현 세부 사항은.