5. Discovering services & characteristics

paweljaneczek edited this page Jun 22, 2018 · 3 revisions

Discovering services

After connecting to peripheral, the most common task is to discover Services. Because all of wanted services are discovered at once, method returns Single<[Service]>. In order to make it into Observable<Service> and fire for each of service discovered, we advice you to use our RxSwift operator Observable.from()

Here's how it works in RxBluetoothKit:

peripheral.establishConnection()
    .flatMap { $0.discoverServices([serviceId]) }.asObservable()
    .flatMap { Observable.from($0) }
    .subscribe(onNext: { service in
        print("Discovered service: \(service)")
    })

Discovering characteristics

Discovering characteristics method is very similar to discoverServices. This time API's returning Observable<[Characteristic]> and to process one characteristic at a time, you need to once again use Observable.from()

peripheral.establishConnection()
    .flatMap { $0.discoverServices([serviceId]) }.asObservable()
    .flatMap { Observable.from($0) }
    .flatMap { $0.discoverCharacteristics([characteristicId])}.asObservable()
    .flatMap { Observable.from($0) }
    .subscribe(onNext: { characteristic in
        print("Discovered characteristic: \(characteristic)")
    })
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.