ios swift 3-打印机蓝牙

ios swift 3-打印机蓝牙,ios,swift,printing,bluetooth,Ios,Swift,Printing,Bluetooth,我正在尝试创建一个应用程序,通过蓝牙打印机打印 在xcode上,我可以连接打印机,也可以查看服务和uuid,但问题是,当我试图查看服务的特性时,我发现没有 有人知道这个问题吗 func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { for service in peripheral.services! { print("Service \(service)\n

我正在尝试创建一个应用程序,通过蓝牙打印机打印

在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")
}

您在
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)

    }
}

然后,您将接到对
外围设备的呼叫(u外围设备:cbperipal,didDiscoveryCharacteristics for service:CBService,error:error?)
delegate method

尝试打印
error
错误为nil@preetam您需要调用
discoverCharacteristics:for
我调用了它,并尝试在其中创建for循环:for characteristic in service.characteristics!{print(“anything”)}但是xcode没有到达循环中的代码行,我也尝试打印此函数的错误,错误也是nil@paulw11请编辑您的问题,以显示您如何尝试发现特征