From 402d8d66a30b62f4bb3343309a089b1b2121eb26 Mon Sep 17 00:00:00 2001
From: cdk <2441919651@qq.com>
Date: 星期三, 30 七月 2025 08:30:27 +0800
Subject: [PATCH] 蓝牙模块增加安卓14以上的处理

---
 lib/base_js/js/print.js |  363 +++++++++++++++++++++++++++++----------------------
 1 files changed, 204 insertions(+), 159 deletions(-)

diff --git a/lib/base_js/js/print.js b/lib/base_js/js/print.js
index a9384f9..1241c1f 100644
--- a/lib/base_js/js/print.js
+++ b/lib/base_js/js/print.js
@@ -1,82 +1,119 @@
+//瀹夊崜13鍙婁互涓婂姩鎬佺敵璇疯摑鐗欐潈闄�
+function requestBluetoothPermissions() {
+    try {
+        var main = plus.android.runtimeMainActivity();
+        var Build = plus.android.importClass('android.os.Build');
+        var sdkInt = Build.VERSION.SDK_INT;
+        if (sdkInt >= 31) { // 瀹夊崜12(API 31)鍙婁互涓�
+            var permissions = [
+                'android.permission.BLUETOOTH_SCAN',
+                'android.permission.BLUETOOTH_CONNECT',
+                'android.permission.BLUETOOTH_ADMIN',
+                'android.permission.BLUETOOTH'
+            ];
+            var PermissionChecker = plus.android.importClass('androidx.core.content.ContextCompat');
+            var ActivityCompat = plus.android.importClass('androidx.core.app.ActivityCompat');
+            for (var i = 0; i < permissions.length; i++) {
+                var hasPermission = PermissionChecker.checkSelfPermission(main, permissions[i]);
+                if (hasPermission !== 0) {
+                    ActivityCompat.requestPermissions(main, [permissions[i]], 1);
+                }
+            }
+        }
+    } catch (e) {
+        console.log('钃濈墮鏉冮檺鐢宠寮傚父锛�' + e);
+    }
+}
+
 //address=""鎼滅储钃濈墮//address=璁惧mac鍦板潃锛岃嚜鍔ㄩ厤瀵圭粰鍑簃ac鍦板潃鐨勮澶�  
 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='姝e湪鎼滅储璇风◢鍊�';  
-    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()); //鎵撳嵃鏈簃ac鍦板潃  
-                        li2.setAttribute('onclick', 'print(id)'); //娉ㄥ唽click鐐瑰嚮鍒楄〃杩涜鎵撳嵃  
-                        li2.innerText = BleDevice.getName();  
-                        vlist2.appendChild(li2);  
-                    }  
+    requestBluetoothPermissions(); // 鏂板锛氬厛鐢宠鏉冮檺
+    try {
+        //娉ㄥ唽绫�  
+        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='姝e湪鎼滅储璇风◢鍊�';  
+        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()); //鎵撳嵃鏈簃ac鍦板潃  
+                            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()); //鎵撳嵃鏈簃ac鍦板潃  
+                        li1.setAttribute('onclick', 'searchDevices(id)'); //娉ㄥ唽click鐐瑰嚮鍒楄〃杩涜閰嶅  
+                        on = BleDevice.getName();  
+                        li1.innerText = on;  
+                        vlist1.appendChild(li1);  
+
+                        }  
+
+                    }  
                 } else {  
-                    if(BleDevice.getName() != on ){ //鍒ゆ柇闃叉閲嶅娣诲姞  
-                    var li1 = document.createElement('li'); //娉ㄥ唽  
-                    li1.setAttribute('id', BleDevice.getAddress()); //鎵撳嵃鏈簃ac鍦板潃  
-                    li1.setAttribute('onclick', 'searchDevices(id)'); //娉ㄥ唽click鐐瑰嚮鍒楄〃杩涜閰嶅  
-                    on = BleDevice.getName();  
-                    li1.innerText = on;  
-                    vlist1.appendChild(li1);  
+                    if(BleDevice.getName() != un ){ //鍒ゆ柇闃叉閲嶅娣诲姞  
+                    console.log("宸查厤瀵硅摑鐗欒澶囷細" + BleDevice.getName() + '    ' + BleDevice.getAddress());  
+                    var li2 = document.createElement('li'); //娉ㄥ唽  
+                    li2.setAttribute('id', BleDevice.getAddress()); //鎵撳嵃鏈簃ac鍦板潃  
+                    li2.setAttribute('onclick', 'print(id)'); //娉ㄥ唽click鐐瑰嚮鍒楄〃杩涜鎵撳嵃  
+                    un = BleDevice.getName();                 
+                    li2.innerText = un;  
+                    vlist2.appendChild(li2);}  
+                }}  
 
-                    }  
+            }  
+        });  
 
