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

[Linux] Scanning in progress gets out of sync if starting to scan while adapter is powered off #340

Closed
Lenart12 opened this issue Feb 19, 2025 · 5 comments

Comments

@Lenart12
Copy link

As in the title, when trying to start scan but adapter is powered off it will internaly think scan is active but that is not the case.

Minimal reproduction:

//go:build linux
// +build linux

package bluetooth

import (
	"testing"
	"time"

	"github.com/godbus/dbus/v5"
)

func (a *Adapter) SetPowered(powered bool) error {
	return a.adapter.SetProperty("org.bluez.Adapter1.Powered", dbus.MakeVariant(powered))
}

func TestScanWhilePoweredOff(t *testing.T) {
	adapter := DefaultAdapter
	if err := adapter.Enable(); err != nil {
		t.Skipf("skipping test: failed to enable adapter: %v", err)
	}

	if err := adapter.SetPowered(false); err != nil {
		t.Fatalf("failed to power off adapter: %v", err)
	}

	scanFinished := make(chan struct{})
	errCh := make(chan error)
	go func() {
		defer close(scanFinished)
		t.Log("starting scan")
		if err := adapter.Scan(func(_ *Adapter, _ ScanResult) {
			// Do nothing.
		}); err != nil {
			errCh <- err
		}
	}()

	// Wait for the scan to start.
	select {
	case err := <-errCh:
		// expected error
		if err.Error() != "Resource Not Ready" {
			t.Fatalf("unexpected error: %v", err)
		}
	case <-time.After(1 * time.Second):
		t.Fatal("scan did not finish")
	case <-scanFinished:
		t.Fatal("scan finished without error")
	}

	if err := adapter.SetPowered(true); err != nil {
		t.Fatalf("failed to power on adapter: %v", err)
	}

	// Wait for adapter to be ready.
	time.Sleep(1 * time.Second)

	// Start scan, again.
	if err := adapter.Scan(func(_ *Adapter, _ ScanResult) {
		// Do nothing.
	}); err != nil {
		t.Fatalf("failed to restart scan: %v", err)
	}
	t.Log("pass: scan started successfully")
}
@Lenart12 Lenart12 changed the title [Linux] Scanning in progres gets out of sync if starting to scan while adapter is powered off [Linux] Scanning in progress gets out of sync if starting to scan while adapter is powered off Feb 19, 2025
@deadprogram
Copy link
Member

@Lenart12 what do you think should happen in this case? Some possibilities:

  • calling adaptor.Enable() on Linux automatically calls something like SetPowered()
  • calling adaptor.Enable() on Linux checks to see if the adaptor is powered, and if not returns an error.
  • something else?

@Lenart12
Copy link
Author

Lenart12 commented Feb 20, 2025

I think it should be handled by the user manualy. Here in the example adapter is restarted by hand but on linux it can be reset (or powered down) by other applications as well. Probably best way in my mind is only to fix getting out of sync and report an error that can be handled by the user, maybe with a call to Enable()

@deadprogram
Copy link
Member

OK, I think I misunderstood the original question. My bad.

My current understanding is that if the adaptor is powered down while already scanning, you want to return right away from scan with an error.

If that is the case, then this is probably the place to try to catch that event happening:
https://github.com/tinygo-org/bluetooth/blob/release/gap_linux.go#L232

@deadprogram
Copy link
Member

@Lenart12 see #342

@deadprogram
Copy link
Member

deadprogram commented Feb 20, 2025

When I live tested on my local machine, and powered off the Bluetooth adaptor while scanning with this branch:

$ go run ./examples/scanner/                
scanning... 
found device: *****************                                                             
found device: *****************                                                             
found device: *****************                                                             
found device: *****************                                                             
found device: *****************                                                             
found device: *****************                                                             
found device: *****************                                                                                                                     
panic: failed to start scan: bluetooth: adaptor is not powered                     

So it appears to work correctly.

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

No branches or pull requests

2 participants