Skip to main content
Use this API to start discovering nearby Bluetooth devices. Use my.onBluetoothDeviceFound to receive discovered devices.

Parameters

PropertyTypeRequiredDescription
servicesArrayNoFilter by service UUIDs
allowDuplicatesKeyBooleanNoAllow duplicate device reports (default: false)
intervalNumberNoReport interval in milliseconds
successFunctionNoCallback on success
failFunctionNoCallback on failure
completeFunctionNoCallback that always executes

Code Example

Basic Usage

my.startBluetoothDevicesDiscovery({
  success() {
    console.log('Started scanning for devices');
  }
});

Filter by Service UUID

my.startBluetoothDevicesDiscovery({
  services: ['FEE7'], // Only discover devices with this service
  success() {
    console.log('Scanning for specific devices...');
  }
});

Complete Discovery Flow

Page({
  data: {
    devices: []
  },

  startScan() {
    my.onBluetoothDeviceFound((res) => {
      const newDevices = res.devices.filter(
        d => !this.data.devices.find(existing => existing.deviceId === d.deviceId)
      );

      if (newDevices.length > 0) {
        this.setData({
          devices: [...this.data.devices, ...newDevices]
        });
      }
    });

    my.startBluetoothDevicesDiscovery({
      allowDuplicatesKey: false,
      success: () => {
        this.setData({ scanning: true });
        // Stop after 10 seconds
        setTimeout(() => this.stopScan(), 10000);
      }
    });
  },

  stopScan() {
    my.stopBluetoothDevicesDiscovery();
    this.setData({ scanning: false });
  }
});
Bluetooth discovery is power-intensive. Stop discovery with my.stopBluetoothDevicesDiscovery when you’ve found the device you need.

my.stopBluetoothDevicesDiscovery

Stop discovery

my.onBluetoothDeviceFound

Listen for devices