非常简单的应用程序,可以使用 Maui 在 Android 和 Windows 上运行。输入一些数据,然后它会通过蓝牙热敏打印机打印收据。
我在 Platforms/Android 和 Platforms/Windows 中构建了一个简单的 IPrintingService 接口和两个单独的实现。
问题出在 Windows 中与打印机交互
安卓 使用BluetoothManager和BluetoothAdapter类我可以找到打印机,打开蓝牙套接字并启动ESC打印机命令!完美,我收到收据了。
Windows 经过大量研究后,我找不到关于 windows win ui3 的太多关于这个主题的信息,
//获取配对设备 DeviceInformationCollection PairedBluetoothDevices = 等待 DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromPairingState(true));
//获取我感兴趣的设备 var device = PairedBluetoothDevices.FirstOrDefault(x=>x.Name == PrinterName);
奇怪的是,返回的设备 Pairing.IsPaired 为 false,Pairing.CanPair 也是如此?但它又返回到配对设备列表中了???
await device.PairAsync(DevicePairingProtectionLevel.None) 不执行任何操作,我也尝试使用自定义配对请求处理程序,但奇怪的是返回“已配对”。
我当时就要去所以用 varserialDevice=awaitSerialDevice.FromIdAsync(device.Id)
然后使用数据写入器写入serialDevice输出流,但SerialDevice.FromIdAsync(device.Id)抛出异常:System.Private.CoreLib.dll中的'System.Runtime.InteropServices.COMException'
我认为我错误地接近窗户一侧,任何帮助将不胜感激。
它会有所不同,具体取决于您使用的是 Plugin.BLE 还是 MAUI 中的本机 Android 代码,以及您如何实现交互(直接使用适配器 API 的意图) 如果你分享你的 GIT 我可以看一下,我最终可以在你的项目中给你留下评论。 干杯 J