All Products
Search
Document Center

SuperApp:Bluetooth

Last Updated:Jan 21, 2025

This topic describes the JavaScript APIs of WVBluetooth. You can refer to this topic when you create HTML5 apps or Miniapps. You can use the JavaScript APIs of WVBluetooth to apply for Bluetooth permissions, search for Bluetooth Low Energy (BLE) devices, connect the device on which the app runs to a Bluetooth device, and query all services related to the BLE devices.

WVBluetooth.requestAuthorization

Applies for Bluetooth permissions and enables Bluetooth.

Input parameters

  • No input parameters exist.

Callback parameters

Parameters for a success callback:

  • [object]value: a JSON object. It contains the following key parameter and corresponding value of the key parameter:

    • [string]state:'poweredOn'.

Parameters for a failure callback:

  • [string]msg: the error message.

window.WindVane.call('WVBluetooth', 'requestAuthorization', {}, function(e) {
        alert('success: ' + JSON.stringify(e));
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
});

WVBluetooth.scan

Searches for BLE devices.

Input parameters

  • No input parameters exist.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

Event listening

WV.Event.WVBluetooth.discoverDevice: discovers BLE devices.

Event parameters:

  • [string]name: the name of the Bluetooth device.

  • [string]devceId: the address of the Bluetooth device.

document.addEventListener('WV.Event.WVBluetooth.discoverDevice', function (e) {
        alert('event discoverDevice: ' + JSON.stringify(e.param));
});

window.WindVane.call('WVBluetooth', 'scan', {}, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.stopScan

Stops searching for BLE devices.

Input parameters

  • No input parameters exist.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

window.WindVane.call('WVBluetooth', 'stopScan', {}, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.connect

Connects the device on which the app runs to the Bluetooth device with a specified Bluetooth address.

Input parameters

  • [string]deviceId: the address of the Bluetooth device to be connected.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

var params = {
  deviceId: '00:aa:bb:cc:dd'
};

window.WindVane.call('WVBluetooth', 'connect', params, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.disconnect

Disconnects the connected Bluetooth devices.

Input parameters

  • No input parameters exist.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

Event listening

WV.Event.WVBluetooth.GATTServerDisconnected:BLE lost connect

document.addEventListener('WV.Event.WVBluetooth.GATTServerDisconnected', function (e) {
        alert('event GATTServerDisconnected: ' + JSON.stringify(e.param));
});
window.WindVane.call('WVBluetooth', 'disconnect', {}, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.getServices

Queries the services related to BLE devices.

Input parameters

  • [string]deviceId: the address of the Bluetooth device.

Note

You must first connect the device on which the app runs to the Bluetooth device.

Callback parameters

Parameters for a failure callback:

  • [string]msg: the error message.

var params = {
  deviceId: '00:aa:bb:cc:dd'
};
window.WindVane.call('WVBluetooth', 'getServices', params, function(e) {
  			alert('success: ' + JSON.stringify(e));
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.getCharacteristics

Obtains all characteristic values of BLE devices.

Input parameters

  • [string]deviceId: the address of the Bluetooth device.

    Note

    You must first connect the device on which the app runs to the Bluetooth device.

  • [string]serviceId: the ID of the service. The ID must be in the UUID format.

Callback parameters

Parameters for a success callback:

  • [object]characteristics: a JSON array. Each JSON object in the array contains the following key parameter and corresponding value of the key parameter:

    • [string]characteristicId: the ID of the characteristic value. The ID must be in the UUID format.

Parameters for a failure callback:

  • [string]msg: the error message.

var params = {
  deviceId: '00:aa:bb:cc:dd', 
  serviceId: 'xxxx'
};
window.WindVane.call('WVBluetooth', 'getCharacteristics', params, function(e) {
  			alert('success: ' + JSON.stringify(e));
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.writeValue

Writes the specified characteristic value to the connected BLE devices.

Input parameters

  • [string]deviceId: the address of the Bluetooth device. Please note that you must first connect the device on which the app runs to the Bluetooth device.

  • [string]serviceId: the ID of the service. The ID must be in the UUID format.

  • [string]characteristicId: the ID of the characteristic value. The ID must be in the UUID format.

  • [string]value: the value to be written.

    Note

    The preceding string value is encoded by using Base64.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

Event listening

WV.Event.WVBluetooth.characteristicValueChanged: characteristicValue changes (require startNotifications to open notifications)

var params = {
  deviceId: '00:aa:bb:cc:dd', 
  serviceId: 'xxxx',
  characteristicId: 'xxx',
  value: 'xxx'
};
window.WindVane.call('WVBluetooth', 'writeValue', params, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.readValue

Reads the specified characteristic value from the connected BLE devices.

Input parameters

  • [string]deviceId: the address of the Bluetooth device.

    Note

    You must first connect the device on which the app runs to the Bluetooth device.

  • [string]serviceId: the ID of the service. The ID must be in the UUID format.

  • [string]characteristicId: the ID of the characteristic value. The ID must be in the UUID format.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

var params = {
  deviceId: '00:aa:bb:cc:dd', 
  serviceId: 'xxxx',
  characteristicId: 'xxx',
};
window.WindVane.call('WVBluetooth', 'readValue', params, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.startNotifications

Enables the notification of characteristic value changes.

Input parameters

  • [string]deviceId: the address of the Bluetooth device.

    Note

    You must first connect the device on which the app runs to the Bluetooth device.

  • [string]serviceId: the ID of the service. The ID must be in the UUID format.

  • [string]characteristicId: the ID of the characteristic value. The ID must be in the UUID format.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

var params = {
  deviceId: '00:aa:bb:cc:dd', 
  serviceId: 'xxxx',
  characteristicId: 'xxx',
};
window.WindVane.call('WVBluetooth', 'startNotifications', params, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}

WVBluetooth.stopNotifications

Disables the notification of characteristic value changes.

Input parameters

  • [string]deviceId: the address of the Bluetooth device.

    Note

    You must first connect the device on which the app runs to the Bluetooth device.

  • [string]serviceId: the ID of the service. The ID must be in the UUID format.

  • [string]characteristicId: the ID of the characteristic value. The ID must be in the UUID format.

Callback parameters

Parameters for a success callback:

  • No callback parameters exist.

Parameters for a failure callback:

  • [string]msg: the error message.

var params = {
  deviceId: '00:aa:bb:cc:dd', 
  serviceId: 'xxxx',
  characteristicId: 'xxx',
};
window.WindVane.call('WVBluetooth', 'stopNotifications', params, function(e) {
}, function(e) {
        alert('failure: ' + JSON.stringify(e));
}