All Products
Search
Document Center

Mobile Platform as a Service:Ikhtisar API Bluetooth

Last Updated:Jul 02, 2025

Persyaratan versi

Bluetooth

Versi mPaaS

Android/iOS

Bluetooth Low Energy (BLE)

mPaaS 10.1.60 atau versi lebih baru

  • Android: 5.0 dan versi lebih baru

  • iOS: Tidak ada persyaratan

Bluetooth Tradisional

mPaaS 10.1.60 atau versi lebih baru

-

Alur proses

BLE

BLE

Bluetooth Tradisional

traditional

API Bluetooth

Bluetooth low energy

API

Deskripsi

my.connectBLEDevice

Menghubungkan ke perangkat bluetooth energi rendah.

my.disconnectBLEDevice

Memutuskan koneksi dari perangkat bluetooth energi rendah.

my.getBLEDeviceCharacteristics

Mendapatkan karakteristik perangkat Bluetooth energi rendah.

my.getBLEDeviceServices

Mendapatkan semua perangkat Bluetooth energi rendah yang ditemukan, termasuk perangkat yang terhubung.

my.notifyBLECharacteristicValueChange

Mengaktifkan fungsi untuk memberitahukan perubahan nilai karakteristik.

my.offBLECharacteristicValueChange

Menonaktifkan fungsi untuk memberitahukan perubahan nilai karakteristik.

my.offBLEConnectionStateChanged

Menonaktifkan pendengar untuk peristiwa perubahan status koneksi.

my.onBLECharacteristicValueChange

Mengaktifkan pendengar untuk peristiwa perubahan nilai karakteristik.

my.onBLEConnectionStateChanged

Mengaktifkan pendengar untuk peristiwa perubahan status koneksi, seperti perangkat hilang dan perangkat terputus.

my.readBLECharacteristicValue

Membaca nilai karakteristik.

my.writeBLECharacteristicValue

Menulis data ke nilai karakteristik.

Bluetooth Tradisional

API

Deskripsi

my.closeBluetoothAdapter

Menutup modul Bluetooth di mini program.

my.getBluetoothAdapterState

Mendapatkan status adapter Bluetooth di mini program.

my.getBluetoothDevices

Mendapatkan semua perangkat Bluetooth energi rendah yang ditemukan, termasuk perangkat yang terhubung.

my.getConnectedBluetoothDevices

Mendapatkan perangkat Bluetooth yang terhubung.

my.offBluetoothAdapterStateChange

Menghapus pendengar untuk peristiwa perubahan status koneksi.

my.offBluetoothDeviceFound

Menghapus pendengar untuk peristiwa penemuan perangkat Bluetooth baru.

my.onBluetoothDeviceFound

Memicu peristiwa ketika perangkat Bluetooth baru ditemukan.

my.onBluetoothAdapterStateChange

Memantau peristiwa perubahan status Bluetooth.

my.openBluetoothAdapter

Menginisialisasi modul Bluetooth di mini program.

my.startBluetoothDevicesDiscovery

Memulai penemuan perangkat Bluetooth di sekitar.

my.stopBluetoothDevicesDiscovery

Menghentikan penemuan perangkat Bluetooth di sekitar.

Kode contoh

//Inisialisasi
my.openBluetoothAdapter({
  success: (res) => {
      console.log(res);
  }
});
//Daftarkan peristiwa penemuan
my.onBluetoothDeviceFound({
  success: (res) => {
    let device = res.devices[0];
    //Hubungkan perangkat yang ditemukan
    my.connectBLEDevice({
      deviceId: deviceId,
      success: (res) => {
        console.log(res)
      },
      fail:(res) => {
      },
      complete: (res)=>{
      }
    });
    //Hentikan penemuan
    my.stopBluetoothDevicesDiscovery({
      success: (res) => {
        console.log(res)
      },
      fail:(res) => {
      },
      complete: (res)=>{
      }
    });
  }
});
//Daftarkan peristiwa koneksi
my.onBLEConnectionStateChanged({
  success: (res) => {
      console.log(res);
    if (res.connected) {
        //Mulai membaca, menulis, memberitahukan, dan operasi lainnya
        my.notifyBLECharacteristicValueChange({
          deviceId: deviceId,
          serviceId: serviceId,
          characteristicId: characteristicId,
          success: (res) => {
              console.log(res)
          },
          fail:(res) => {
          },
          complete: (res)=>{
          }
        });
    }
  }
});
//Daftarkan peristiwa penerimaan data baca atau notifikasi
my.onBLECharacteristicValueChange({
  success: (res) => {
      console.log(res);
  }
});
//Mulai penemuan
my.startBluetoothDevicesDiscovery({
  services: ['fff0'],
  success: (res) => {
      console.log(res)
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});

//Putuskan koneksi
my.disconnectBLEDevice({
  deviceId: deviceId,
  success: (res) => {
      console.log(res)
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});

//Batalkan pendaftaran peristiwa
my.offBluetoothDeviceFound();
my.offBLEConnectionStateChanged();
my.offBLECharacteristicValueChange();

//Keluar dari modul bluetooth
my.closeBluetoothAdapter({
  success: (res) => {
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});