PureSwift / GATT

Bluetooth Generic Attribute Profile (GATT) for Swift (Supports Linux)
MIT License
64 stars 18 forks source link

CharacteristicProperty Notify #3

Closed tnmendes closed 6 years ago

tnmendes commented 7 years ago

I am trying to have listener to read notify (Bluetooth.GATT.CharacteristicProperty.Notify) but i am having falling to do that.

can you help me by doing some example?

This is what i have done.

`let foundPeripherals = central.scan()

    print("Scan results: \(foundPeripherals.map({ $0.identifier }))")

    for peripheral in foundPeripherals {

        do { try central.connect(to: peripheral) }
        catch {

            print("Error connecting to \(peripheral.identifier): \(error)")
            continue
        }

        print("Did connect to \(peripheral.identifier)")

        let services = try! central.discoverServices(for: peripheral)

        print("Discovered services: \(services.map({ $0.UUID }))")

        let serviceUuid = BluetoothUUID.init(rawValue: "1706BBC0-88AB-4B8D-877E-2237916EE929")

        if services.contains(where: { $0.UUID == serviceUuid }) {

            print("    SERVICE FOUND")
            print("    Number of services",services.count)
            print("    service 1 ",services.first?.UUID ?? "null")

            for service in services {

                let characteristics = try! central.discoverCharacteristics(for: service.UUID, peripheral: peripheral)

                print("Found \(characteristics.count) characteristics for service \(service.UUID)")

                if(characteristics.count == 0){
                    self.central.disconnect(peripheral: peripheral)
                    return
                }

                for characteristic in characteristics {

                    print("         characteristic: ",characteristic.UUID)
                    print("         characteristic prop: ",characteristic.properties)

                    let charWriteUuid = BluetoothUUID.init(rawValue: CHARACTERISTIC_WRITE)

                    if (characteristic.UUID == charWriteUuid)  {
                        do { try central.write(data: "Test Write".data(using: .utf8)!, response: true, characteristic: characteristic.UUID, service: service.UUID, peripheral: peripheral) }

                        catch {   print("Error write to \(peripheral.identifier): \(error)")}

                    }

                    let charNotifyUuid = BluetoothUUID.init(rawValue: CHARACTERISTIC_NOTIFY)

                    if(characteristic.UUID == charNotifyUuid){

                        DispatchQueue.main.async(execute: {
                            var value: Data!

                            var a = self.central.stateChanged

                            /*do { value = try self.central.read(characteristic: characteristic.UUID, service: service.UUID, peripheral: peripheral) }
                            catch {   print("Error read to \(peripheral.identifier): \(error)")}

                            print("         READDDDD: ",value)*/
                            self.central.disconnect(peripheral: peripheral)

                        })
                    }

                }
            }

        }

`

colemancda commented 7 years ago

Notify doesn't work, its just a placeholder.

colemancda commented 7 years ago

See BluetoothLinux for the actual functionality available. All GATT operations except notify should be there.

tnmendes commented 7 years ago

thank you for the link, i am going to have look on that code.

colemancda commented 6 years ago

I have implemented notifications now.