欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Native.js获取监听开关等操作Android蓝牙设备实例代码

程序员文章站 2023-12-03 13:53:22
native.js开启关闭蓝牙 var main = plus.android.runtimemainactivity(); var context = plu...

native.js开启关闭蓝牙

var main = plus.android.runtimemainactivity();
var context = plus.android.importclass("android.content.context");
var bmanager = main.getsystemservice(context.bluetooth_service);
plus.android.importclass(bmanager);//引入相关的method函数
var badapter = bmanager.getadapter();
plus.android.importclass(badapter);//引入相关的method函数,这样之后才会有isenabled函数支持
if(!badapter.isenabled()) {
badapter.enable();
}

native.js监听蓝牙开关状态

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

native.js获取蓝牙设备列表

unction bluetooth_list(){
var main = plus.android.runtimemainactivity();
var context = plus.android.importclass("android.content.context");
var lists = badapter.getbondeddevices();
plus.android.importclass(lists);
var resultdiv = document.getelementbyid('bluetooth_list');
var iterator = lists.iterator();
plus.android.importclass(iterator);
while (iterator.hasnext()) {
var d = iterator.next();
plus.android.importclass(d);
console.log(d.getaddress());
}
}

native.js蓝牙连接票据打印机

测试机:佳博pt-280便携打印机
手机:华为低端
功能:扫描周围蓝牙设备加入列表,点击未配对设备,自动配对设备,点击已配对设备,进行打印测试

html页面代码

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title></title>
 <script src="js/pr.js"></script>
 </head>
 <body>
 <div>
 <p><input id="bt1" type="button" value="搜索设备" onclick="searchdevices('a')"></p>
 </div>
 <div>
 未配对蓝牙设备
 <ul id="list1">

 </ul>
 </div>

 <div>
 已配对蓝牙设备

 <ul id="list2">

 </ul>
 </div>
 </body>
</html>

js文件

//address=""搜索蓝牙//address=设备mac地址,自动配对给出mac地址的设备
function searchdevices(address) {
 //注册类
 var main = plus.android.runtimemainactivity();
 var intentfilter = plus.android.importclass("android.content.intentfilter");
 var bluetoothadapter = plus.android.importclass("android.bluetooth.bluetoothadapter");
 var bluetoothdevice = plus.android.importclass("android.bluetooth.bluetoothdevice");
 var badapter = bluetoothadapter.getdefaultadapter();
 console.log("开始搜索设备");
 var filter = new intentfilter();
 var bdevice = new bluetoothdevice();
 var on = null;
 var un = null;
 var vlist1 = document.getelementbyid("list1"); //注册容器用来显示未配对设备
 vlist1.innerhtml = ""; //清空容器
 var vlist2 = document.getelementbyid("list2"); //注册容器用来显示未配对设备
 vlist2.innerhtml = ""; //清空容器
 var button1 = document.getelementbyid("bt1");
 button1.disabled=true;
 button1.value="正在搜索请稍候";
 badapter.startdiscovery(); //开启搜索
 var receiver;
 receiver = plus.android.implements("io.dcloud.android.content.broadcastreceiver", {
 onreceive: function(context, intent) { //实现onreceiver回调函数
 plus.android.importclass(intent); //通过intent实例引入intent类,方便以后的‘.'操作
 console.log(intent.getaction()); //获取action
 if(intent.getaction() == "android.bluetooth.adapter.action.discovery_finished"){
 main.unregisterreceiver(receiver);//取消监听
 button1.disabled=false;
 button1.value="搜索设备";
 console.log("搜索结束");
 }else{
 bledevice = intent.getparcelableextra(bluetoothdevice.extra_device);
 //判断是否配对
 if (bledevice.getbondstate() == bdevice.bond_none) {
 console.log("未配对蓝牙设备:" + bledevice.getname() + " " + bledevice.getaddress());
 //参数如果跟取得的mac地址一样就配对
 if (address == bledevice.getaddress()) {
  if (bledevice.createbond()) { //配对命令.createbond()
  console.log("配对成功");
  var li2 = document.createelement("li"); //注册
  li2.setattribute("id", bledevice.getaddress()); //打印机mac地址
  li2.setattribute("onclick", "print(id)"); //注册click点击列表进行打印
  li2.innertext = bledevice.getname();
  vlist2.appendchild(li2);
  }

 } else {
  if(bledevice.getname() != on ){ //判断防止重复添加
  var li1 = document.createelement("li"); //注册
  li1.setattribute("id", bledevice.getaddress()); //打印机mac地址
  li1.setattribute("onclick", "searchdevices(id)"); //注册click点击列表进行配对
  on = bledevice.getname();
  li1.innertext = on;
  vlist1.appendchild(li1);

  }

 }
 } else {
 if(bledevice.getname() != un ){ //判断防止重复添加
 console.log("已配对蓝牙设备:" + bledevice.getname() + " " + bledevice.getaddress());
 var li2 = document.createelement("li"); //注册
 li2.setattribute("id", bledevice.getaddress()); //打印机mac地址
 li2.setattribute("onclick", "print(id)"); //注册click点击列表进行打印
 un = bledevice.getname(); 
 li2.innertext = un;
 vlist2.appendchild(li2);}
 }}
 }
 });

 filter.addaction(bdevice.action_found);
 filter.addaction(badapter.action_discovery_started);
 filter.addaction(badapter.action_discovery_finished);
 filter.addaction(badapter.action_state_changed);

 main.registerreceiver(receiver, filter); //注册监听
}

var device = null,
 badapter = null,
 bluetoothadapter = null,
 uuid = null,
 main = null,
 bluetoothsocket = null;

function print(mac_address) {
 if (!mac_address) {
 mui.toast("请选择蓝牙打印机");
 return;
 }

 main = plus.android.runtimemainactivity();
 bluetoothadapter = plus.android.importclass("android.bluetooth.bluetoothadapter");
 uuid = plus.android.importclass("java.util.uuid");
 uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");
 badapter = bluetoothadapter.getdefaultadapter();
 device = badapter.getremotedevice(mac_address);
 plus.android.importclass(device);
 bluetoothsocket = device.createinsecurerfcommsockettoservicerecord(uuid);
 plus.android.importclass(bluetoothsocket);

 if (!bluetoothsocket.isconnected()) {
 console.log("检测到设备未连接,尝试连接....");
 bluetoothsocket.connect();
 }

 console.log("设备已连接");

 if (bluetoothsocket.isconnected()) {
 var outputstream = bluetoothsocket.getoutputstream();
 plus.android.importclass(outputstream);
 var string = "打印测试\r\n";
 var bytes = plus.android.invoke(string, "getbytes", "gbk");
 outputstream.write(bytes);
 outputstream.flush();
 device = null //这里关键
 bluetoothsocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误

 }

}

以上是针对native.js对android蓝牙设备的操作实例代码,如果你想了解native.js的其它实例代码或原生的android蓝牙操作方法请查看下面的相关链接