7***@qq.com
7***@qq.com
  • 发布:2018-03-21 09:35
  • 更新:2023-12-04 23:25
  • 阅读:10919

蓝牙打印机打印二维码

分类:Native.js

蓝牙连接打印借鉴

var OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");  
//mac_address 蓝牙地址  
//byteStr 二维码内容  
function PrintQrCode(mac_address,byteStr) {  
    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);  

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

    console.log('设备已连接');  

    if (bluetoothSocket.isConnected()) {  
      var outputStream = bluetoothSocket.getOutputStream();  
      plus.android.importClass(outputStream);  
      var writer = new OutputStreamWriter(outputStream, 'GBK');   
      plus.android.importClass(writer);  

      //init  
      writer.write(0x1B);  
      writer.write(0x40);  
      writer.flush();  

      var moduleSize = 8;  
      var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk');  
      var length = bytes.length;  

      console.log(length);  
      //打印二维码矩阵    
      writer.write(0x1D);// init    
      writer.write("(k");// adjust height of barcode    
      writer.write(length + 3); // pl    
      writer.write(0); // ph    
      writer.write(49); // cn    
      writer.write(80); // fn    
      writer.write(48); //    
      writer.write(byteStr);  

      writer.write(0x1D);  
      writer.write("(k");  
      writer.write(3);  
      writer.write(0);  
      writer.write(49);  
      writer.write(69);  
      writer.write(48);  

      writer.write(0x1D);  
      writer.write("(k");  
      writer.write(3);  
      writer.write(0);  
      writer.write(49);  
      writer.write(67);  
      writer.write(moduleSize);  

      writer.write(0x1D);  
      writer.write("(k");  
      writer.write(3); // pl    
      writer.write(0); // ph    
      writer.write(49); // cn    
      writer.write(81); // fn    
      writer.write(48); // m    

      writer.flush();   
    }  
}
0 关注 分享

要回复文章请先登录注册

1***@qq.com

1***@qq.com

回复 Apple小赞 :
你好 bytestr 是二维码包含的内容字符串吗,还是要转成其他的才行的,我打印的时候打印机无反应
2019-10-09 15:55
温柔如斯

温柔如斯

回复 Apple小赞 :
这个直接传字符串就能打出来二维码还是?能否发个demo。1145769693@qq.com
2019-06-04 15:51
温柔如斯

温柔如斯

回复 那年暑假 :
是用的这个代码吗?怎么打印出来的
2019-06-04 15:51
温柔如斯

温柔如斯

打不出二维码吧
2019-06-04 15:29
1***@qq.com

1***@qq.com

搞不好,打印不出来
2019-04-28 17:29
1***@qq.com

1***@qq.com

也有这个需求,关注并期待
2019-04-26 11:20
那年暑假

那年暑假

回复 Morning :
弄好了
2019-04-11 09:31
4***@qq.com

4***@qq.com

能问一下writer.write()里面的参数,这些数字都是什么意思吗,文档在哪里看?
2019-04-09 16:50
Morning

Morning

回复 那年暑假 :
请问你打印二维码和文字的弄好了吗
2019-04-04 09:48
那年暑假

那年暑假

请问如果是打印条形码code128,该怎么写?
2018-09-25 15:21