Native.js開(kāi)啟關(guān)閉藍(lán)牙
創(chuàng)新互聯(lián)成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)公司、成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)站定制、網(wǎng)絡(luò)營(yíng)銷推廣、小程序制作、微信公眾號(hào)開(kāi)發(fā)、成都網(wǎng)站營(yíng)銷服務(wù),提供專業(yè)營(yíng)銷思路、內(nèi)容策劃、視覺(jué)設(shè)計(jì)、程序開(kāi)發(fā)來(lái)完成項(xiàng)目落地,為混凝土攪拌罐企業(yè)提供源源不斷的流量和訂單咨詢。
var main = plus.android.runtimeMainActivity(); var Context = plus.android.importClass("android.content.Context"); var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE); plus.android.importClass(BManager);//引入相關(guān)的method函數(shù) var BAdapter = BManager.getAdapter(); plus.android.importClass(BAdapter);//引入相關(guān)的method函數(shù),這樣之后才會(huì)有isEnabled函數(shù)支持 if(!BAdapter.isEnabled()) { BAdapter.enable(); }
Native.js監(jiān)聽(tīng)藍(lán)牙開(kāi)關(guān)狀態(tài)
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) { //實(shí)現(xiàn)onReceiver回調(diào)函數(shù) 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); //監(jiān)聽(tīng)藍(lán)牙開(kāi)關(guān) main.registerReceiver(receiver, filter); //注冊(cè)監(jiān)聽(tīng) if (!BAdapter.isEnabled()) { BAdapter.enable(); //啟動(dòng)藍(lán)牙 }else{ BAdapter.disable(); }
Native.js獲取藍(lán)牙設(shè)備列表
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藍(lán)牙連接票據(jù)打印機(jī)
測(cè)試機(jī):佳博PT-280便攜打印機(jī)
手機(jī):華為低端
功能:掃描周圍藍(lán)牙設(shè)備加入列表,點(diǎn)擊未配對(duì)設(shè)備,自動(dòng)配對(duì)設(shè)備,點(diǎn)擊已配對(duì)設(shè)備,進(jìn)行打印測(cè)試
html頁(yè)面代碼
<!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="搜索設(shè)備" onclick="searchDevices('a')"></p> </div> <div> 未配對(duì)藍(lán)牙設(shè)備 <ul id="list1"> </ul> </div> <div> 已配對(duì)藍(lán)牙設(shè)備 <ul id="list2"> </ul> </div> </body> </html>
Js文件
//address=""搜索藍(lán)牙//address=設(shè)備mac地址,自動(dòng)配對(duì)給出mac地址的設(shè)備 function searchDevices(address) { //注冊(cè)類 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("開(kāi)始搜索設(shè)備"); var filter = new IntentFilter(); var bdevice = new BluetoothDevice(); var on = null; var un = null; var vlist1 = document.getElementById("list1"); //注冊(cè)容器用來(lái)顯示未配對(duì)設(shè)備 vlist1.innerHTML = ""; //清空容器 var vlist2 = document.getElementById("list2"); //注冊(cè)容器用來(lái)顯示未配對(duì)設(shè)備 vlist2.innerHTML = ""; //清空容器 var button1 = document.getElementById("bt1"); button1.disabled=true; button1.value="正在搜索請(qǐng)稍候"; BAdapter.startDiscovery(); //開(kāi)啟搜索 var receiver; receiver = plus.android.implements("io.dcloud.android.content.BroadcastReceiver", { onReceive: function(context, intent) { //實(shí)現(xiàn)onReceiver回調(diào)函數(shù) plus.android.importClass(intent); //通過(guò)intent實(shí)例引入intent類,方便以后的‘.'操作 console.log(intent.getAction()); //獲取action if(intent.getAction() == "android.bluetooth.adapter.action.DISCOVERY_FINISHED"){ main.unregisterReceiver(receiver);//取消監(jiān)聽(tīng) button1.disabled=false; button1.value="搜索設(shè)備"; console.log("搜索結(jié)束"); }else{ BleDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //判斷是否配對(duì) if (BleDevice.getBondState() == bdevice.BOND_NONE) { console.log("未配對(duì)藍(lán)牙設(shè)備:" + BleDevice.getName() + " " + BleDevice.getAddress()); //參數(shù)如果跟取得的mac地址一樣就配對(duì) if (address == BleDevice.getAddress()) { if (BleDevice.createBond()) { //配對(duì)命令.createBond() console.log("配對(duì)成功"); var li2 = document.createElement("li"); //注冊(cè) li2.setAttribute("id", BleDevice.getAddress()); //打印機(jī)mac地址 li2.setAttribute("onclick", "print(id)"); //注冊(cè)click點(diǎn)擊列表進(jìn)行打印 li2.innerText = BleDevice.getName(); vlist2.appendChild(li2); } } else { if(BleDevice.getName() != on ){ //判斷防止重復(fù)添加 var li1 = document.createElement("li"); //注冊(cè) li1.setAttribute("id", BleDevice.getAddress()); //打印機(jī)mac地址 li1.setAttribute("onclick", "searchDevices(id)"); //注冊(cè)click點(diǎn)擊列表進(jìn)行配對(duì) on = BleDevice.getName(); li1.innerText = on; vlist1.appendChild(li1); } } } else { if(BleDevice.getName() != un ){ //判斷防止重復(fù)添加 console.log("已配對(duì)藍(lán)牙設(shè)備:" + BleDevice.getName() + " " + BleDevice.getAddress()); var li2 = document.createElement("li"); //注冊(cè) li2.setAttribute("id", BleDevice.getAddress()); //打印機(jī)mac地址 li2.setAttribute("onclick", "print(id)"); //注冊(cè)click點(diǎn)擊列表進(jìn)行打印 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); //注冊(cè)監(jiān)聽(tīng) } var device = null, BAdapter = null, BluetoothAdapter = null, uuid = null, main = null, bluetoothSocket = null; function print(mac_address) { if (!mac_address) { mui.toast("請(qǐng)選擇藍(lán)牙打印機(jī)"); 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("檢測(cè)到設(shè)備未連接,嘗試連接...."); bluetoothSocket.connect(); } console.log("設(shè)備已連接"); if (bluetoothSocket.isConnected()) { var outputStream = bluetoothSocket.getOutputStream(); plus.android.importClass(outputStream); var string = "打印測(cè)試\r\n"; var bytes = plus.android.invoke(string, "getBytes", "gbk"); outputStream.write(bytes); outputStream.flush(); device = null //這里關(guān)鍵 bluetoothSocket.close(); //必須關(guān)閉藍(lán)牙連接否則意外斷開(kāi)的話打印錯(cuò)誤 } }
以上是針對(duì)Native.js對(duì)Android藍(lán)牙設(shè)備的操作實(shí)例代碼,如果你想了解Native.js的其它實(shí)例代碼或原生的Android藍(lán)牙操作方法請(qǐng)查看下面的相關(guān)鏈接
名稱欄目:Native.js獲取監(jiān)聽(tīng)開(kāi)關(guān)等操作Android藍(lán)牙設(shè)備實(shí)例代碼
當(dāng)前地址:http://www.rwnh.cn/article22/ippsjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、虛擬主機(jī)、品牌網(wǎng)站制作、微信公眾號(hào)、定制網(wǎng)站、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容