bluetooth/adapter_linux.go
Ayke van Laethem b06d666dbf all: remove Addresser
Remove the Addresser type. It isn't really necessary (the Address type
can change between OSes) and makes it difficult to fix a heap allocation
in interrupts (on the Nordic SoftDevices).

This is a backwards incompatible change, but only programs that use
SetConnectHandler should notice this.
2023-04-26 23:37:36 +02:00

58 lines
1.3 KiB
Go

//go:build !baremetal
// +build !baremetal
// Some documentation for the BlueZ D-Bus interface:
// https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
package bluetooth
import (
"errors"
"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/bluez/profile/adapter"
)
type Adapter struct {
adapter *adapter.Adapter1
id string
cancelChan chan struct{}
defaultAdvertisement *Advertisement
connectHandler func(device Address, connected bool)
}
// DefaultAdapter is the default adapter on the system. On Linux, it is the
// first adapter available.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = &Adapter{
connectHandler: func(device Address, connected bool) {
return
},
}
// Enable configures the BLE stack. It must be called before any
// Bluetooth-related calls (unless otherwise indicated).
func (a *Adapter) Enable() (err error) {
if a.id == "" {
a.adapter, err = api.GetDefaultAdapter()
if err != nil {
return
}
a.id, err = a.adapter.GetAdapterID()
}
return nil
}
func (a *Adapter) Address() (MACAddress, error) {
if a.adapter == nil {
return MACAddress{}, errors.New("adapter not enabled")
}
mac, err := ParseMAC(a.adapter.Properties.Address)
if err != nil {
return MACAddress{}, err
}
return MACAddress{MAC: mac}, nil
}