Polidea / RxBluetoothKit

iOS & OSX Bluetooth library for RxSwift
Apache License 2.0
1.41k stars 370 forks source link

peripheral:didDiscoverServices not implemented #274

Closed aleksanderaleksic closed 6 years ago

aleksanderaleksic commented 6 years ago

Describe the bug I have a Peripheral that I have been connected to and now try to connect again after a disconnect. But when I reconnect and try to discoverServices, I get this error from CoreBluetooth: [CoreBluetooth] API MISUSE: Discovering services for peripheral <CBPeripheral: 0x1c811c320, identifier = F33ABC8C-4A72-5FCB-C6D0-91708AB10A3B, name = Airthings Node, state = connected> while delegate is either nil or does not implement peripheral:didDiscoverServices:

To Reproduce Steps to reproduce the behavior:

  1. Scan
  2. Connect
  3. Discover Services
  4. Discover Characteristics
  5. Disconnect
  6. Scan
  7. Connect
  8. Discover Services

Expected behavior I expect it to scan and return the services.

Environment:

pouljohn1 commented 6 years ago

@aleksanderaleksic Thanks for report. I've tried to reproduce this behaviour but without success. From the above error it means that CBPeripheral doesn't have delegate set or peripheral:didDiscoverServices: in delegate is not implemented.

Are you changing CBPeripheral delegate? or maybe are you using some other bluetooth library that could do that?

aleksanderaleksic commented 6 years ago

Mhm, I am actually using another Library that I provide the CBPeripheral to. But shouldn't the delegate get set again when I connect to it? If not, how can I set it again?

aleksanderaleksic commented 6 years ago

I think i see ut now. If i call the attach() method om the peripheral object, that will "reset" the peripheral? Right?

Cannot test it now, i where just looking through your source code.

aleksanderaleksic commented 6 years ago

It solved it!

Thanks for the help!

dariuszseweryn commented 6 years ago

Hello @aleksanderaleksic

Could you share how did you solve the problem?

pouljohn1 commented 6 years ago

@aleksanderaleksic yes, attach() method is for re attaching RxBluetoothKit delegates to CBPeripheral. Great that you solved it :)

aleksanderaleksic commented 6 years ago

@dariuszseweryn I solved it by calling peripheral.attach() on the peripheral object.

Like this (but this is a very simple example):

manager.scanForPeripherals(withServices: [SOME UUIDS])
                .flatMap{ $0.peripheral.establishConnection() }
                .flatMap{ $0.attach() }

So i do this everytime i connect to a peripheral, just to be sure its set.