Git Product home page Git Product logo

wapp_printer's Introduction

WAPP_printer

基于小程序开发的蓝牙连接打印机打印标签



1.在需要调用蓝牙的页面中
初始化是传入设备的主服务UUID base.Ble.initBle(['FFF0', '0A71']);

2.在onLoad中执行监听函数(记得在onUnload中 remove掉事件监听)
//初始化所有监听
    initListen: function() {
        //监听蓝牙可用状态
        base.Event.listen(base.EventModel.EVENT_BLESTATE, function(data) {
            if (data.code) {
                console.log('蓝牙已打开')
                base.blestate = true;
            } else {
                console.log('蓝牙已关闭')
                base.blestate = false;
            }
        })

        //监听扫描设备
        base.Event.listen(base.EventModel.EVENT_SCAN, function(data) {
            if (data.stop) {
                console.log('停止扫描');
                that.setData({
                    bleSearchIng: false,
                })
            } else {
                var device = data.datainfo[0];
                console.log('扫描到的设备 : ' + JSON.stringify(device));
                that.data.deviceArray.push(device);
                that.setData({
                    deviceArray: that.data.deviceArray,
                    showDeviceDialog: true,
                })
            }
        })

        //监听蓝牙连接状态
        base.Event.listen(base.EventModel.EVENT_CONNSTATE, function(data) {
            console.log('连接状态变化');
            if (data.code == true) {
                console.log('index 连接成功')

                for (var i = 0; i < that.data.deviceArray.length; i++) {
                    if (that.data.deviceArray[i].deviceId === data.datainfo) {
                        that.data.connDevice = that.data.deviceArray[i];
                    }
                }

                that.setData({
                    connSucced: true,
                    connDevice: that.data.connDevice,
                })
            } else {
                that.setData({
                    connSucced: false,
                })
            }
        })

        //监听蓝牙值变化
        base.Event.listen(base.EventModel.EVENT_RECEIVE, function(result) {
            if (result.code == true && pageState == 'onLoad' || pageState == 'onShow') {
                var data = result.datainfo;
                console.log('main 页面接受到消息:' + data);

            }
        })
    },

3.如果打印机无法打印 请查看打印机说明书 知否支持TSPL或ESC指令指令 (本demo使用的佳博答应机)
  自行将PrintUtil.js中的打印指令修改为对应的打印指令即可

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.