iOS peripheral service still empty after discoveri

2019-07-13 17:53发布

I am trying to use Bluetooth I have something like this :

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])
}

My service function :

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

my Service is found and I got in console :

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

Why my services for my peripheral is still empty after discovering but got no error ?

Any ideas ?

thanks !

2条回答
贼婆χ
2楼-- · 2019-07-13 18:36

Well, I restarted my iPhone and all work very good now... Thanks guys !

查看更多
可以哭但决不认输i
3楼-- · 2019-07-13 18:49

It can be nil. You should only check for services under the for loop you're using. Since you're not getting any service, you can also try peripheral.discoverServices(nil). It will list all the services.

查看更多
登录 后发表回答