Skip to main content
Use this API to stop discovering Bluetooth devices. Call this when you’ve found the device you need to save battery.

Parameters

PropertyTypeRequiredDescription
successFunctionNoCallback on success
failFunctionNoCallback on failure
completeFunctionNoCallback that always executes

Code Example

Basic Usage

my.stopBluetoothDevicesDiscovery({
  success() {
    console.log('Stopped scanning');
  }
});

Stop When Device Found

Page({
  targetDeviceName: 'MyDevice',

  startScan() {
    my.onBluetoothDeviceFound((res) => {
      const targetDevice = res.devices.find(
        d => d.name === this.targetDeviceName
      );

      if (targetDevice) {
        // Found target, stop scanning
        my.stopBluetoothDevicesDiscovery();
        this.connectToDevice(targetDevice);
      }
    });

    my.startBluetoothDevicesDiscovery();
  },

  connectToDevice(device) {
    console.log('Connecting to:', device.name);
    // Connect logic here
  }
});

Scan with Timeout

Page({
  scanTimeout: null,

  startTimedScan() {
    my.startBluetoothDevicesDiscovery({
      success: () => {
        // Auto-stop after 15 seconds
        this.scanTimeout = setTimeout(() => {
          my.stopBluetoothDevicesDiscovery();
          my.showToast({ content: 'Scan complete' });
        }, 15000);
      }
    });
  },

  stopScanEarly() {
    if (this.scanTimeout) {
      clearTimeout(this.scanTimeout);
    }
    my.stopBluetoothDevicesDiscovery();
  }
});

my.startBluetoothDevicesDiscovery

Start discovery

my.getBluetoothDevices

Get discovered devices