我浏览过各种教程和帮助页面,但我似乎无法弄清楚这一点。
我购买了一台带蓝牙 (ble) 的 Zebra ZD421D 标签打印机,现在我正在尝试开发自己的自定义 iOS 应用程序来打印。我已经做到了这一点,我可以在我的应用程序中将其视为外围设备。我可以连接到它,并且可以获得服务和特性。但是当使用 WriteValue 作为特征时,什么也没有发生。我尝试了所有我能找到的不同特征,看看我是否得到了错误的特征,但结果相同。
为了看看我是否做错了什么,我下载了 LightBlue 应用程序,可以在其中测试外围设备和特性。但这也不会向打印机发送任何内容。所以我有点困惑。在 LightBlue 中,我找到打印机,连接到它,选择外围设备,选择特性,单击“写入新值”,然后单击“确定”。但什么也没发生。
不知道是否可以是打印机中的某种配置什么的?我查看了 Zebra 打印机设置应用程序,但找不到任何听起来可以解决我的问题的内容。
下面是我正在使用的快速代码。
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")
//print("\(peripheral.name ?? "Unnamed Device") - \(peripheral.identifier) - \(RSSI)")
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))")
//cleanup()
}
// The handler if we do connect succesfully
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
if peripheral == self.printerPeripheral {
print("Connected to your printer")
//peripheral.discoverServices([CBUUID.init(string: peripheral.identifier.uuidString)])
peripheral.discoverServices(nil)
}
}
// Handles discovery event
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 == CBUUID.init(string: peripheral.identifier.uuidString) {
//if service.uuid == service.uuid {
if service.uuid.uuidString.elementsEqual("38EB4A80-C570-11E3-9507-0002A5D5C51B") {
print("Printer service found")
//Now kick off discovery of characteristics
//peripheral.discoverCharacteristics([CBUUID.init(string: service.uuid.uuidString)], for: service)
peripheral.discoverCharacteristics(nil, for: service)
//return
}
}
}
}
// Handling discovery of characteristics
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")
//let bytesToPrint: [UInt8] = [27, 64, 10, 10, 10, 99, 105, 97, 111, 10, 10, 10]
//let bytesToPrint: [UInt8] = [27, 64, 10, 10]
//let data = Data(bytes: bytesToPrint)
//peripheral.writeValue(data, for: characteristic, type: CBCharacteristicWriteType.withoutResponse)
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) { peripheral in
Text(peripheral)
.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 向打印机写入值。什么也没发生。
我也在解决这个问题。在React Native Expo托管应用程序中,我能够连接到打印机(ZQ 521)并使用react-native-ble-plx读取它的服务和特征。
我找到了要打印的服务和特征的 uuid,并且我能够将 base64 编码的 ZPL 字符串发送到打印机并成功打印。
但是我对较长的 ZPL 存在疑问。不知道写入操作是否有长度限制,我应该进行多次写入调用...
根据您的情况,请检查您的打印机设置,是否能够处理您发送的内容。我将我的配置为读取 ZPL,至少对于短 ZPL,它可以正常工作。