文档

本文介绍蓝牙类WVBluetooth相关的JS API,供您在通过跨平台DevOps创建H5端应用或者小程序时参考。蓝牙类WVBluetooth相关JS API提供申请蓝牙权限、寻找蓝牙低功耗设备、连接蓝牙设备、查询低功耗蓝牙设备所有service等多个蓝牙相关能力。

WVBluetooth.requestAuthorization

申请蓝牙相关权限并打开蓝牙。

输入参数

  • 无输入参数。

回调参数

成功回调参数:

  • [object] value:value是一个JSON Object对象,其包含的key值以及对应的value值为:

    • [string] state:'poweredOn'。

失败回调参数:

  • [string] msg:错误信息。

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

WVBluetooth.scan

寻找蓝牙低功耗设备。

输入参数

  • 无输入参数。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

监听事件

WVBluetooth.Event.discoverDevice:发现蓝牙低功耗设备

事件参数:

  • [string] name:蓝牙设备的名称。

  • [string] devceId:蓝牙设备的地址。

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

停止寻找蓝牙低功耗设备。

输入参数

  • 无输入参数。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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

WVBluetooth.connect

连接指定蓝牙地址的蓝牙设备。

输入参数

  • [string] deviceId:要连接的蓝牙地址。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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

同已连接的蓝牙设备断开连接。

输入参数

  • 无输入参数。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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

WVBluetooth.getServices

开始查找低功耗蓝牙设备的服务。

输入参数

  • [string] deviceId:蓝牙设备的地址。

说明

需要提前和该设备建立连接。

回调参数

成功回调参数:

  • [boolean] started:查找工作是否开启,true代表已开启,正在查找中;false代表开启失败。

失败回调参数:

  • [string] msg:错误信息。

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

获取低功耗蓝牙设备的所有特征值。

输入参数

  • [string] deviceId:蓝牙设备的地址。

    说明

    需要提前和该设备建立连接。

  • [string] serviceId:服务ID,uuid格式。

回调参数

成功回调参数:

  • [object] characteristics:是一个JSONArray数组,数组里面的每个JSONObject的key值以及对应的value值为:

    • [string] characteristicId:获取到的characteristic的uuid。

失败回调参数:

  • [string] msg:错误信息。

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

向已连接的低功耗蓝牙设备写入指定的特征值。

输入参数

  • [string] deviceId:蓝牙设备的地址。注意,需要和该设备提前建立连接。

  • [string] serviceId:服务ID,uuid格式。

  • [string] characteristicId:特征值ID,uuid格式。

  • [string] value:要写入的值。

    说明

    value是经过Base64编码的值。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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

从已连接的低功耗蓝牙设备中读取指定特征值。

输入参数

  • [string] deviceId:蓝牙设备的地址。

    说明

    需要提前和该设备建立连接。

  • [string] serviceId:服务ID,uuid格式。

  • [string] characteristicId:特征值ID,uuid格式。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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

开启特征值变化通知。

输入参数

  • [string] deviceId:蓝牙设备的地址。

    说明

    需要提前和该设备建立连接。

  • [string] serviceId:服务ID,uuid格式。

  • [string] characteristicId:特征值ID,uuid格式。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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

关闭特征值变化通知。

输入参数

  • [string] deviceId:蓝牙设备的地址。

    说明

    需要和该设备提前建立连接。

  • [string] serviceId:服务ID,uuid格式。

  • [string] characteristicId:特征值ID,uuid格式。

回调参数

成功回调参数:

  • 无回调参数。

失败回调参数:

  • [string] msg:错误信息。

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));
}

  • 本页导读 (0)
文档反馈