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

Cannot connect to device #320

Open
ztolley opened this issue Jan 3, 2025 · 2 comments
Open

Cannot connect to device #320

ztolley opened this issue Jan 3, 2025 · 2 comments
Labels
question Further information is requested

Comments

@ztolley
Copy link

ztolley commented Jan 3, 2025

I have a BLE device that I've successfully scanned for and connected to in a simple diagnostic SwiftUI app, now I'm trying to talk to it from a command line app written in Go that will ultimately run on Linux.

I can scan for my device and read the advertised manufacturer data but when I try to connect (which should take no longer than 4-5 seconds) the code just sits there and never responds.

I've created a minimal example of what I'm doing:

package main

import (
	"fmt"

	"tinygo.org/x/bluetooth"
)

func main() {
	targetUUID := bluetooth.NewUUID([16]byte{0xb7, 0x31, 0x81, 0x0c, 0x26, 0x62, 0x8c, 0x80, 0x5f, 0x4f, 0xfa, 0x1f, 0x53, 0x3e, 0x9d, 0x11})

	Adapter := bluetooth.DefaultAdapter
	must("enable BLE stack", Adapter.Enable())

	fmt.Printf("Start scanning for devices\n")

	must("start scan", Adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
		if device.HasServiceUUID(targetUUID) {
			fmt.Printf("Found device %s\nConnecting\n", device.Address.String())

			peripheral, err := Adapter.Connect(device.Address, bluetooth.ConnectionParams{})

			if err != nil {
				fmt.Printf("Failed to connect to device %s: %v\n", device.Address.String(), err)
				return
			}

			fmt.Printf("Connected to device %s\n", device.Address.String())
			peripheral.Disconnect()
			Adapter.StopScan()
		}
	}))

}

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}

When I run this the only output is:

Start scanning for devices
Found device bfc981a3-dd25-77a0-31af-04d7ff2d69b0
Connecting

Am I doing something fundamentally wrong?

I've also successfully connected to the same device using Rust/btleplug so I'm confident the device works.

@deadprogram
Copy link
Member

I would suggest look at the examples here https://github.com/tinygo-org/bluetooth/tree/release/examples/discover and here https://github.com/tinygo-org/bluetooth/tree/release/examples/heartrate-monitor for how to connect as a Central to a Peripheral.

Hope that helps!

@deadprogram deadprogram added the question Further information is requested label Jan 4, 2025
@ztolley
Copy link
Author

ztolley commented Jan 5, 2025

I tried those . I've done more testing and the problem seems to be only when running on Linux . Runs fine on Mac, in fact:

1: Run on linux, get unable to connect - Times out - bluetooth: failed to connect: le-connection-abort-by-local
2: Run same code on MacOS - The occasional connection failure but connects and I can read services, characteristics and read data
3: Run on linux again - Works fine.

After a while Linux stops working and I have to run it on the mac again

I suspect this is something to do with Bluez so feel free to close this ticket unless anyone out there has a clue why, otherwise I'll go to Stack Overflow :)

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

No branches or pull requests

2 participants