发现后iOS外围服务仍然空闲

时间:2016-06-03 17:56:30

标签: ios swift bluetooth swift2 cbperipheral

我正在尝试使用蓝牙 我有这样的事情:

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
    Connected = true
    TableView.reloadData()
    Activityview.stopAnimating()
    TabBar.topItem!.rightBarButtonItem = UIBarButtonItem(title: "Suivant", style: UIBarButtonItemStyle.Done, target: nil, action: #selector(Connexion.next))
    CM.stopScan()
    Scanning = false
    Refresh_Button.setTitle("Deconnecté", forState: UIControlState.Normal)
    let AlertMessage = UIAlertController(title: "Connecté", message: "Le module est bien connecté", preferredStyle: .Alert)
    let ActionAlert = UIAlertAction(title: "Ok", style: .Default) { (action) in }
    AlertMessage.addAction(ActionAlert)
    self.presentViewController(AlertMessage, animated: true, completion: nil)
    peripheral.delegate = self
    let servicetoFind = CBUUID(string: "19B10000-E8F2-537E-4F6C-D104768A1214")
    peripheral.discoverServices([servicetoFind])
}

我的服务功能:

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
    print("found service")
    print(peripheral)
    print(peripheral.services)
    print(error)
    for service in peripheral.services! {
        print("Searching for charac...")
        peripheral.discoverCharacteristics(nil, forService: service)
    }
}

找到了我的服务,我进入了控制台:

found service
<CBPeripheral: 0x13ce1c430, identifier = F39F9D5D-98FE-A350-17BD-21C1185E2089, name = GENUINO 101-1E2D, state = connected>
Optional([])
nil

为什么我的外设服务在发现后仍然是空的但没有错误?

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

好吧,我重新启动了我的iPhone,现在一切都很好......谢谢你们!

答案 1 :(得分:1)

它可以是零。您应该只检查您正在使用的for循环下的服务。由于您没有获得任何服务,您还可以尝试peripheral.discoverServices(nil)。它将列出所有服务。