cdk
4 天以前 402d8d66a30b62f4bb3343309a089b1b2121eb26
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地址,自动配对给出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);
                    }
    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='正在搜索请稍候';
        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() != 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);
                    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);}
                }}
                    }
            }
        });
                }
            } 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);
        }
    });
    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) {}
    }
}