html5页面初始化调用接口,HTML5+ API Reference

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值