iOS Swift 3 - 打印机蓝牙连接

4
我正在尝试创建一款可以通过蓝牙与打印机连接的应用程序。在Xcode上,我可以连接打印机并查看服务和UUID,但问题在于,当我尝试查看服务特征时,我发现是空值。有没有人对这个问题有什么想法?
  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)

    }
}



override func viewDidLoad() {
    super.viewDidLoad()
    centralManager = CBCentralManager(delegate: self, queue: nil)
}

 func centralManagerDidUpdateState(_ central: CBCentralManager) {

 if #available(iOS 10.0, *){
 switch (central.state) {
 case CBManagerState.poweredOff:
 print("CBCentralManagerState.PoweredOff")
 case CBManagerState.unauthorized:
 print("CBCentralManagerState.Unauthorized")
 break
 case CBManagerState.unknown:
 print("CBCentralManagerState.Unknown")
 break
 case CBManagerState.poweredOn:
 print("CBCentralManagerState.PoweredOn")
 centralManager.scanForPeripherals(withServices: nil, options: nil)
 centralManager.scanForPeripherals(withServices: nil, options: nil)
 case CBManagerState.resetting:
 print("CBCentralManagerState.Resetting")
 case CBManagerState.unsupported:
 print("CBCentralManagerState.Unsupported")
 break
 }}else{
 switch central.state.rawValue{
 case 0:
 print("CBCentralManagerState.Unknown")
 break
 case 1:
 print("CBCentralManagerState.Resetting")
 case 2:
 print("CBCentralManagerState.Unsupported")
 break
 case 3:
 print("This app is not authorised to use Bluetooth low energy")
 break
 case 4:
 print("Bluetooth is currently powered off.")
 case 5:
 print("Bluetooth is currently powered on and available to use.")
 self.centralManager.scanForPeripherals(withServices: nil, options: nil)
 break
 default:
    break
 }}}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    print("name : \(peripheral.name )")


    let device = (advertisementData as NSDictionary)
        .object(forKey: CBAdvertisementDataLocalNameKey)
        as? NSString

    if device?.contains(BEAN_NAME) == true {
        self.centralManager.stopScan()

        self.peripheral = peripheral
        self.peripheral.delegate = self
        print("peripheral: \(self.peripheral)")
        centralManager.connect(peripheral, options: nil)
        print("peripheral: \(self.peripheral)")
    }
}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    peripheral.delegate = self
    peripheral.discoverServices(nil)
}


 func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
print(error)
        for characteristic in service.characteristics! {
           print(anything)

}


func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
    print("Sent")
}

尝试打印“错误”。 - Preetam Jadakar
1
错误为 nil @preetam - Anthony Shahine
你需要调用 discoverCharacteristics:for 方法。https://developer.apple.com/reference/corebluetooth/cbperipheral/1518797-discovercharacteristics - Paulw11
我调用了它并尝试在其中创建一个for循环:for characteristic in service.characteristics!{print(“anything”)},但是Xcode没有到达循环内的代码行,我还尝试打印此函数的错误,但错误也为nil。@Paulw11 - Anthony Shahine
请编辑您的问题,展示您如何尝试发现这些特征。 - Paulw11
@Paulw11,好的,完成了。 - Anthony Shahine
1个回答

3

在你的didDiscoverServices中,你缺少了一个重要的步骤 - 你需要调用discoverCharacteristics:for:方法 -

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)

    }
}

接下来您将收到一个调用您的peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)委托方法的电话。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接