manolofdez / AsyncBluetooth

A small library that adds concurrency to CoreBluetooth APIs.
MIT License
168 stars 31 forks source link

How do you get the discover characteristic for service? #19

Closed vhscott closed 1 year ago

vhscott commented 1 year ago

Not a bug.

Can you please show me how to get the discovered characteristic?

Thanks!

sample code below:

            if let services = peripheral.discoveredServices {
                for service in services {
                    if service.uuid.uuidString == myServiceUUID {
                        print("found service ", service.uuid.uuidString)
                        try await peripheral.discoverCharacteristics([characteristicUUIDTX], for: service)
                    }
                }
manolofdez commented 1 year ago

Hi! After you await, you can do service.discoveredCharacteristics to get the list of all of them, or service.discoveredCharacteristics?.first(where: { $0.uuid == characteristicUUIDTX }) to get the characteristic you were looking for.

Also, the readValue, writeValue and setNotifyValue extensions on Peripheral will automatically discover the given service and characteristic.

vhscott commented 1 year ago

Thank you!