Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sarbogast opened this issue Sep 4, 2019 · 0 comments

Comments

@sarbogast
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant