Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CBPeripheral advertisementData is different when discovering peripherals on OSX vs iOS (GAP/GATT)

I am hoping to port some of my CoreBluetooth code from iOS to OS X. I've set up a shared set of CoreBluetooth wrappers which are consumed by both an iOS app and an OS X app in exactly the same manner with the same BLE devices.

Scanning for peripherals:

override init() {
    super.init()
    let queue = DispatchQueue.global(qos: .background)
    centralManager = CBCentralManager(delegate: self, queue: queue)
}

func startScanning() {
    let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: true]
    let deviceUUID = CBUUID(string: Project.Service.Device)
    let recoveryUUID = CBUUID(string: Project.Service.DFURecovery)
    centralManager?.scanForPeripherals(withServices: [deviceUUID, recoveryUUID], options: options)
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
    // Inspect advertisementData here to decipher what kind of device
}

On my iOS app, didDiscoverPeripheral is fired. Then when I inspect the advertisement data I get all the keys/values that I am expecting:

{
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = "My Device";
    kCBAdvDataManufacturerData = <34045254 5877f283 43fdd12d ff530978 45000000 000050c2 6500>;
    kCBAdvDataServiceData =     {
        Battery = <64>;
    };
    kCBAdvDataServiceUUIDs =     (
        "My Inforamtion"
    );
}

However when this same code is run (scanning for the same devices) from an OS X app, the advertisement data is missing some of the fields.

{
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataManufacturerData = <34045254 5877f36e 43fdd12d ff530978 45000000 000050c2 6500>;
}

The following key/value pairs are missing from advertisedData.

kCBAdvDataLocalName
kCBAdvDataServiceData
kCBAdvDataServiceUUIDs

I've tried adding those keys to the scanForPeripherals call like so:

    let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey: true,
                                  CBAdvertisementDataLocalNameKey: true,
                                  CBAdvertisementDataServiceDataKey: true,
                                  CBAdvertisementDataServiceUUIDsKey: true]
    let deviceUUID = CBUUID(string: Nightlight.Service.Device)
    let recoveryUUID = CBUUID(string: Nightlight.Service.DFURecovery)
    centralManager?.scanForPeripherals(withServices: [deviceUUID, recoveryUUID], options: options)

With no effect.

like image 866
VaporwareWolf Avatar asked Jan 13 '17 05:01

VaporwareWolf


1 Answers

Update. I am using UIKitForMac Beta 2 (now called Catalyst) and am seeing the same issue. The solution listed above (caching/aggregating the adverstisment data) applies in this situation as well.

like image 142
VaporwareWolf Avatar answered Nov 04 '22 18:11

VaporwareWolf