var main = plus.android.runtimeMainActivity();
var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
var BAdapter = new BluetoothAdapter.getDefaultAdapter();
var resultDiv = document.getElementById('output');
var receiver=plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function(context, intent) { //实现onReceiver回调函数
plus.android.importClass(intent);
console.log(intent.getAction());
resultDiv.textContent += '\nAction :' + intent.getAction();
main.unregisterReceiver(receiver);
}
});
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
var filter = new IntentFilter();
filter.addAction(BAdapter.ACTION_STATE_CHANGED); //监听蓝牙开关
main.registerReceiver(receiver, filter); //注册监听
if (!BAdapter.isEnabled()) {
BAdapter.enable(); //启动蓝牙
}else{
BAdapter.disable();
}
代码已测试过了ctrl+c直接使用
更新:5+已提供蓝牙,参考http://www.html5plus.org/doc/zh_cn/bluetooth.html 注意需要最新版HBuilderX
18 个评论
要回复文章请先登录或注册
mosowe
z***@qq.com
糖糖男儿
太2真人
DCloud_heavensoft
妙妙
韩跑跑
小伙灬
s***@qq.com
y***@126.com