笑语相识
笑语相识
  • 发布:2018-03-20 15:35
  • 更新:2020-06-04 18:04
  • 阅读:2602

二维码打印

分类:5+ SDK

想请教一下,dcloud如何实现打印二维码到蓝牙打印机,打印字符串的已经实现。
<!doctype html>
<html>

<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />  
    <link href="css/mui.min.css" rel="stylesheet" />  
</head>  

<body>
<header class="mui-bar mui-bar-nav">
<h1 class="mui-title">打印</h1>
</header>
<footer id="footer" class="mui-bar mui-bar-footer">
<h1 class="mui-title" id='search'>
<span class="mui-icon mui-icon-search"></span>
<span style="font-size:16px;">搜索蓝牙设备</span>
</h1>
</footer>
<div class="mui-content mui-content-padded">
<h5 class="mui-content-padded">点击设备开始打印</h5>
<ul id="list" class="mui-table-view mui-table-view-chevron">
</ul>
</div>
<script src="js/mui.min.js"></script>
<script type="text/javascript" charset="utf-8">
mui.init();
mui.plusReady(function(){
scan();
})

        //获取已配对的蓝牙设备列表  
        function scan() {  
            var main = plus.android.runtimeMainActivity();  
            var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
            var BAdapter = BluetoothAdapter.getDefaultAdapter(); //获得本机蓝牙适配器  

            if (!BAdapter.isEnabled()) {  
                console.log('检测到未打开蓝牙,尝试打开中....');  
                BAdapter.enable();  
            }  

            var lists = BAdapter.getBondedDevices();//获取配对的设备列表  
            plus.android.importClass(lists);  
            var iterator = lists.iterator();  
            plus.android.importClass(iterator);  
            var ul = document.getElementById('list');  
            ul.innerHTML = '';  
            while (iterator.hasNext()) {  
                var d = iterator.next();  
                plus.android.importClass(d);  
                var li = document.createElement('li');  
                li.setAttribute('id', d.getAddress());  
                li.className = 'mui-table-view-cell';  
                var a = document.createElement('a');  
                a.setAttribute('class', 'mui-navigate-right')  
                a.innerText = d.getName();  
                li.appendChild(a);  
                ul.appendChild(li);  
            }  
        }  

        //mac_address:打印机的mac地址  
        function print(mac_address) {  
            if (!mac_address) {  
                mui.toast('请选择蓝牙打印机');  
                return;  
            }  
            var main = plus.android.runtimeMainActivity();  
            var BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
            var UUID = plus.android.importClass("java.util.UUID");  
            var uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  
            var BAdapter = BluetoothAdapter.getDefaultAdapter();  

            var device = BAdapter.getRemoteDevice(mac_address);  
            plus.android.importClass(device);  

            var bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);  
            plus.android.importClass(bluetoothSocket);  

            if (!bluetoothSocket.isConnected()) {  
                console.log('检测到设备未连接,尝试连接....');  
                bluetoothSocket.connect();  
            }  

            //console.log('bluetoothSocket.isConnected()='   bluetoothSocket.isConnected());  

            //为什么只能是首次连接成功,之后就一直是未连接  
            if (bluetoothSocket.isConnected()) {  
                var outputStream = bluetoothSocket.getOutputStream();  
                plus.android.importClass(outputStream);  
                var string ='测试测试\r\n\r\n\r\n';  
                var bytes = plus.android.invoke(string, 'getBytes', 'gbk');  
                outputStream.write(bytes);  
                outputStream.flush();  
            }else{  
                alert("not connect");  
            }  
        }  

        mui('#list').on('tap', 'li', function() {  
            print(this.id);  
            //localStorage.setItem('mac_address',this.id);  
        })  

        document.getElementById("search").addEventListener('tap',scan);  

    </script>  
</body>  

</html>

2018-03-20 15:35 负责人:无 分享
已邀请:
wnsuan

wnsuan

请问这个问题你解决了吗

l***@163.com

l***@163.com

我也卡在打印二维码上了

宁夏

宁夏

请问可以打印二维码了吗

该问题目前已经被锁定, 无法添加新回复