Bluetooth API overview

更新时间:
复制 MD 格式

Version requirement

Bluetooth

mPaaS version

Android/iOS

Bluetooth Low Energy (BLE)

mPaaS 10.1.60 or later versions

  • Android: 5.0 and later versions

  • iOS: No requirement

Traditional Bluetooth

mPaaS 10.1.60 or later versions

-

Process flow

BLE

BLE

Traditional Bluetooth

traditional

Bluetooth API

Bluetooth low energy

API

Description

my.connectBLEDevice

Connect to low energy bluetooth devices.

my.disconnectBLEDevice

Disconnect to low energy bluetooth devices.

my.getBLEDeviceCharacteristics

Get the characteristics of low energy Bluetooth devices.

my.getBLEDeviceServices

Get all the low energy Bluetooth devices that are discovered, including the connected devices.

my.notifyBLECharacteristicValueChange

Enable the function to notify changes to the characteristic value.

my.offBLECharacteristicValueChange

Disable the function to notify changes to the characteristic value.

my.offBLEConnectionStateChanged

Disable the listener for connection status change events.

my.onBLECharacteristicValueChange

Enable the listener for characteristic value change events.

my.onBLEConnectionStateChanged

Enable the listener for connection status change events, such as device lost and device disconnected.

my.readBLECharacteristicValue

Read the characteristic value.

my.writeBLECharacteristicValue

Write data to the characteristic value.

Traditional Bluetooth

API

Description

my.closeBluetoothAdapter

Close the Bluetooth module in the mini program.

my.getBluetoothAdapterState

Get the Bluetooth adapter status in the mini program.

my.getBluetoothDevices

Get all the low energy Bluetooth devices that are discovered, including the connected devices.

my.getConnectedBluetoothDevices

Get the Bluetooth devices that are connected.

my.offBluetoothAdapterStateChange

Remove the listener for connection status change events.

my.offBluetoothDeviceFound

Remove the listener for new Bluetooth device discovery events.

my.onBluetoothDeviceFound

Trigger the event when a new Bluetooth device is found.

my.onBluetoothAdapterStateChange

Monitor the Bluetooth status change events.

my.openBluetoothAdapter

Initialize the Bluetooth module in the mini program.

my.startBluetoothDevicesDiscovery

Start discovering Bluetooth devices nearby.

my.stopBluetoothDevicesDiscovery

Stop discovering Bluetooth devices nearby.

Sample code

//Initialize
my.openBluetoothAdapter({
  success: (res) => {
      console.log(res);
  }
});
//Register discovery event
my.onBluetoothDeviceFound({
  success: (res) => {
    let device = res.devices[0];
    //Connect the discovered device
    my.connectBLEDevice({
      deviceId: deviceId,
      success: (res) => {
        console.log(res)
      },
      fail:(res) => {
      },
      complete: (res)=>{
      }
    });
    //Stop discovering
    my.stopBluetoothDevicesDiscovery({
      success: (res) => {
        console.log(res)
      },
      fail:(res) => {
      },
      complete: (res)=>{
      }
    });
  }
});
//Register connection event
my.onBLEConnectionStateChanged({
  success: (res) => {
      console.log(res);
    if (res.connected) {
        //Start reading and writing notify and other operations
        my.notifyBLECharacteristicValueChange({
          deviceId: deviceId,
          serviceId: serviceId,
          characteristicId: characteristicId,
          success: (res) => {
              console.log(res)
          },
          fail:(res) => {
          },
          complete: (res)=>{
          }
        });
    }
  }
});
//Register the read or notify data reception events
my.onBLECharacteristicValueChange({
  success: (res) => {
      console.log(res);
  }
});
//Start discovering
my.startBluetoothDevicesDiscovery({
  services: ['fff0'],
  success: (res) => {
      console.log(res)
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});

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

// Unregister events
my.offBluetoothDeviceFound();
my.offBLEConnectionStateChanged();
my.offBLECharacteristicValueChange();

//Exit bluetooth module
my.closeBluetoothAdapter({
  success: (res) => {
  },
  fail:(res) => {
  },
  complete: (res)=>{
  }
});