移动端蓝牙打印

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <title>html5+ android蓝牙操作</title>
    </head>

    <body>
        <div id="app">
            <input type="button" name="" id="" value="打开蓝牙" @click="turnOnBluetooth" />
            <input type="button" name="" id="" value="关闭蓝牙" @click="turnOffBluetooth" />
            <input type="button" name="" id="" value="已配对的设备" @click="getPairedDevices" />
            <input type="button" name="" id="" value="搜索蓝牙设备" @click="discoveryNewDevice" />
            <div>
                蓝牙状态:
                <pre>{{bluetoothState}}</pre>
            </div>
            <div>
                消息:{{msg}}
            </div>
            <div>
                <input type="button" value="断开连接" @click="disConnDevice" />
            </div>
            已配对的设备:
            <br>
            <ul>
                <li v-for="device in pairedDevices">
                    名称:{{device.name}}<br> 地址:{{device.address}}
                    <br>
                    <input type="button" value="连接" @click="connDevice(device.address)" />
                </li>
            </ul>
            发现的设备:<br>
            <ul>
                <li v-for="device in newDevices">
                    名称:{{device.name}}<br> 地址:{{device.address}}<br>
                    <input type="button" value="连接" @click="connDevice(device.address)" />
                </li>
            </ul>
            <div>
                发送数据:
                <textarea cols="20" rows="4" v-model="sendData"></textarea>
                <input type="button" value="发送" @click="onSendData" />
            </div>
            <br>
            <div style="margin-bottom: 100px;">接收的数据:
                <input type="button" value="清空" @click="receiveDataArr = [];" />
                <div style="width: 100%;min-height: 50px;">byte数组:
                    <span v-for="data in receiveDataArr">
                        {{data}}&nbsp;
                    </span>
                </div>
                <div style="width: 100%;min-height: 50px;word-wrap:break-word;">
                    对应的string字符串:<br> {{receiveDataStr}}
                </div>
            </div>
        </div>

        <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript" src="js/BluetoothTool.js"></script>
        <script type="text/javascript">
            // 监听plusready事件
            document.addEventListener("plusready", function() {
                var bluetoothTool = null;
                var vm = new Vue({
                    "el": "#app",
                    data: function() {
                        return {
                            bluetoothState: {},
                            pairedDevices: [],
                            newDevices: [],
                            receiveDataArr: [],
                            sendData: "",
                            msg: ""
                        }
                    },
                    computed: {
                        receiveDataStr: function() {
                            return String.fromCharCode.apply(String, this.receiveDataArr);
                        },
                        bluetoothStatusDesc: function() {
                            return this.bluetoothStatus ? "已开启" : "已关闭";
                        }
                    },
                    created: function() {
                        let that = this;
                        bluetoothTool = BluetoothTool();
                        this.bluetoothState = bluetoothTool.state;
                        bluetoothTool.init({
                            listenBTStatusCallback: function(state) {
                                that.msg = "蓝牙状态: " + state;
                            },
                            discoveryDeviceCallback: function(newDevice) {
                                that.newDevices.push(newDevice);
                            },
                            discoveryFinishedCallback: function() {
                                that.msg = "搜索完成";
                            },
                            readDataCallback: function(dataByteArr) {
                                if(that.receiveDataArr.length >= 200) {
                                    that.receiveDataArr = [];
                                }
                                that.receiveDataArr.push.apply(that.receiveDataArr, dataByteArr);
                            },
                            connExceptionCallback: function(e) {
                                console.log(e);
                                that.msg = "设备连接失败";
                            }
                        });
                    },
                    methods: {
                        turnOnBluetooth: function() {
                            bluetoothTool.turnOnBluetooth();
                        },
                        turnOffBluetooth: function() {
                            bluetoothTool.turnOffBluetooth();
                        },
                        getPairedDevices: function() {
                            this.pairedDevices = bluetoothTool.getPairedDevices();
                        },
                        discoveryNewDevice: function() {
                            this.newDevices = [];
                            bluetoothTool.discoveryNewDevice();
                        },
                        cancelDiscovery: function() {
                            bluetoothTool.cancelDiscovery();
                        },
                        connDevice: function(address) {
                            bluetoothTool.connDevice(address);
                        },
                        disConnDevice: function() {
                            bluetoothTool.disConnDevice();
                        },
                        onSendData: function() {//sendData
                            bluetoothTool.sendData(this.sendData);
                        },
                        
                    }
                });
            }, false);
        </script>

    </body>

</html>

移动端打印前端实现,可以使用浏览器提供的 `window.print()` 方法来实现。同时,为了适应移动端的页面大小,需要对页面进行一些调整。下面是一个简单的实现示例: ``` <template> <div class="print-demo"> <button @click="print">打印</button> <div class="print-content"> <!-- 要打印的内容 --> </div> </div> </template> <script> export default { methods: { print() { const printContent = document.querySelector('.print-content') const windowWidth = document.documentElement.clientWidth const windowHeight = document.documentElement.clientHeight const printWindow = window.open('', '', `width=${windowWidth},height=${windowHeight}`) printWindow.document.write(` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>打印页面</title> <style> @media print { /* 隐藏不需要打印的元素 */ body *:not(.print-content) { display: none; } } </style> </head> <body> <div class="print-content"> ${printContent.innerHTML} </div> </body> </html> `) printWindow.print() printWindow.close() } } } </script> ``` 在上面的示例中,点击“打印”按钮后,会弹出一个新窗口,并将 `print-content` 元素的内容复制到新窗口中。同时,为了适应移动端页面大小,使用 `windowWidth` 和 `windowHeight` 调整了新窗口的大小。在打印时,使用 `print()` 方法进行打印,并在打印完成后关闭窗口。同时,为了避免不需要打印的元素也被打印,使用 CSS 的 `@media print` 样式将这些元素隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值