Skip to content

Can't figure out how to scan for peripherals supporting specific service #104

@sarbogast

Description

@sarbogast

Issue

When I scan for all peripherals without specifying a service like the following, I get a certain number of devices that support service 180A (Device Information), as evidenced by the logs printed by discoverServices:

RZBUserInteraction.setEnabled(true)
RZBUserInteraction.setTimeout(5)    
var tags = [Tag]()
central.scanForPeripherals(withServices: nil, options: nil) { (scanInfo, scanError) in
    if let error = scanError {
        if let bluetoothError = RZBluetoothError(rawValue: UInt(error._code)) {
            if bluetoothError == .timeoutError {
                completion(.success(tags))
            } else {
                print("Failed to scan for peripherals because \(String(describing: bluetoothError))")
            }
        } else {
            print("Failed to scan for peripherals because \(String(describing: error))")
        }
    } else if let info = scanInfo {
        let peripheral = info.peripheral
        if(!tags.contains(where: { tag -> Bool in
            tag.uuid == peripheral.identifier.uuidString
        })) {
            tags.append(Tag(
                id: nil,
                uuid: peripheral.identifier.uuidString,
                name: peripheral.name,
                RSSI: info.rssi.intValue
            ))
            peripheral.discoverServiceUUIDs([CBUUID(string: "180A")], completion: { error in
                if let error = error {
                    print("Failed to discover services for \(peripheral.identifier.uuidString) because \(String(describing: error))")
                } else {
                    print("Peripheral \(peripheral.identifier.uuidString) supports 180A")
               }
            })
        }
    }
}

But if I scan specifically for peripherals that advertise that 180A service, I get zero device, as evidenced by the fact that the breakpoint I place on let peripheral = info.peripheral is never triggered.

RZBUserInteraction.setEnabled(true)
RZBUserInteraction.setTimeout(5)
var tags = [Tag]()
central.scanForPeripherals(withServices: [CBUUID(string: "180A")], options: nil) { (scanInfo, scanError) in
    if let error = scanError {
        if let bluetoothError = RZBluetoothError(rawValue: UInt(error._code)) {
            if bluetoothError == .timeoutError {
                completion(.success(tags))
            } else {
                print("Failed to scan for peripherals because \(String(describing: bluetoothError))")
            }
        } else {
            print("Failed to scan for peripherals because \(String(describing: error))")
        }
    } else if let info = scanInfo {
        let peripheral = info.peripheral
        if(!tags.contains(where: { tag -> Bool in
            tag.uuid == peripheral.identifier.uuidString
        })) {
            tags.append(Tag(
                id: nil,
                uuid: peripheral.identifier.uuidString,
                name: peripheral.name,
                RSSI: info.rssi.intValue
            ))
            peripheral.discoverServiceUUIDs([CBUUID(string: "180A")], completion: { error in
                if let error = error {
                    print("Failed to discover services for \(peripheral.identifier.uuidString) because \(String(describing: error))")
                } else {
                    print("Peripheral \(peripheral.identifier.uuidString) supports 180A")
                }
            })
        }
    }
}

Am I forgetting something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions