Bluetooth模块用于管理蓝牙设备,搜索附近蓝牙设备、实现简单数据传输等。
支持搜索发现所有蓝牙设备,但仅支持低功耗蓝牙ble传输协议,不支持蓝牙设备的配对连接传输大量数据。
如果要连接非ble蓝牙设备,可以使用Native.js调用(请到http://ask.dcloud.net.cn搜索bluetooth相关问答)。
方法:
对象:
回调方法:
权限:
5+功能模块(permissions)
{
// ...
"permissions":{
// ...
"Bluetooth": {
"description": "Bluetooth"
}
}
}
关闭蓝牙模块
void plus.bluetooth.closeBluetoothAdapter(options);
说明:
断开所有已经建立的连接,释放系统资源,要求在蓝牙功能使用完成后调用(于openBluetoothAdapter成对使用)。
关闭成功后触发options参数中的success回调,失败触发options参数中的fail回调。
参数:
options参数为json类型,包含以下属性:
success:
fail:
complete:
调用成功或失败都会触发此回调。
返回值:
void
: 无
示例:
// 关闭蓝牙模块
function closeBluetoothAdapter(){
plus.bluetooth.closeBluetoothAdapter({
success:function(e){
console.log('close success: '+JSON.stringify(e));
},
fail:function(e){
console.log('close failed: '+JSON.stringify(e));
}
});
}
获取本机蓝牙适配器状态
void plus.bluetooth.getBluetoothAdapterState(options);
说明:
获取成功后触发options参数中的success回调,失败触发options参数中的fail回调。
参数:
options参数为json类型,包含以下属性:
success:
回调函数参数event对象包括以下属性:
discovering - Boolean类型,蓝牙适配器是否正在搜索设备;
available - Boolean类型,蓝牙适配器是否可用。
fail:
complete:
调用成功或失败都会触发此回调。
返回值:
void
: 无
示例:
// 获取蓝牙状态
function getBluetoothState(){
plus.bluetooth.getBluetoothAdapterState({
success:function(e){
console.log('state success: '+JSON.stringify(e));
},
fail:function(e){
console.log('state failed: '+JSON.stringify(e));
}
});
}
获取已搜索到的蓝牙设备
void plus.bluetooth.getBluetoothDevices(options);
说明:
包括已经和本机处于连接状态的设备。
获取成功后触发options参数中的success回调,失败触发options参数中的fail回调。
参数:
options参数为json类型,包含以下属性:
success:
回调函数参数event对象包括以下属性:
devices - Array,设备列表信息。
fail:
complete:
调用成功或失败都会触发此回调。
返回值:
void
: 无
示例:
// 获取已搜索到的蓝牙设备
function getBluetoothDevices(){
plus.bluetooth.getBluetoothDevices({
success:function(e){
var devices = e.devices;
console.log('get devices success: '+e.length);
for(var i in devices){
console.log(i+': '+JSON.stringify(devices[i]));
}
},
fail:function(e){
console.log('get devices failed: '+JSON.stringify(e));
}
});
}
根据uuid获取处于已连接的设备
void plus.bluetooth.getConnectedBluetoothDevices(options);
说明:
获取成功后触发options参数中的success回调,失败触发options参数中的fail回调。
参数:
options参数为json类型,包含以下属性:
services:
(
Array[
String
]
)
必选要获取设备的uuid列表
蓝牙设备主service的uuid列表。
success:
回调函数参数event对象包括以下属性:
devices - Array,设备列表信息(只包含name和deviceId属性)。
fail:
complete:
调用成功或失败都会触发此回调。
返回值:
void
: 无
示例:
// 获取已连接的蓝牙设备
function getConnectedDevices(){
plus.bluetooth.getConnectedBluetoothDevices({
success:function(e){
var devices = e.devices;
console.log('connected devices success: '+e.length);
for(var i in devices){
console.log(i+': '+JSON.stringify(devices[i]));
}
},
fail:function(e){
console.log('connected devices failed: '+JSON.stringify(e));
}
});
}
监听蓝牙适配器状态变化事件
void plus.bluetooth.onBluetoothAdapterStateChange(changeCB);
说明:
蓝牙适配器状态发生变化时触发回调。
参数:
changeCB:
回调函数参数event对象包括以下属性:
discovering - Boolean类型,蓝牙适配器是否正在搜索设备;
available - Boolean类型,蓝牙适配器是否可用。
返回值:
void
: 无
示例:
// 监听状态变化
function listenerStateChange(){
plus.bluetooth.onBluetoothAdapterStateChange(function(e){
console.log('state changed: '+JSON.stringify(e));
});
}
监听搜索到新设备的事件
void plus.bluetooth.onBluetoothDeviceFound(callback);
说明:
搜索到新设备时触发回调。
参数:
callback:
回调函数参数event对象包括以下属性:
devices - Array,设备列表信息。
返回值:
void
: 无
示例:
// 监听发现新设备
function listenerDeviceFound(){
plus.bluetooth.onBluetoothDeviceFound(function(e){
var devices = e.devices;
console.log('device found: '+e.length);
for(var i in devices){
console.log(i+': '+JSON.stringify(devices[i]));
}
});
}
初始化蓝牙模块
void plus.bluetooth.openBluetoothAdapter(options);
说明:
初始化成功后触发options参数中的success回调,失败触发options参数中的fail回调。
参数:
options参数为json类型,包含以下属性:
comple