le-connection-abort-by-local on Raspberry Pi 4
I am running on Raspberry Pi 4 Model B to connect with BLE devices.
When I run adapter.Connect, it always returns error "le-connection-abort-by-local."
BlueZ version = 5.66
package main
import (
"fmt"
"strings"
"time"
"tinygo.org/x/bluetooth"
)
func main() {
adapter := bluetooth.DefaultAdapter
if err := adapter.Enable(); err != nil {
panic(err)
}
scanCh := make(chan bluetooth.ScanResult, 1)
err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
println("found device:", device.Address.String(), device.RSSI, device.LocalName())
if strings.Contains(device.LocalName(), "PD") {
scanCh <- device
adapter.StopScan()
}
})
device := <- scanCh
close(scanCh)
adapter.SetConnectHandler(func(device bluetooth.Address, connected bool) {
fmt.Printf("Connected to %s with %v\n", device.String(), connected)
})
time.Sleep(time.Second * 1)
_, err = adapter.Connect(device.Address, bluetooth.ConnectionParams{
ConnectionTimeout: bluetooth.NewDuration(time.Second * 100),
})
if err != nil {
fmt.Printf("Failed to Connect %s\n", err.Error())
} else {
fmt.Printf("Connected to %s\n", device.Address)
}
}
Have you tried connecting manually through bluetoothctl? Does it work there?
Hi, this also happens sporadically on the Raspberry Pi 5. Sometimes it works and sometimes it doesn't. I'm wondering if there is some internal state in the BT stack that the library scan/connect pattern isn't always achieving. Similarly bluetoothctl connect does work, but only after doing scan on and letting it find the target device. Not sure what all data is helpful but here is some basic info:
Using tinygo.org/x/bluetooth v0.10.0
$ bluetoothd -v
5.66
$ uname -a
Linux raspberrypi 6.6.31+rpt-rpi-2712 #1 SMP PREEMPT Debian 1:6.6.31-1+rpt1 (2024-05-29) aarch64 GNU/Linux
This is a 2 part question:
I notice a difference in the behavior of adapter.Connect between linux and macos (trying to migrate my app from my laptop to the rasppi). ConnectionTimeout doesn't seem to be honored on linux - it seems to either connect right away or fail, based on if a scan "found" the device already. This isn't inside the same go app PID, across different executions. Which seems to indicate the state is lower in the BT stack. On macos no pre-scan is necessary, can connect straight to the device using a hardcoded addr, and it's not instantaneous, can take 30+ seconds but does honor the timeout and works 99+%.
For linux - is the pre-scan required even if I already know the device addr I want to connect to?