This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Swift 3 - CBPeripheral.services nil problem

Hi, I was trying to create an NUS (Nordic UART Service) application for iOS 10 devices using Swift 3.

I was testing with my

  • iPhone 6 Plus 64GB (iOS 10.2.1)

  • PCA10040 v1.1 that has NUS peripheral example from SDK 12.1 + S132 v3.0

ViewController.swift

As you see my attachment, I left a comment at the centralManager function.

When the centralManagerfunction is called, the log screen printed Nordic_UART.

However, the services couldn't be printed because it was nil.

Are there extra configurations to set in order to get the services?

-Best Regards

  • I recommend that you take a look at the nRF Toolbox app source code, found here on our GitHub page. You can find the code for the UART module here.

    Best regards

    Bjørn

  • Hi, Bjørn.

    It seems that the centralManagerDidSelectPeripheral calls connectPeripheral in NORBluetoothManager.swift while connecting with the peripheral.

    Where can I find the code about UART service discovery and read/write?

  • You are on the right track, but you are still missing the service and characteristic discovery steps.

    When you connect to a peripheral, you should call discoverServices, and when you get the callback to didDiscoverServices, the services property of your CBPeripheral will be populated.

    Afterwards, you should discover characteristics for each service using the discoverCharacteristics method, you will then get a callback to didDiscoverCharacteristics, at this point, your CBPeriphreal will include all the Services/Characteristics. without those steps, it'll all remain Nil.

    Tips: You should specify the services you're interested in discovering, leaving this filter Nil will work, but is discouraged by Apple.

    And here is the code that can walk you through how the nRFToolbox app does it

  • Hi, Mostafa. Thanks for your guidance.

    You should specify the services you're interested in discovering
    

    Does this mean I have to specify the UUID of NUS TX and RX, which are

    6e400002-b5a3-f393-e0a9-e50e24dcca9e // RX UUID
    6e400003-b5a3-f393-e0a9-e50e24dcca9e // TX UUID
    

    ?

  • No, those are the characteristics, you should scan only for the service first, using that UUID:

    `6E400001-B5A3-F393-E0A9-E50E24DCCA9E` //UART Service
    

    After that service is discovered, you should discover characteristics for this service, you may use a nil filter to discover all, or specify only the RX/TX characteristic UUIDs like so:

    `6E400002-B5A3-F393-E0A9-E50E24DCCA9E` //RX Characteristic
    `6E400003-B5A3-F393-E0A9-E50E24DCCA9E` //TX Characteristic
    

    after those are discovered, you may proceed to write/read form those characteristics.

Related