Comprehensive Bluetooth and Bluetooth Low Energy (BLE) capabilities for device discovery, connection management, and data exchange.
Control and monitor the device's Bluetooth adapter state.
/**
* Initialize the Bluetooth adapter
* @param options - Configuration options with callbacks
*/
function openBluetoothAdapter(options: OpenBluetoothAdapterOptions): void;
/**
* Close the Bluetooth adapter and clean up resources
* @param options - Options with callbacks
*/
function closeBluetoothAdapter(options?: CloseBluetoothAdapterOptions): void;
/**
* Get the current Bluetooth adapter state
* @param options - Options with callbacks
*/
function getBluetoothAdapterState(options: GetBluetoothAdapterStateOptions): void;
/**
* Listen for Bluetooth adapter state changes
* @param callback - Callback function for state changes
*/
function onBluetoothAdapterStateChange(callback: (result: BluetoothAdapterState) => void): void;
/**
* Remove Bluetooth adapter state change listener
* @param callback - Optional specific callback to remove
*/
function offBluetoothAdapterStateChange(callback?: Function): void;
interface OpenBluetoothAdapterOptions {
success?: (result: any) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface BluetoothAdapterState {
discovering: boolean;
available: boolean;
}
interface BluetoothError {
errCode: number;
errMsg: string;
}Discover and manage Bluetooth devices in the vicinity.
/**
* Start searching for Bluetooth devices
* @param options - Discovery configuration options
*/
function startBluetoothDevicesDiscovery(options: StartBluetoothDevicesDiscoveryOptions): void;
/**
* Stop searching for Bluetooth devices
* @param options - Options with callbacks
*/
function stopBluetoothDevicesDiscovery(options?: StopBluetoothDevicesDiscoveryOptions): void;
/**
* Get discovered Bluetooth devices
* @param options - Options with callbacks
*/
function getBluetoothDevices(options: GetBluetoothDevicesOptions): void;
/**
* Get connected Bluetooth devices
* @param options - Options with callbacks
*/
function getConnectedBluetoothDevices(options: GetConnectedBluetoothDevicesOptions): void;
/**
* Listen for newly discovered Bluetooth devices
* @param callback - Callback function for device discovery
*/
function onBluetoothDeviceFound(callback: (result: BluetoothDeviceFound) => void): void;
/**
* Remove device discovery listener
* @param callback - Optional specific callback to remove
*/
function offBluetoothDeviceFound(callback?: Function): void;
interface StartBluetoothDevicesDiscoveryOptions {
services?: string[];
allowDuplicatesKey?: boolean;
interval?: number;
success?: (result: any) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface BluetoothDevice {
name: string;
deviceId: string;
RSSI: number;
advertisData: ArrayBuffer;
advertisServiceUUIDs: string[];
localName: string;
serviceData: object;
}
interface BluetoothDeviceFound {
devices: BluetoothDevice[];
}Establish and manage Bluetooth Low Energy connections.
/**
* Create a BLE connection to a device
* @param options - Connection options with device ID
*/
function createBLEConnection(options: CreateBLEConnectionOptions): void;
/**
* Close a BLE connection
* @param options - Options with device ID
*/
function closeBLEConnection(options: CloseBLEConnectionOptions): void;
/**
* Listen for BLE connection state changes
* @param callback - Callback function for connection state changes
*/
function onBLEConnectionStateChange(callback: (result: BLEConnectionStateChange) => void): void;
/**
* Remove BLE connection state change listener
* @param callback - Optional specific callback to remove
*/
function offBLEConnectionStateChange(callback?: Function): void;
interface CreateBLEConnectionOptions {
deviceId: string;
timeout?: number;
success?: (result: any) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface BLEConnectionStateChange {
deviceId: string;
connected: boolean;
}Discover and interact with BLE services and characteristics.
/**
* Get BLE device services
* @param options - Options with device ID
*/
function getBLEDeviceServices(options: GetBLEDeviceServicesOptions): void;
/**
* Get characteristics for a BLE service
* @param options - Options with device and service IDs
*/
function getBLEDeviceCharacteristics(options: GetBLEDeviceCharacteristicsOptions): void;
/**
* Get RSSI for a connected BLE device
* @param options - Options with device ID
*/
function getBLEDeviceRSSI(options: GetBLEDeviceRSSIOptions): void;
/**
* Set MTU (Maximum Transmission Unit) for BLE device
* @param options - Options with device ID and MTU value
*/
function setBLEMTU(options: SetBLEMTUOptions): void;
interface GetBLEDeviceServicesOptions {
deviceId: string;
success?: (result: BLEDeviceServices) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface BLEService {
uuid: string;
isPrimary: boolean;
}
interface BLEDeviceServices {
services: BLEService[];
}
interface BLECharacteristic {
uuid: string;
properties: {
read: boolean;
write: boolean;
notify: boolean;
indicate: boolean;
};
}
interface GetBLEDeviceCharacteristicsOptions {
deviceId: string;
serviceId: string;
success?: (result: { characteristics: BLECharacteristic[] }) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}Read, write, and monitor BLE characteristic values.
/**
* Read BLE characteristic value
* @param options - Options with device, service, and characteristic IDs
*/
function readBLECharacteristicValue(options: ReadBLECharacteristicValueOptions): void;
/**
* Write BLE characteristic value
* @param options - Options with device, service, characteristic IDs and value
*/
function writeBLECharacteristicValue(options: WriteBLECharacteristicValueOptions): void;
/**
* Enable/disable notifications for BLE characteristic changes
* @param options - Options with device, service, characteristic IDs and state
*/
function notifyBLECharacteristicValueChange(options: NotifyBLECharacteristicValueChangeOptions): void;
/**
* Listen for BLE characteristic value changes
* @param callback - Callback function for value changes
*/
function onBLECharacteristicValueChange(callback: (result: BLECharacteristicValueChange) => void): void;
/**
* Remove BLE characteristic value change listener
* @param callback - Optional specific callback to remove
*/
function offBLECharacteristicValueChange(callback?: Function): void;
interface ReadBLECharacteristicValueOptions {
deviceId: string;
serviceId: string;
characteristicId: string;
success?: (result: { value: ArrayBuffer }) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface WriteBLECharacteristicValueOptions {
deviceId: string;
serviceId: string;
characteristicId: string;
value: ArrayBuffer;
success?: (result: any) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface NotifyBLECharacteristicValueChangeOptions {
deviceId: string;
serviceId: string;
characteristicId: string;
state: boolean;
success?: (result: any) => void;
fail?: (error: BluetoothError) => void;
complete?: () => void;
}
interface BLECharacteristicValueChange {
deviceId: string;
serviceId: string;
characteristicId: string;
value: ArrayBuffer;
}Discover and monitor iBeacon devices.
/**
* Start discovering iBeacon devices
* @param options - Discovery options with UUIDs
*/
function startBeaconDiscovery(options: StartBeaconDiscoveryOptions): void;
/**
* Stop discovering iBeacon devices
* @param options - Options with callbacks
*/
function stopBeaconDiscovery(options?: StopBeaconDiscoveryOptions): void;
/**
* Get discovered iBeacon devices
* @param options - Options with callbacks
*/
function getBeacons(options: GetBeaconsOptions): void;
/**
* Listen for iBeacon updates
* @param callback - Callback function for beacon updates
*/
function onBeaconUpdate(callback: (result: BeaconUpdate) => void): void;
/**
* Listen for iBeacon service changes
* @param callback - Callback function for service changes
*/
function onBeaconServiceChange(callback: (result: BeaconServiceChange) => void): void;
interface StartBeaconDiscoveryOptions {
uuids: string[];
success?: (result: any) => void;
fail?: (error: any) => void;
complete?: () => void;
}
interface Beacon {
uuid: string;
major: number;
minor: number;
proximity: number;
accuracy: number;
rssi: number;
}
interface BeaconUpdate {
beacons: Beacon[];
}
interface BeaconServiceChange {
available: boolean;
discovering: boolean;
}Usage Examples:
import uni from "@dcloudio/uni-app-plus";
// Initialize Bluetooth adapter
uni.openBluetoothAdapter({
success() {
console.log('Bluetooth adapter initialized');
// Start device discovery
uni.startBluetoothDevicesDiscovery({
success() {
console.log('Started device discovery');
}
});
},
fail(error) {
console.error('Failed to initialize:', error);
}
});
// Listen for discovered devices
uni.onBluetoothDeviceFound((result) => {
result.devices.forEach(device => {
console.log(`Found device: ${device.name} (${device.deviceId})`);
});
});
// Connect to a BLE device
uni.createBLEConnection({
deviceId: 'device-id-here',
success() {
console.log('BLE connection established');
// Get device services
uni.getBLEDeviceServices({
deviceId: 'device-id-here',
success(result) {
result.services.forEach(service => {
console.log(`Service: ${service.uuid}`);
});
}
});
}
});
// Read characteristic value
uni.readBLECharacteristicValue({
deviceId: 'device-id',
serviceId: 'service-uuid',
characteristicId: 'characteristic-uuid',
success(result) {
const dataView = new DataView(result.value);
console.log('Characteristic value:', dataView);
}
});Common Bluetooth error codes:
10000: Not initialized10001: Not available10002: No device found10003: Connection failed10004: No service found10005: No characteristic found10006: No connection10007: Property not support10008: System error10009: System not support10012: Operate time out10013: Invalid data