-                }  
-            } else {  
-                if(BleDevice.getName() != un ){ //鍒ゆ柇闃叉閲嶅娣诲姞  
-                console.log("宸查厤瀵硅摑鐗欒澶囷細" + BleDevice.getName() + '    ' + BleDevice.getAddress());  
-                var li2 = document.createElement('li'); //娉ㄥ唽  
-                li2.setAttribute('id', BleDevice.getAddress()); //鎵撳嵃鏈簃ac鍦板潃  
-                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);  
 
-        }  
-    });  
-
-    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); //娉ㄥ唽鐩戝惉  
+        main.registerReceiver(receiver, filter); //娉ㄥ唽鐩戝惉  
+    } catch (e) {
+        console.log('鎼滅储钃濈墮璁惧寮傚父锛�' + e);
+        var button1 = document.getElementById('bt1');
+        if (button1) {
+            button1.disabled = false;
+            button1.value = '鎼滅储璁惧';
+        }
+    }
 }  
 
 var device = null,  
@@ -87,96 +124,104 @@
     bluetoothSocket = null;  
 
 function print(mac_address,printStr) {  
-    if (!mac_address) {  
-        mui.toast('璇烽�夋嫨钃濈墮鎵撳嵃鏈�');  
-        return;  
-    }  
+    requestBluetoothPermissions(); // 鏂板锛氬厛鐢宠鏉冮檺
+    try {
+        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);  
+        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();  
-    }  
+        if (!bluetoothSocket.isConnected()) {  
+            console.log('妫�娴嬪埌璁惧鏈繛鎺ワ紝灏濊瘯杩炴帴....');  
+            bluetoothSocket.connect();  
+        }  
 
-    console.log('璁惧宸茶繛鎺�');  
+        console.log('璁惧宸茶繛鎺�');  
 
