Skip to main content
Use this API to initialize the Bluetooth adapter. This must be called before using any other Bluetooth APIs.

Parameters

PropertyTypeRequiredDescription
successFunctionNoCallback on success
failFunctionNoCallback on failure
completeFunctionNoCallback that always executes

Code Example

Basic Usage

my.openBluetoothAdapter({
  success() {
    console.log('Bluetooth adapter initialized');
  },
  fail(err) {
    console.error('Failed to initialize Bluetooth:', err);
  }
});

Check Bluetooth Before Operations

Page({
  onLoad() {
    this.initBluetooth();
  },

  initBluetooth() {
    my.openBluetoothAdapter({
      success: () => {
        console.log('Bluetooth ready');
        this.setData({ bluetoothReady: true });
      },
      fail: (err) => {
        if (err.error === 10001) {
          my.alert({
            title: 'Bluetooth Required',
            content: 'Please enable Bluetooth to use this feature.'
          });
        }
      }
    });
  },

  onUnload() {
    my.closeBluetoothAdapter();
  }
});

Error Codes

CodeDescription
10001Bluetooth is not enabled on the device
10000Bluetooth adapter not initialized
Always call my.closeBluetoothAdapter when you’re done using Bluetooth to release system resources.

my.closeBluetoothAdapter

Close Bluetooth adapter

my.startBluetoothDevicesDiscovery

Start device discovery