uniapp 蓝牙连接斑马打印机发送zpl指令打印

历程

  • 需求是想通过斑马的zpl语言打印小票等,需要用到蓝牙连接。
  • 一开始采用的是uniapp自带的蓝牙连接和打印,用的是uni.writeBLECharacteristicValue(OBJECT)方法,蓝牙能正常连接和发送数据。奇怪的是发送蓝牙数据始终都是ok,打印机确是一点动静都没有。
  • 基于以上的疑惑,我开始怀疑是uniapp的api存在问题,于是使用c#连接蓝牙打印测试,居然成功了。那么问题就锁定了,是uniapp的api有问题
  • 于是各种找资料,后来在论坛找到了native.js蓝牙发送的,测了下果然可以。

uniapp示例

  • 两个方法,分别对应的两个按钮,依次点击就完事了

			blueOpen() {
				var main = plus.android.runtimeMainActivity();
				var Context = plus.android.importClass("android.content.Context");
				var BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
				plus.android.importClass(BManager); //引入相关的method函数
				var BAdapter = BManager.getAdapter();
				plus.android.importClass(BAdapter); //引入相关的method函数,这样之后才会有isEna
				var lists = BAdapter.getBondedDevices();
				plus.android.importClass(lists);
				//var resultDiv = document.getElementById('bluetooth_list');
				var iterator = lists.iterator();
				plus.android.importClass(iterator);
				while (iterator.hasNext()) {
					var d = iterator.next();
					plus.android.importClass(d);
					console.log('d', d);
					console.log("名称:" + d.getName() + ",地址:" + d.getAddress());
				}

			},
			blueSearch() {
                //蓝牙地址
				var mac_address = "123123"
				var _this = this
				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('设备已连接');

				if (bluetoothSocket.isConnected()) {
					var outputStream = bluetoothSocket.getOutputStream();
					plus.android.importClass(outputStream);
                    //斑马zpl语言
					var string = `^XA^FO50,50^A0N,50,50^FDHello, World!hahahha^FS^XZ`
					var bytes = plus.android.invoke(string, 'getBytes', 'gbk');
					outputStream.write(bytes);
					console.log('发送数据完成', bytes);
					outputStream.flush();
					//成功!!!!
					//device = null //这里关键  
					//bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误  

				}

			},

c#蓝牙示例

        static void PrintTest()
        {
            //有时候无反应,重新打印机 
          
            BluetoothClient client = new BluetoothClient();
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();
            //蓝牙地址
            var device = devices.FirstOrDefault(a => a.DeviceAddress.ToString() == "123123");

            // 连接到蓝牙设备
            BluetoothAddress address = device.DeviceAddress;
            Guid serviceClass = BluetoothService.SerialPort;
            BluetoothEndPoint endPoint = new BluetoothEndPoint(address, serviceClass);
            BluetoothClient clientConn = new BluetoothClient();
            clientConn.Connect(endPoint);

            // 发送数据
            Stream peerStream = clientConn.GetStream();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(@"^XA
            ^FO50,50^A0N,50,50^FDHello, World!^FS
            ^XZ");

            peerStream.Write(data, 0, data.Length);

            // 关闭连接
            clientConn.Close();

            Console.WriteLine("Communication finished.");

        }
posted @ 2024-03-21 15:51  Alex_Mercer  阅读(712)  评论(0编辑  收藏  举报