Panduan
Cara menggunakan Bluetooth Low Energy di Capacitor
Menggunakan @capgo/capacitor-bluetooth-low-energy
Plugin Capacitor Bluetooth Low Energy untuk komunikasi BLE.
Instal
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync
bunx cap sync menyalin code native plugin ke dalam proyek native Anda. Jalankan kembali setiap kali melakukan upgrade plugin.
Impor
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
Sekilas tentang API
| Metode | Deskripsi |
|---|---|
initialize |
Inisialisasi plugin BLE. Harus dipanggil sebelum metode lainnya. |
shimWebBluetooth |
Instal Capacitor Web Bluetooth shim pada navigator.bluetooth. Panggil ini secara manual sebelum menggunakan Web Bluetooth API dari aplikasi native Capacitor. |
isAvailable |
Cek apakah Bluetooth tersedia di perangkat. |
isEnabled |
Cek apakah Bluetooth aktif di perangkat. |
isLocationEnabled |
Cek apakah layanan lokasi aktif (khusus Android). |
openAppSettings |
Buka halaman pengaturan aplikasi. |
openBluetoothSettings |
Buka halaman pengaturan Bluetooth (khusus Android). |
openLocationSettings |
Buka halaman pengaturan lokasi (khusus Android). |
checkPermissions |
Periksa status izin saat ini. |
requestPermissions |
Minta izin Bluetooth. |
startScan |
Mulai scan perangkat BLE. |
stopScan |
Hentikan scan perangkat BLE. |
connect |
Hubungkan ke perangkat BLE. |
disconnect |
Putuskan koneksi dari perangkat BLE. |
createBond |
Buat bonding dengan perangkat BLE (khusus Android). |
isBonded |
Cek apakah perangkat sudah bonded (khusus Android). |
discoverServices |
Temukan service pada perangkat yang terhubung. |
getServices |
Ambil daftar service yang ditemukan untuk perangkat. |
getConnectedDevices |
Dapatkan daftar perangkat yang terhubung. |
readCharacteristic |
Baca nilai karakteristik. |
writeCharacteristic |
Tulis nilai ke karakteristik. |
startCharacteristicNotifications |
Aktifkan notifikasi untuk karakteristik. |
stopCharacteristicNotifications |
Hentikan notifikasi untuk sebuah characteristic. |
readDescriptor |
Baca nilai descriptor. |
writeDescriptor |
Tulis nilai ke sebuah descriptor. |
readRssi |
Baca RSSI (kekuatan sinyal) dari perangkat yang terhubung. |
requestMtu |
Minta perubahan ukuran MTU (Android saja). |
requestConnectionPriority |
Minta prioritas koneksi (Android saja). |
startAdvertising |
Mulai advertising sebagai peripheral (BLE server). |
stopAdvertising |
Hentikan advertising. |
addGattService |
Tambahkan layanan GATT dengan karakteristik ke server GATT lokal. Harus dipanggil dalam mode peripheral sebelum memulai advertising. |
removeGattService |
Hapus layanan GATT dari server GATT lokal. |
setGattCharacteristicValue |
Atur nilai karakteristik GATT lokal. |
notifyGattCharacteristicChanged |
Berikan notifikasi ke central yang terhubung bahwa nilai karakteristik GATT lokal telah berubah. |
startForegroundService |
Jalankan foreground service untuk menjaga koneksi BLE di background (khusus Android). |
stopForegroundService |
Hentikan foreground service (khusus Android). |
Contoh
initialize()
Inisialisasi plugin BLE. Harus dipanggil sebelum metode lainnya.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
shimWebBluetooth()
Instal Capacitor Web Bluetooth shim pada navigator.bluetooth. Panggil ini secara manual sebelum menggunakan Web Bluetooth API dari aplikasi native Capacitor.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();
isAvailable()
Cek apakah Bluetooth tersedia di perangkat.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();
isEnabled()
Cek apakah Bluetooth aktif di perangkat.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();
isLocationEnabled()
Cek apakah layanan lokasi aktif (khusus Android).
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isLocationEnabled();
openAppSettings()
Buka halaman pengaturan aplikasi.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.openAppSettings();
Tabel di atas mencantumkan 36 metode inti. Helper listener, versi, dan kontrak lengkap setiap metode didokumentasikan di repositori GitHub.
Mendengarkan event
addListener mengembalikan sebuah handle. Panggil handle.remove() saat layar di-unmount, atau BluetoothLowEnergy.removeAllListeners() untuk menghapus semua listener.
Referensi lengkap
Lanjutkan dari Menggunakan @capgo/capacitor-bluetooth-low-energy
Jika Anda menggunakan Menggunakan @capgo/capacitor-bluetooth-low-energy untuk merencanakan pekerjaan plugin native, hubungkan dengan @capgo/capacitor-bluetooth-low-energy untuk detail implementasi di @capgo/capacitor-bluetooth-low-energy, Panduan Memulai untuk detail implementasi di Getting Started, Direktori Plugin Capgo untuk alur kerja produk di Direktori Plugin Capgo, Plugin Capacitor oleh Capgo untuk detail implementasi di Plugin Capacitor oleh Capgo, dan Menambahkan atau Memperbarui Plugin untuk detail implementasi di bagian Menambahkan atau Memperbarui Plugin.