91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Native.js獲取監聽開關等操作Android藍牙設備實例代碼

發布時間:2020-08-30 22:02:55 來源:腳本之家 閱讀:262 作者:wdc 欄目:移動開發

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藍牙操作方法請查看下面的相關鏈接

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

鲁山县| 常宁市| 定南县| 安远县| 崇左市| 新郑市| 蒙自县| 新竹县| 营口市| 和平区| 通海县| 安庆市| 师宗县| 通化市| 特克斯县| 崇信县| 蕉岭县| 新源县| 安庆市| 兴安县| 治多县| 鄂尔多斯市| 襄樊市| 吴堡县| 洱源县| 涪陵区| 泌阳县| 桓台县| 诸暨市| 晋城| 邢台县| 琼中| 昌江| 上蔡县| 拉孜县| 陇川县| 塔城市| 阳东县| 共和县| 临朐县| 天台县|