Panduan
Cara Menggunakan Bluetooth Low Energy di Capacitor
Menggunakan @capgo/capacitor-bluetooth-low-energy
Capacitor Plugin Bluetooth Low Energy untuk Komunikasi BLE.
Install
bun add @capgo/capacitor-bluetooth-low-energy
bunx cap sync
bunx cap sync Mengkopi plugin code native ke proyek native Anda. Jalankan lagi setelah setiap pembaruan plugin.
Import
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
Lihat API secara singkat
| Metode | Deskripsi |
|---|---|
initialize |
Inisialisasi plugin BLE. Harus dipanggil sebelum metode lainnya. |
shimWebBluetooth |
Pasang shim Web Bluetooth Capacitor di navigator.bluetoothPanggil secara manual sebelum menggunakan Web Bluetooth API dari aplikasi native Capacitor. |
isAvailable |
Cek apakah Bluetooth tersedia di perangkat. |
isEnabled |
Cek apakah Bluetooth diaktifkan di perangkat. |
isLocationEnabled |
Cek apakah layanan lokasi diaktifkan (hanya Android). |
openAppSettings |
Buka halaman pengaturan aplikasi. |
openBluetoothSettings |
Buka halaman pengaturan Bluetooth (hanya Android). |
openLocationSettings |
Buka halaman pengaturan lokasi (hanya Android). |
checkPermissions |
Periksa status izin saat ini. |
requestPermissions |
Minta izin Bluetooth. |
startScan |
Mulai mencari perangkat BLE. |
stopScan |
Hentikan mencari perangkat BLE. |
connect |
Nyalakan koneksi ke perangkat BLE. |
disconnect |
Putuskan koneksi dari perangkat BLE. |
createBond |
Membuat ikatan dengan perangkat BLE (hanya Android). |
isBonded |
Mengecek apakah perangkat terikat (hanya Android). |
discoverServices |
Mengidentifikasi layanan pada perangkat yang terhubung. |
getServices |
Mengambil layanan yang teridentifikasi untuk perangkat. |
getConnectedDevices |
Mengambil daftar perangkat yang terhubung. |
readCharacteristic |
Membaca nilai karakteristik. |
writeCharacteristic |
Mengirim nilai ke karakteristik. |
startCharacteristicNotifications |
Mengaktifkan notifikasi untuk karakteristik. |
stopCharacteristicNotifications |
Menghentikan notifikasi untuk karakteristik. |
readDescriptor |
Membaca nilai deskriptor. |
writeDescriptor |
Mengirim nilai ke deskriptor. |
readRssi |
Membaca RSSI (kuat sinyal) dari perangkat yang terhubung. |
requestMtu |
Ubah ukuran MTU (hanya untuk Android). |
requestConnectionPriority |
Minta prioritas koneksi (hanya untuk Android). |
startAdvertising |
Mulai iklan sebagai perangkat perifer (server BLE). |
stopAdvertising |
Hentikan iklan. |
addGattService |
Tambahkan layanan GATT dengan karakteristik ke server GATT lokal. Harus dipanggil dalam mode perifer sebelum memulai iklan. |
removeGattService |
Hapus layanan GATT dari server GATT lokal. |
setGattCharacteristicValue |
Set nilai karakteristik GATT lokal. |
notifyGattCharacteristicChanged |
Informasikan centrals terhubung bahwa nilai karakteristik GATT lokal berubah. |
startForegroundService |
Mulai layanan latar depan untuk menjaga koneksi BLE di latar belakang (hanya untuk Android). |
stopForegroundService |
Hentikan layanan latar depan (hanya untuk Android). |
Contoh
initialize()
Mulai plugin BLE. Harus dipanggil sebelum metode lainnya.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
await BluetoothLowEnergy.initialize({ mode: 'central' });
shimWebBluetooth()
Pasang shim Web Bluetooth Capacitor navigator.bluetoothGunakan fungsi ini secara manual sebelum menggunakan Bluetooth Web API dari sebuah aplikasi native Capacitor.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
BluetoothLowEnergy.shimWebBluetooth();
isAvailable()
Cek apakah Bluetooth tersedia pada perangkat.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { available } = await BluetoothLowEnergy.isAvailable();
isEnabled()
Cek apakah Bluetooth diaktifkan pada perangkat.
import { BluetoothLowEnergy } from '@capgo/capacitor-bluetooth-low-energy';
const { enabled } = await BluetoothLowEnergy.isEnabled();
isLocationEnabled()
Cek apakah layanan lokasi diaktifkan (hanya 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();
Daftar tabel di atas mencantumkan 36 metode inti. Bantuan pemanggil dan versi, serta kontrak lengkap dari setiap metode, dapat dilihat dalam repository GitHub.
Dengarkan event
addListener kembali handle. Panggil handle.remove() ketika layar dihapus, atau BluetoothLowEnergy.removeAllListeners() untuk menghapus setiap pemanggil.
Referensi lengkap
Teruskan dari Menggunakan @capgo/capacitor-bluetooth-low-energy
Jika Anda menggunakan Menggunakan @capgo/capacitor-bluetooth-low-energy untuk merencanakan pekerjaan plugin native, hubungkannya dengan @capgo/capacitor-bluetooth-low-energy untuk detail implementasi di @capgo/capacitor-bluetooth-low-energy, Mulai Menggunakan untuk detail implementasi di Mulai, Capgo Direktori Plugin untuk alur kerja produk di Capgo Direktori Plugin, Capacitor Plugin oleh Capgo untuk detail implementasi di Capacitor Plugin oleh Capgo, dan Menambahkan atau Mengupdate Plugin untuk detail implementasi di Menambahkan atau Mengupdate Plugin.