Langsung ke konten utama
@capgo/capacitor-bluetooth-low-energy API Perangkat Open source

Bluetooth Low Energy plugin Capacitor

Plugin BLE lengkap untuk scanning, koneksi, membaca, menulis, dan menerima notifikasi dari perangkat Bluetooth

Instal

bun add @capgo/capacitor-bluetooth-low-energy bunx cap sync
npm, pnpm atau yarn
  • npm install @capgo/capacitor-bluetooth-low-energy
  • pnpm add @capgo/capacitor-bluetooth-low-energy
  • yarn add @capgo/capacitor-bluetooth-low-energy

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.

FAQ

FAQ plugin Bluetooth Low Energy

Bagaimana cara install plugin Bluetooth Low Energy di aplikasi Capacitor?

Jalankan "bun add @capgo/capacitor-bluetooth-low-energy" (atau "npm install @capgo/capacitor-bluetooth-low-energy"), lalu jalankan "bunx cap sync" agar proyek iOS dan Android mengenali code native. Import dari "@capgo/capacitor-bluetooth-low-energy" di code aplikasi Anda.

Apakah @capgo/capacitor-bluetooth-low-energy bisa digunakan dengan React, Vue, dan Angular?

Ya. Bluetooth Low Energy adalah package Capacitor, jadi bisa digunakan dengan framework web apa pun yang berjalan di dalam Capacitor, termasuk Ionic, React, Vue, Angular, Svelte, dan JavaScript biasa.

Versi Capacitor mana yang didukung @capgo/capacitor-bluetooth-low-energy?

Plugin Capgo mengikuti versi mayor Capacitor: gunakan versi mayor plugin yang sesuai dengan versi mayor Capacitor Anda (contoh: plugin v8 dengan Capacitor 8). Tabel kompatibilitas di README GitHub mencantumkan versi yang masih di-maintain.

Apakah @capgo/capacitor-bluetooth-low-energy gratis dan open source?

Ya. Source code tersedia publik di GitHub pada https://github.com/Cap-go/capacitor-bluetooth-low-energy/ dan package ini gratis diinstal dari npm. Bug report dan pull request sangat kami terima.

Bisakah saya update code yang menggunakan Bluetooth Low Energy tanpa review App Store?

Instalasi atau upgrade plugin akan mengubah native code, jadi butuh build store baru. Setelah itu, perubahan JavaScript, HTML, dan CSS yang memanggil plugin bisa langsung dikirim lewat live updates Capgo.

Rilis perubahan Bluetooth Low Energy tanpa menunggu review aplikasi

Setelah plugin ada di build store Anda, live updates Capgo akan mengirim perubahan JavaScript, HTML, dan CSS ke pengguna dalam hitungan menit.

Mulai dengan Capgo