2

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 !

Logan Gallois
  • 594
  • 5
  • 27
  • Have you saved your `peripheral` in a property, perhaps in `didDiscover,,` which isn't shown here? – Paulw11 Jun 03 '16 at 21:39
  • I record all peripheral found into an array of CBPeripheral like this : Peripherals.append(peripheral). But i am not using them yet. – Logan Gallois Jun 04 '16 at 08:12

2 Answers2

3

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

Logan Gallois
  • 594
  • 5
  • 27
  • I've just wasted hours on this! My backgrounded peripheral was discovered and connected to when using my service UUID, but `didDiscoverServices` returned no services. A restart fixed it. – Darren Jan 30 '20 at 16:01
  • Seems I spoke too soon. It worked all day yesterday in the background fine. Today I used Xcode to run a new version of the app on both devices, and i'm back to not being able to get the background service even though it discovers it. I've restarted both devices many times :( – Darren Jan 31 '20 at 11:41
  • Maybe disable/enable BT ? – Logan Gallois Feb 10 '20 at 19:20
1

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.

Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
  • I alteady tried with nil parameter and iOS just find 0 service. But if I use my service in parameter, iOS find the service I got one my Peripheral. I understood that iOS add service in peripheral.services for all service found and can be use directly in this handler : "didDiscoverservice" – Logan Gallois Jun 03 '16 at 18:47
  • @LoganGallois If you are not getting any services then there are no services and you should create a service and add it to your peripheral. – Bhavuk Jain Jun 03 '16 at 18:52
  • I am getting one because I got the message "found service" in "didDiscoverservice" – Logan Gallois Jun 03 '16 at 19:01
  • It will be called when you call discoverServices method but you should check the services using the peripheral only. – Bhavuk Jain Jun 03 '16 at 19:05
  • Oh ok. So how I use the peripheral only ? With discoverService(nil) ? – Logan Gallois Jun 03 '16 at 19:10
  • using peripheral.services, the one you're using in for statement. – Bhavuk Jain Jun 03 '16 at 19:13
  • I don't get it :/ peripheral.services is nil so i can't use it because I didn't check services – Logan Gallois Jun 03 '16 at 19:23