iPad连接RPP02热敏打印机打印失败问题求助
问题
我需要在iPad设备上向RPP02型号(VRTEC热敏打印机)打印内容。该打印机在我的安卓项目中可正常使用,且我了解到它支持蓝牙4.0。我创建了一个包含扫描设备及打印按钮的简单项目,但始终无法成功打印,怀疑是CBCharacteristic使用有误。以下是项目代码及调试信息:
import UIKit import CoreBluetooth fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool { switch (lhs, rhs) { case let (l?, r?): return l < r case (nil, _?): return true default: return false } } extension String { struct GBEncoding { public static let GB_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue))) } } class BluetoothConnect: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var buttonTestPrint: UIButton! @IBAction func onClickBarItemClose(_ sender: UIBarButtonItem) { self.dismiss(animated: true, completion: nil) } @IBAction func onClickButtonTestPrint(_ sender: UIButton) { dataToSend = "TEST PRINT GOOOOO".data(using: String.Encoding.utf8) sendDataIndex = 0 sendData() let line = "FORMFEED" peripheral?.writeValue(line.data(using: .utf8)!, for: service.characteristics![3], type: .withResponse) peripheral?.writeValue(line.data(using: String.GBEncoding.GB_18030_2000)!, for: service.characteristics![3], type: .withResponse) peripheral?.writeValue(Data(Array(line.utf8)), for: service.characteristics![3], type: .withResponse) } var centralManager: CBCentralManager! var peripheral: CBPeripheral! var service: CBService! var characteristic: CBCharacteristic! var dataPeripheral: [CBPeripheral] = [CBPeripheral]() var dataPrinterDevice: [Any] = [Any]() var bluetoothAvailable = false override func viewDidLoad() { super.viewDidLoad() self.centralManager = CBCentralManager(delegate: self, queue: .main) self.tableView.delegate = self self.tableView.dataSource = self } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.centralManager.stopScan() } // MARK: - Table View Delegate func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.dataPrinterDevice.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "DevicePrinterCell", for: indexPath) return cell } // // MARK: - Central Manager Delegate func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) { print("disconnect \(String(describing: peripheral.name))") } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { if (!self.dataPeripheral.contains(peripheral)) { if (String(describing: peripheral.name) != "nil" || peripheral.name != nil) { if let power = advertisementData[CBAdvertisementDataTxPowerLevelKey] as? Double { guard let uniqueIDs = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID], let uniqueID = uniqueIDs.last else { return } var objPeripheral: [AnyHashable: Any] = [AnyHashable: Any]() objPeripheral.updateValue(power, forKey: "power") objPeripheral.updateValue(peripheral.name!, forKey: "name") objPeripheral.updateValue(uniqueID.uuidString, forKey: "uuid") self.dataPeripheral.append(peripheral) self.dataPrinterDevice.append(objPeripheral) self.tableView.reloadData() print("Name : ", peripheral.name!) print("UUID : ", uniqueID.uuidString) self.centralManager?.connect(peripheral, options: nil) } } } } func centralManagerDidUpdateState(_ central: CBCentralManager) { print("Checking state") switch (central.state) { case .poweredOn: print("CoreBluetooth BLE hardware is powered on and ready") self.bluetoothAvailable = true; @unknown default: print("Central State None Of The Above") } if bluetoothAvailable == true { self.discoverDevices() } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { self.peripheral = peripheral self.peripheral.delegate = self self.peripheral.discoverServices(nil) } // MARK: - Peripheral Delegate func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) { print("sent \(characteristic)") } func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { for service in peripheral.services! { print("Service \(service)\n") print("Discovering Characteristics for Service : \(service.uuid)") print(service.characteristics as Any) self.service = service self.peripheral.discoverCharacteristics(nil, for: service ) } } func discoverDevices() { print("Discovering devices") self.centralManager.scanForPeripherals(withServices: nil) } }
调试信息
点击打印按钮获取的peripheral信息:
<CBPeripheral: 0x28260dfe0, identifier = 2B2A97C8-F3A3-AD56-19C1-0CA2E011712D, name = BlueTooth Printer, mtu = 185, state = connected>
服务对应的characteristic信息:
<__NSArrayI 0x281800270>( <CBCharacteristic: 0x28335f300, UUID = Serial Number String, properties = 0x2, value = (null), notifying = NO>, <CBCharacteristic: 0x28335d860, UUID = Software Revision String, properties = 0x2, value = (null), notifying = NO>, <CBCharacteristic: 0x28335d980, UUID = Hardware Revision String, properties = 0x2, value = (null), notifying = NO>, <CBCharacteristic: 0x28335c480, UUID = Manufacturer Name String, properties = 0x2, value = (null), notifying = NO> )
内容的提问来源于stack exchange,提问作者MrX
相关产品推荐
相关产品推荐

