-
Notifications
You must be signed in to change notification settings - Fork 1
/
ibeacon.go
44 lines (36 loc) · 847 Bytes
/
ibeacon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package tilt
import (
"encoding/binary"
"encoding/hex"
"errors"
)
//iBeaconType identifies an iBeacon
const iBeaconType uint32 = 0x4c000215
// ErrNotBeacon - the BLE device is not an iBeacon
var ErrNotBeacon = errors.New("Not an iBeacon")
// IBeacon data
type IBeacon struct {
UUID string
Major uint16
Minor uint16
}
// IsIBeacon to determine if data is an iBeacon
func IsIBeacon(data []byte) bool {
if len(data) < 25 || binary.BigEndian.Uint32(data) != iBeaconType {
return false
}
return true
}
// NewIBeacon creates an iBeacon from a valid BLE event
func NewIBeacon(data []byte) (b *IBeacon, err error) {
if !IsIBeacon(data) {
err = ErrNotBeacon
return
}
b = &IBeacon{
UUID: hex.EncodeToString(data[4:20]),
Major: binary.BigEndian.Uint16(data[20:22]),
Minor: binary.BigEndian.Uint16(data[22:24]),
}
return
}