muka / go-bluetooth

Golang bluetooth client based on bluez DBus interfaces
Apache License 2.0
653 stars 123 forks source link

cannot receive notifications on Raspberry Pi #159

Closed jagobagascon closed 2 years ago

jagobagascon commented 2 years ago

Hi!

I'm trying to setup a small application that connects to a BLE device, enables notifications on a characteristic and periodically receives data updates. I already have an application up and running on macOS using tinygo-org/bluetooth. But when I tried it on my Raspberry Pi I could not make it work.

So, instead of relying on tinygo-org/bluetooth I tried using this library, but I've had no luck.

I've tried using AcquireNotify and reading from the file descriptor:

fd, mtu, err := characteristic.AcquireNotify(map[string]interface{}{})
if err != nil {
    return err
}

go func(f dbus.UnixFD, mtu uint16) {
    fileR := os.NewFile(uintptr(f), "notifyfd")
    defer fileR.Close()
    reader := bufio.NewReader(fileR)

    p := make([]byte, mtu)

    for {
        n, err := reader.Read(p)
        if err != nil {
            if err == io.EOF {
                // channel closed on disconnection
                println("Stopping notifications reader")
                return
            }
            println("Err: ", err.Error())
        }
        println("Received message with size: ", n)
    }
}(fd, mtu)

And also WatchProperties and StartNotify:

ch, err := characteristic.WatchProperties()
if err != nil {
    return err
}
go func() {
    for update := range ch {
        if update.Interface == "org.bluez.GattCharacteristic1" && update.Name == "Value" {
            println("Message received")
        }
    }
}()
c.characteristic.StartNotify()

But none of them work. I'm not sure if this is a problem related to the raspberry pi itself, my bluez version, or if it is a bug somewhere in this library.

I've checked the examples and I would say that there's nothing with my code. Has anyone successfully received notifications using this library? Is there something I'm missing here?

Thanks in advance!

Environment

jagobagascon commented 2 years ago

Apparently the BLE device I was connecting to was not properly working. After trying with a new one my code worked perfectly.

Sorry for the inconvenience. I'm closing this now.