Polidea / RxBluetoothKit

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

Discovered Peripheral name is Nil #297

Open PaulEhrhardt opened 6 years ago

PaulEhrhardt commented 6 years ago

Describe the bug

I have a Bluetooth device that advertises it's name. When running a scan/discovery with plain old CoreBluetooth i receive a CBPeripheral with the correct name in its name property.

When using RxBluetoothKit for scan/discovery on the same device i receive a nil value for the name property.

To Reproduce Steps to reproduce the behaviour:

  1. Scan for a device that advertises its name
  2. Receive RxBluetoothKit.ScannedPeripheral, indicating the successful discovery
  3. The underlying RxBluetoothKit.Peripheral does not contain a value in the name property. Also the wrapped RxBluetoothKit.Peripheral.CBPeripheral in this object does not contain a value in its own name property.

Expected behaviour The name property should contain the name of the device.

Please note the different behaviour using the CBManagerDelegate:

When performing a discovery via Apple's CoreBluetooth the CBPeripheral in the centralManager(_:didDiscover:advertisementData:rssi:) delegate callback has a valid value in it's name property

Environment:

PaulEhrhardt commented 6 years ago

Further information If add a delay of around 100ms before accessing the name property i am finally able to read the name.

Michael-Bischof commented 6 years ago

I noticed, that the name is only missing in the first advertisement for each peripheral received by scanForPeripheral. This should definitely be fixed.

A workaround for now would be to use CBCentralManagerScanOptionAllowDuplicatesKey to allow receiving multiple advertisements for one peripheral, and skipping the ones without a name.

scanForPeripherals(withServices: services, options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
    .skipWhile({ (peripheral) -> Bool in
        return peripheral.peripheral.name == nil
    })
dariuszseweryn commented 5 years ago

This is caused by the iOS vanilla API that may populate name at a later time depending on how it was obtained. The library does not know if a particular peripheral will have a name so I do not expect this issue to be fixable. There may be an additional API for name updates though I suppose. We will get back to this issue next week probably