Bluetooth

更新时间:
复制 MD 格式

This topic describes the JavaScript (JS) APIs for the WVBluetooth class. You can use these APIs to create H5 applications or miniapps with cross-platform DevOps. The WVBluetooth JS APIs provide Bluetooth capabilities, such as requesting Bluetooth permissions, scanning for Bluetooth Low Energy (BLE) devices, connecting to Bluetooth devices, and querying all services of a BLE device.

WVBluetooth.requestAuthorization

Requests Bluetooth permissions and enables Bluetooth.

Input parameters

  • There are no input parameters.

Callback parameters

Success callback parameters:

  • [object] value: A JSON object that contains the following key-value pair:

    • [string] state: 'poweredOn'.

Failure callback parameters:

  • [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

Scans for BLE devices.

Input parameters

  • None.

Callback parameters

Success callback parameters:

  • None.

Failure callback parameters:

  • [string] msg: The error message.

Event listener

WVBluetooth.Event.discoverDevice: An event that is triggered when a BLE device is discovered

Event parameters:

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

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

document.addEventListener('WVBluetooth.Event.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 scanning for BLE devices.

Input parameters

  • No input parameters are required.

Callback parameters

Success callback parameters:

  • None.

Failure callback parameters:

  • [string] msg: The error description.

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

WVBluetooth.connect

Connect to a Bluetooth device at the specified address.

Input parameters

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

Callback parameters

Success callback parameters:

  • This callback takes no parameters.

Parameters for the 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 from a Bluetooth device.

Parameters

  • There are no input parameters.

Callback parameters

Parameters for the success callback:

  • None.

Parameters for the failure callback:

  • [string] msg: Error message.

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

WVBluetooth.getServices

Starts the discovery of services on a BLE device.

Input parameters

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

Note

First, connect to the device.

Callback parameters

Parameters of the success callback:

  • [boolean] started: Indicates whether the discovery process has started. A value of `true` indicates that the process has started and is ongoing. A value of `false` indicates that the process failed to start.

The failure callback has the following parameters:

  • [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

Retrieves all the characteristics of a BLE device.

Input parameters

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

    Note

    You must connect to the device first.

  • [string] serviceId: The service ID in UUID format.

Callback parameters

The success callback receives the following parameters:

  • characteristics: A JSON array of objects. Each object contains the following key-value pair:

    • characteristicId: The UUID of the retrieved characteristic.

Parameters for the failure callback:

  • msg (string): 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 value of a specified characteristic to a connected BLE device.

Input parameters

  • [string] deviceId: The address of the Bluetooth device. Note: You must connect to the device first.

  • [string] serviceId: The UUID of the service.

  • [string] characteristicId: The UUID-formatted ID of the characteristic value.

  • [string] value: The value to write.

    Note

    The value must be Base64-encoded.

Callback parameters

Parameters for the success callback:

  • None.

Failure callback parameters:

  • [string] msg: The returned error message.

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 value of a specified characteristic from a connected BLE device.

Input parameters

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

    Note

    You must connect to the device first.

  • [string] serviceId: The UUID of the service.

  • [string] characteristicId: The UUID of the characteristic value.

Callback parameters

Parameters of the success callback:

  • None.

Parameters for the 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 notifications for changes to a characteristic's value.

Input parameters

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

    Note

    You must be connected to the device.

  • [string] serviceId: The UUID of the service.

  • [string] characteristicId: The UUID of the characteristic.

Callback parameters

Parameters for the success callback:

  • None.

Parameters for the failure callback:

  • [string] msg: A description of the error.

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 notifications of characteristic value changes.

Input parameters

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

    Note

    Ensure that you are connected to the device.

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

  • [string] characteristicId: The UUID of the characteristic value.

Callback parameters

Parameters of the success callback:

  • None.

Parameters for the failure callback:

  • msg (string): 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));
}