Skip to content

Cannot connect to device #320

Open
Open
@ztolley

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions