ios swift 3 - 打印机蓝牙

时间:2016-10-18 07:41:58

标签: ios swift printing bluetooth

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

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

1 个答案:

答案 0 :(得分: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?)委托方法