.NET MAUI Android应用如何通过通用蓝牙打印机打印内容?
MAUI Android 通用蓝牙打印机打印实现方案
核心思路
大部分通用蓝牙小票打印机支持ESC/POS指令集,我们可以通过蓝牙串口连接后,直接发送对应指令和文本内容实现打印,无需专用SDK。
步骤实现
1. 完善蓝牙连接的Android底层实现
你的IBluetoothConnector需在Android项目中实现,确保获取蓝牙Socket的输出流用于发送数据,示例代码:
[assembly: Dependency(typeof(BluetoothConnectorAndroid))] namespace YourAppName.Platforms.Android { public class BluetoothConnectorAndroid : IBluetoothConnector { private BluetoothSocket _bluetoothSocket; private Stream _outputStream; public async Task Connect(string deviceName) { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.DefaultAdapter; if (bluetoothAdapter == null || !bluetoothAdapter.IsEnabled) { throw new Exception("蓝牙未启用"); } // 根据设备名称查找已配对蓝牙设备 BluetoothDevice device = bluetoothAdapter.BondedDevices.FirstOrDefault(d => d.Name == deviceName); if (device == null) { throw new Exception("未找到指定蓝牙设备"); } // 使用通用蓝牙串口服务UUID创建连接 UUID uuid = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"); _bluetoothSocket = device.CreateRfcommSocketToServiceRecord(uuid); await _bluetoothSocket.ConnectAsync(); _outputStream = _bluetoothSocket.OutputStream; } // 提供输出流供打印功能调用 public Stream GetOutputStream() { return _outputStream; } public void Disconnect() { _outputStream?.Close(); _bluetoothSocket?.Close(); } } }
2. 封装通用打印工具类
创建打印服务类,整合ESC/POS指令和文本发送逻辑:
public class BluetoothPrintService { private readonly IBluetoothConnector _connector; public BluetoothPrintService(IBluetoothConnector connector) { _connector = connector; } public async Task PrintText(string content) { var outputStream = ((BluetoothConnectorAndroid)_connector).GetOutputStream(); if (outputStream == null) { throw new Exception("蓝牙未连接"); } // 发送打印机初始化指令 byte[] initCmd = new byte[] { 0x1B, 0x40 }; await outputStream.WriteAsync(initCmd, 0, initCmd.Length); // 设置默认字体大小(可根据需求修改指令参数) byte[] fontSizeCmd = new byte[] { 0x1B, 0x21, 0x00 }; await outputStream.WriteAsync(fontSizeCmd, 0, fontSizeCmd.Length); // 将文本转为GBK编码(适配多数打印机的中文支持) byte[] textBytes = Encoding.GetEncoding("GBK").GetBytes(content + "\n\n"); await outputStream.WriteAsync(textBytes, 0, textBytes.Length); // 发送切纸指令(部分打印机可能需要调整指令参数) byte[] cutCmd = new byte[] { 0x1D, 0x56, 0x41, 0x00 }; await outputStream.WriteAsync(cutCmd, 0, cutCmd.Length); // 刷新输出流确保数据发送完成 await outputStream.FlushAsync(); } }
3. 调用打印功能
在MAUI页面中完成蓝牙连接后,直接调用打印方法:
// 连接指定蓝牙打印机 const string ArduinoBluetoothTransceiverName = "MPT-II"; var connector = DependencyService.Get<IBluetoothConnector>(); await connector.Connect(ArduinoBluetoothTransceiverName); // 实例化打印服务并执行打印 var printService = new BluetoothPrintService(connector); await printService.PrintText("测试打印内容\nMAUI蓝牙打印示例\n");
注意事项
- 权限配置:在Android项目的
AndroidManifest.xml中添加必要权限:<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" android:maxSdkVersion="33" /> - 编码适配:若打印机不支持GBK,可尝试UTF-8编码,老款打印机可能仅支持GB2312。
- 指令兼容:不同品牌打印机的ESC/POS指令可能略有差异,如切纸指令无效,可尝试替换为
0x1D, 0x56, 0x00。
内容的提问来源于stack exchange,提问作者Simba Chigodo
相关产品推荐
相关产品推荐

