diff --git a/gap.go b/gap.go index 092afee..50229b5 100644 --- a/gap.go +++ b/gap.go @@ -11,6 +11,28 @@ var ( errAdvertisementPacketTooBig = errors.New("bluetooth: advertisement packet overflows") ) +// MACAddress contains a Bluetooth address which is a MAC address. +type MACAddress struct { + // MAC address of the Bluetooth device. + MAC + isRandom bool +} + +// IsRandom if the address is randomly created. +func (mac MACAddress) IsRandom() bool { + return mac.isRandom +} + +// SetRandom if is a random address. +func (mac MACAddress) SetRandom(val bool) { + mac.isRandom = val +} + +// Set the address +func (mac MACAddress) Set(val interface{}) { + mac.MAC = val.(MAC) +} + // AdvertisementOptions configures an advertisement instance. More options may // be added over time. type AdvertisementOptions struct { diff --git a/gap_darwin.go b/gap_darwin.go index 9e1da79..5f19540 100644 --- a/gap_darwin.go +++ b/gap_darwin.go @@ -8,8 +8,7 @@ import ( "github.com/JuulLabs-OSS/cbgo" ) -// Address contains a Bluetooth address, which on macOS instead of a MAC address -// is instead a UUID. +// Address contains a Bluetooth address which on macOS is a UUID. type Address struct { // UUID since this is macOS. UUID diff --git a/gap_linux.go b/gap_linux.go index 8b2e145..5a6a912 100644 --- a/gap_linux.go +++ b/gap_linux.go @@ -11,27 +11,9 @@ import ( "github.com/muka/go-bluetooth/bluez/profile/device" ) -// Address contains a Bluetooth address, which is a MAC address plus some extra -// information. +// Address contains a Bluetooth MAC address. type Address struct { - // The MAC address of a Bluetooth device. - MAC - isRandom bool -} - -// IsRandom if the address is randomly created. -func (ad Address) IsRandom() bool { - return ad.isRandom -} - -// SetRandom if is a random address. -func (ad Address) SetRandom(val bool) { - ad.isRandom = val -} - -// Set the address -func (ad Address) Set(val interface{}) { - ad.MAC = val.(MAC) + MACAddress } // Advertisement encapsulates a single advertisement instance. @@ -237,12 +219,13 @@ func makeScanResult(props *device.Device1Properties) ScanResult { serviceUUIDs = append(serviceUUIDs, parsedUUID) } + a := Address{} + a.Set(addr) + a.SetRandom(props.AddressType == "random") + return ScanResult{ - RSSI: props.RSSI, - Address: Address{ - MAC: addr, - isRandom: props.AddressType == "random", - }, + RSSI: props.RSSI, + Address: a, AdvertisementPayload: &advertisementFields{ AdvertisementFields{ LocalName: props.Name, diff --git a/gap_nrf51.go b/gap_nrf51.go index 1b42674..86da013 100644 --- a/gap_nrf51.go +++ b/gap_nrf51.go @@ -16,27 +16,9 @@ import ( "time" ) -// Address contains a Bluetooth address, which is a MAC address plus some extra -// information. +// Address contains a Bluetooth MAC address. type Address struct { - // The MAC address of a Bluetooth device. - MAC - isRandom bool -} - -// IsRandom if the address is randomly created. -func (ad Address) IsRandom() bool { - return ad.isRandom -} - -// SetRandom if is a random address. -func (ad Address) SetRandom(val bool) { - ad.isRandom = val -} - -// Set the address -func (ad Address) Set(val interface{}) { - ad.MAC = val.(MAC) + MACAddress } // Advertisement encapsulates a single advertisement instance. diff --git a/gap_nrf528xx.go b/gap_nrf528xx.go index 704e06b..b2a8933 100644 --- a/gap_nrf528xx.go +++ b/gap_nrf528xx.go @@ -27,27 +27,9 @@ var ( globalScanResult ScanResult ) -// Address contains a Bluetooth address, which is a MAC address plus some extra -// information. +// Address contains a Bluetooth MAC address. type Address struct { - // The MAC address of a Bluetooth device. - MAC - isRandom bool -} - -// IsRandom if the address is randomly created. -func (ad Address) IsRandom() bool { - return ad.isRandom -} - -// SetRandom if is a random address. -func (ad Address) SetRandom(val bool) { - ad.isRandom = val -} - -// Set the address -func (ad Address) Set(val interface{}) { - ad.MAC = val.(MAC) + MACAddress } // Advertisement encapsulates a single advertisement instance. diff --git a/gap_windows.go b/gap_windows.go index 6f19c0e..dda3fbf 100644 --- a/gap_windows.go +++ b/gap_windows.go @@ -4,27 +4,9 @@ import ( "github.com/tinygo-org/bluetooth/winbt" ) -// Address contains a Bluetooth address, which is a MAC address plus some extra -// information. +// Address contains a Bluetooth MAC address. type Address struct { - // The MAC address of a Bluetooth device. - MAC - isRandom bool -} - -// IsRandom if the address is randomly created. -func (ad Address) IsRandom() bool { - return ad.isRandom -} - -// SetRandom if is a random address. -func (ad Address) SetRandom(val bool) { - ad.isRandom = val -} - -// Set the address -func (ad Address) Set(val interface{}) { - ad.MAC = val.(MAC) + MACAddress } // Scan starts a BLE scan. It is stopped by a call to StopScan. A common pattern