iOS CoreBluetooth写入Zebra ZD421D BLE打印机特征无响应问题
问题:Zebra ZD421D BLE打印机写入特征值无反应
我查了各种教程和帮助文档,还是解决不了这个问题。我买了一台带BLE蓝牙的Zebra ZD421D标签打印机,现在开发自定义iOS应用实现打印功能。目前已经能发现设备、成功连接,也能获取服务和特征,但调用WriteValue写入特征值时,打印机完全没反应。我试了所有找到的特征,结果都一样。
为了排查是不是代码的问题,我用LightBlue应用测试,同样能找到并连接打印机,选特征写入新值后,打印机还是没反应,这让我很困惑。
我不确定是不是打印机配置的问题,查了Zebra打印机设置应用,但没找到相关解决选项。
我的Swift代码
import SwiftUI import CoreBluetooth class BluetoothViewModel: NSObject, ObservableObject { private var centralManager: CBCentralManager? private var peripherals: [CBPeripheral] = [] private var printerPeripheral: CBPeripheral! @Published var peripheralNames: [String] = [] override init() { super.init() self.centralManager = CBCentralManager(delegate: self, queue: .main) } } extension BluetoothViewModel: CBCentralManagerDelegate, CBPeripheralDelegate { func centralManagerDidUpdateState(_ central: CBCentralManager) { if central.state == .poweredOn { self.centralManager?.scanForPeripherals(withServices: nil) } } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { if !peripherals.contains(peripheral) { self.peripherals.append(peripheral) self.peripheralNames.append(peripheral.name ?? "Unnamed Device") if (peripheral.identifier.uuidString == "F9FC7DFB-F149-4F95-7CEF-D6596338D032") { centralManager?.connect(peripheral, options: nil) self.printerPeripheral = peripheral self.printerPeripheral.delegate = self print("connecting to \(peripheral.name ?? \"Unnamed Device\")") centralManager?.stopScan() } } } func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) { print("Failed to connect to \(peripheral). (\(error!.localizedDescription))") } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { if peripheral == self.printerPeripheral { print("Connected to your printer") peripheral.discoverServices(nil) } } func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { print("didDiscoverServices") if let services = peripheral.services { for service in services { print("Service - \(service.uuid.uuidString) - \(service.description)") if service.uuid.uuidString.elementsEqual("38EB4A80-C570-11E3-9507-0002A5D5C51B") { print("Printer service found") peripheral.discoverCharacteristics(nil, for: service) } } } } func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { print("didDiscoverCharactericsFor") if let characteristics = service.characteristics { print(characteristics.count) for characteristic in characteristics { print("Characteristic - \(characteristic.uuid)") if (characteristic.uuid.uuidString.elementsEqual("38EB4A82-C570-11E3-9507-0002A5D5C51B")) { print("write") var data: String = "test" let valueString = (data as NSString).data(using: String.Encoding.utf8.rawValue) peripheral.writeValue(valueString!, for: characteristic, type: .withResponse) print("Value String===>\(valueString.debugDescription)") } } } } func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { guard error == nil else { print("Fejl \(error)") return } print("Message sent") } } struct ContentView: View { @ObservedObject private var bluetoothViewModel = BluetoothViewModel() var body: some View { NavigationView { List(bluetoothViewModel.peripheralNames, id: \.self) { Text($0) .navigationTitle("Peripherals") } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
控制台输出
connecting to D8J222507660 Connected to your printer didDiscoverServices Service - 180A - <CBService: 0x280250580, isPrimary = YES, UUID = Device Information> Service - 38EB4A80-C570-11E3-9507-0002A5D5C51B - <CBService: 0x2802502c0, isPrimary = YES, UUID = 38EB4A80-C570-11E3-9507-0002A5D5C51B> Printer service found didDiscoverCharactericsFor 6 Characteristic - 38EB4A81-C570-11E3-9507-0002A5D5C51B Characteristic - 38EB4A82-C570-11E3-9507-0002A5D5C51B write Value String===>6 bytes Characteristic - 38EB4A83-C570-11E3-9507-0002A5D5C51B Characteristic - 38EB4A84-C570-11E3-9507-0002A5D5C51B Characteristic - 38EB4A87-C570-11E3-9507-0002A5D5C51B Characteristic - 38EB4A88-C570-11E3-9507-0002A5D5C51B Message sent
我尝试通过自定义应用和LightBlue写入值,打印机均无反应。
内容的提问来源于stack exchange,提问作者frigsfrogs
相关产品推荐
相关产品推荐

