2022-02-22 13:04:20 +03:00
|
|
|
//go:build !baremetal
|
2019-11-09 17:55:38 +03:00
|
|
|
// +build !baremetal
|
|
|
|
|
2020-06-11 16:51:24 +03:00
|
|
|
// Some documentation for the BlueZ D-Bus interface:
|
|
|
|
// https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc
|
|
|
|
|
2019-11-09 17:55:38 +03:00
|
|
|
package bluetooth
|
|
|
|
|
|
|
|
import (
|
2021-11-02 01:35:48 +03:00
|
|
|
"errors"
|
|
|
|
|
2019-11-09 17:55:38 +03:00
|
|
|
"github.com/muka/go-bluetooth/api"
|
|
|
|
"github.com/muka/go-bluetooth/bluez/profile/adapter"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Adapter struct {
|
2020-06-01 19:04:03 +03:00
|
|
|
adapter *adapter.Adapter1
|
|
|
|
id string
|
2020-06-15 19:11:53 +03:00
|
|
|
cancelChan chan struct{}
|
2020-06-01 19:04:03 +03:00
|
|
|
defaultAdvertisement *Advertisement
|
2020-09-10 18:17:45 +03:00
|
|
|
|
2023-04-25 03:12:05 +03:00
|
|
|
connectHandler func(device Address, connected bool)
|
2019-11-09 17:55:38 +03:00
|
|
|
}
|
|
|
|
|
2020-06-01 14:26:45 +03:00
|
|
|
// 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.
|
2020-09-10 18:17:45 +03:00
|
|
|
var DefaultAdapter = &Adapter{
|
2023-04-25 03:12:05 +03:00
|
|
|
connectHandler: func(device Address, connected bool) {
|
2020-09-10 18:17:45 +03:00
|
|
|
return
|
|
|
|
},
|
|
|
|
}
|
2019-11-09 17:55:38 +03:00
|
|
|
|
|
|
|
// Enable configures the BLE stack. It must be called before any
|
|
|
|
// Bluetooth-related calls (unless otherwise indicated).
|
2020-06-01 14:26:45 +03:00
|
|
|
func (a *Adapter) Enable() (err error) {
|
|
|
|
if a.id == "" {
|
|
|
|
a.adapter, err = api.GetDefaultAdapter()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.id, err = a.adapter.GetAdapterID()
|
|
|
|
}
|
2019-11-09 17:55:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-11-02 01:35:48 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|