| | |
| | | var main = plus.android.runtimeMainActivity(); |
| | | var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter"); |
| | | var UUID = plus.android.importClass("java.util.UUID"); |
| | | |
| | | |
| | | uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); |
| | | |
| | | console.log(JSON.stringify(uuid, null, 2)); |
| | | |
| | | var BAdapter = BluetoothAdapter.getDefaultAdapter(); |
| | | BAdapter.cancelDiscovery(); //停止扫描 |
| | | |
| | |
| | | var address_mac = api_localStorageGet("printMac") ? api_localStorageGet("printMac") : |
| | | "DC:1D:30:3F:D2:50"; |
| | | device = BAdapter.getRemoteDevice(address_mac); |
| | | |
| | | plus.android.importClass(device); |
| | | |
| | | bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid); |
| | | |
| | | console.log(JSON.stringify(bluetoothSocket, null, 2)); |
| | | |
| | | plus.android.importClass(bluetoothSocket); |
| | | } catch (err) { |
| | | |
| | | console.log(err); |
| | | //mui.alert(err) |
| | | plus.ui.toast("蓝牙连接异常!"); |
| | | plus.ui.toast("蓝牙连接异常!"+ err); |
| | | } |
| | | }) |
| | | |
| | |
| | | //获取打印信息 |
| | | var pdaMac = $("#pdaMac").val(); |
| | | var printMac = $("#printMac").val(); |
| | | console.log(pdaMac); |
| | | //执行打印 |
| | | //console.log(pdaMac + "-0-" + printMac) |
| | | doPrintTest(pdaMac, printMac); |
| | |
| | | //执行打印 |
| | | function doPrintTest(pdaMac, printMac) { |
| | | try { |
| | | |
| | | console.log(1); |
| | | |
| | | console.log(bluetoothSocket); |
| | | |
| | | if (!bluetoothSocket.isConnected()) { |
| | | plus.ui.toast("重新连接"); |
| | | bluetoothSocket.connect(); |
| | | } |
| | | |
| | | |
| | | |
| | | var outputStream = bluetoothSocket.getOutputStream(); |
| | | plus.android.importClass(outputStream); |
| | | |
| | |
| | | var WifiInfo = plus.android.importClass("android.net.wifi.WifiInfo"); |
| | | var wifiInfo = wifiManager.getConnectionInfo(); |
| | | mac = wifiInfo.getMacAddress(); |
| | | |
| | | //如果mac为“02:00:00:00:00:00”,则可能是安卓6.0以上版本,则使用另一种方法获取mac地址 |
| | | if (mac == "02:00:00:00:00:00") { |
| | | mac = getMacNew(); |
| | | } |
| | | //console.log(mac) |
| | | //如果mac使用新方法依然是“02:00:00:00:00:00”则不进行保存 |
| | | if(mac == "02:00:00:00:00:00") { |
| | | return; |
| | | } |
| | | |
| | | api_localStorageSave("pdaMac", mac); |
| | | //console.log(mac) |
| | | |
| | | } |
| | | } |
| | | //安卓6.0以上版本获取Mac地址 |