-    if (bluetoothSocket.isConnected()) {  
-        var outputStream = bluetoothSocket.getOutputStream();  
-        plus.android.importClass(outputStream);  
-//         var string = 
-// '! 0 200 200 640 1\n'
-// +'PAGE-WIDTH 640\n'
-// +'PW 848\n'
-// +'TONE 0\n'
-// +'SPEED 0\n'
-// +'GAP-SENSE\n'
-// +'NO-PACE\n'
-// +'POSTFEED 0\n'
-// +'LEFT\n'
-// +'T 65 1 20 300 鐗╂枡缂栫爜锛歕n'
-// +'SETBOLD 2\n'
-// +'T 65 1 135 300 {0}\n'
-// +'SETBOLD 0\n'
-// +'T 65 0 20 330 鐗╂枡鍚嶇О锛歕n'
-// +'T 65 1 135 330 {4}\n'
-// +'T 65 1 20 360 鐗╂枡瑙勬牸锛歕n'
-// +'T 65 1 135 360 {1} \n'
-// +'T 65 1 135 390 {18}\n'
-// +'T 65 0 20 420 鍒拌揣鍗曞彿锛歕n'
-// +'T 65 1 135 420 {16}\n'
-// +'T 65 0 20 450 閿�鍞鍗曪細\n'
-// +'T 65 1 135 450 {17}\n'
-// +'T 65 0 20 475 渚涘簲鍟嗭細\n'
-// +'T 65 1 135 475 {2}\n'
-// +'T 65 0 20 500 妫�楠屽憳锛歕n'
-// +'T 65 1 135 500 {3}\n'
-// +'T 65 0 320 500 妫�楠屾棩鏈燂細\n'
-// +'T 65 1 435 500 {5}\n'
-// +'SETBOLD 2\n'
-// +'SETBOLD 0\n'
-// +'SETMAG 0 0\n'
-// +'T 65 1 350 20 鏁伴噺锛歕n'
-// +'T 65 1 445 20 {6}\n'
-// +'T 65 1 350 55 鏍囧噯瑁呯鏁帮細\n'
-// +'T 65 1 490 55 {7}\n'
-// +'T 65 1 350 90 绠辨暟锛歕n'
-// +'T 65 1 445 90 {8}\n'
-// +'T 65 1 350 125 瀹㈡埛锛歕n'
-// +'T 65 1 445 125 {9}\n'
-// +'T 65 1 350 160 鐗堟湰鍙凤細\n'
-// +'T 65 1 445 160 {10}\n'
-// +'T 65 1 350 195 鏉愯川锛歕n'
-// +'T 65 1 445 195 {11}\n'
-// +'T 65 1 350 230 灏哄锛歕n'
-// +'T 65 1 445 230 {12}\n'
-// +'T 65 1 350 265 鍏跺畠鍖呰瑕佹眰锛歕n'
-// +'T 65 1 510 265 {13}\n'
-// +'T 65 1 350 300 涓濆嵃鍟嗘爣锛歕n'
-// +'T 65 1 470 300 {14}\n'
-// +'SETBOLD 2\n'
-// +'SETBOLD 0\n'
-// +'B QR 40 20 M 12 H 4\n'
-// +'MA,A1234567890\n'
-// +'ENDQR\n'
-// +'B 128 2 1 50 50 530 A1234567890\n'
-// +'T 65 1 200 590 A1234567890\n'
-// +'FORM\n'
-// +'PRINT\n';  
-        var bytes = plus.android.invoke(printStr, 'getBytes', 'gbk');  
-        outputStream.write(bytes);  
-        outputStream.flush();  
-        device = null //杩欓噷鍏抽敭  
-        bluetoothSocket.close(); //蹇呴』鍏抽棴钃濈墮杩炴帴鍚﹀垯鎰忓鏂紑鐨勮瘽鎵撳嵃閿欒  
+        if (bluetoothSocket.isConnected()) {  
+            var outputStream = bluetoothSocket.getOutputStream();  
+            plus.android.importClass(outputStream);  
+    //         var string = 
+    // '! 0 200 200 640 1\n'
+    // +'PAGE-WIDTH 640\n'
+    // +'PW 848\n'
+    // +'TONE 0\n'
+    // +'SPEED 0\n'
+    // +'GAP-SENSE\n'
+    // +'NO-PACE\n'
+    // +'POSTFEED 0\n'
+    // +'LEFT\n'
+    // +'T 65 1 20 300 鐗╂枡缂栫爜锛歕n'
+    // +'SETBOLD 2\n'
+    // +'T 65 1 135 300 {0}\n'
+    // +'SETBOLD 0\n'
+    // +'T 65 0 20 330 鐗╂枡鍚嶇О锛歕n'
+    // +'T 65 1 135 330 {4}\n'
+    // +'T 65 1 20 360 鐗╂枡瑙勬牸锛歕n'
+    // +'T 65 1 135 360 {1} \n'
+    // +'T 65 1 135 390 {18}\n'
+    // +'T 65 0 20 420 鍒拌揣鍗曞彿锛歕n'
+    // +'T 65 1 135 420 {16}\n'
+    // +'T 65 0 20 450 閿�鍞鍗曪細\n'
+    // +'T 65 1 135 450 {17}\n'
+    // +'T 65 0 20 475 渚涘簲鍟嗭細\n'
+    // +'T 65 1 135 475 {2}\n'
+    // +'T 65 0 20 500 妫�楠屽憳锛歕n'
+    // +'T 65 1 135 500 {3}\n'
+    // +'T 65 0 320 500 妫�楠屾棩鏈燂細\n'
+    // +'T 65 1 435 500 {5}\n'
+    // +'SETBOLD 2\n'
+    // +'SETBOLD 0\n'
+    // +'SETMAG 0 0\n'
+    // +'T 65 1 350 20 鏁伴噺锛歕n'
+    // +'T 65 1 445 20 {6}\n'
+    // +'T 65 1 350 55 鏍囧噯瑁呯鏁帮細\n'
+    // +'T 65 1 490 55 {7}\n'
+    // +'T 65 1 350 90 绠辨暟锛歕n'
+    // +'T 65 1 445 90 {8}\n'
+    // +'T 65 1 350 125 瀹㈡埛锛歕n'
+    // +'T 65 1 445 125 {9}\n'
+    // +'T 65 1 350 160 鐗堟湰鍙凤細\n'
+    // +'T 65 1 445 160 {10}\n'
+    // +'T 65 1 350 195 鏉愯川锛歕n'
+    // +'T 65 1 445 195 {11}\n'
+    // +'T 65 1 350 230 灏哄锛歕n'
+    // +'T 65 1 445 230 {12}\n'
+    // +'T 65 1 350 265 鍏跺畠鍖呰瑕佹眰锛歕n'
+    // +'T 65 1 510 265 {13}\n'
+    // +'T 65 1 350 300 涓濆嵃鍟嗘爣锛歕n'
+    // +'T 65 1 470 300 {14}\n'
+    // +'SETBOLD 2\n'
+    // +'SETBOLD 0\n'
+    // +'B QR 40 20 M 12 H 4\n'
+    // +'MA,A1234567890\n'
+    // +'ENDQR\n'
+    // +'B 128 2 1 50 50 530 A1234567890\n'
+    // +'T 65 1 200 590 A1234567890\n'
+    // +'FORM\n'
+    // +'PRINT\n';  
+            var bytes = plus.android.invoke(printStr, 'getBytes', 'gbk');  
+            outputStream.write(bytes);  
+            outputStream.flush();  
+            device = null //杩欓噷鍏抽敭  
+            bluetoothSocket.close(); //蹇呴』鍏抽棴钃濈墮杩炴帴鍚﹀垯鎰忓鏂紑鐨勮瘽鎵撳嵃閿欒  
 
-    }  
-
+        }  
+    } catch (e) {
+        console.log('钃濈墮鎵撳嵃寮傚父锛�' + e);
+        mui.toast('钃濈墮鎵撳嵃澶辫触锛岃妫�鏌ユ潈闄愬拰璁惧杩炴帴');
+        try {
+            if (bluetoothSocket) bluetoothSocket.close();
+        } catch (ex) {}
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3