@@ -3,25 +3,10 @@ package main
3
3
import (
4
4
"encoding/hex"
5
5
6
- "errors"
7
-
6
+ "github.com/hybridgroup/go-haystack/lib/findmy"
8
7
"tinygo.org/x/bluetooth"
9
8
)
10
9
11
- const (
12
- // Apple, Inc.
13
- appleCompanyID = 0x004C
14
-
15
- // Offline Finding type
16
- findMyPayloadType = 0x12
17
-
18
- // Length of the payload
19
- findMyPayloadLength = 0x19
20
-
21
- // Hint byte
22
- findMyHint = 0x00
23
- )
24
-
25
10
func scanDevices (verboseFlag * bool ) error {
26
11
bluetooth .DefaultAdapter .Enable ()
27
12
@@ -30,8 +15,8 @@ func scanDevices(verboseFlag *bool) error {
30
15
println ("found device:" , device .Address .String (), device .RSSI , device .LocalName ())
31
16
}
32
17
33
- if device .ManufacturerData () != nil && device .ManufacturerData ()[0 ].CompanyID == appleCompanyID {
34
- status , key , err := parseData (device .Address , device .ManufacturerData ()[0 ].Data )
18
+ if device .ManufacturerData () != nil && device .ManufacturerData ()[0 ].CompanyID == findmy . AppleCompanyID {
19
+ status , key , err := findmy . ParseData (device .Address , device .ManufacturerData ()[0 ].Data )
35
20
if err != nil {
36
21
println ("failed to parse data:" , err )
37
22
return
@@ -40,35 +25,3 @@ func scanDevices(verboseFlag *bool) error {
40
25
}
41
26
})
42
27
}
43
-
44
- func parseData (address bluetooth.Address , data []byte ) (byte , []byte , error ) {
45
- if len (data ) < 27 {
46
- return 0 , nil , errors .New ("data is too short" )
47
- }
48
-
49
- if data [0 ] != findMyPayloadType {
50
- return 0 , nil , errors .New ("invalid payload type" )
51
- }
52
-
53
- if data [1 ] != findMyPayloadLength {
54
- return 0 , nil , errors .New ("invalid payload length" )
55
- }
56
-
57
- if data [26 ] != findMyHint {
58
- return 0 , nil , errors .New ("invalid hint" )
59
- }
60
-
61
- findMyStatus := data [2 ]
62
- var key [28 ]byte
63
- copy (key [6 :], data [3 :25 ])
64
-
65
- // turn address into key bytes
66
- key [0 ] = address .MAC [5 ]
67
- key [1 ] = address .MAC [4 ]
68
- key [2 ] = address .MAC [3 ]
69
- key [3 ] = address .MAC [2 ]
70
- key [4 ] = address .MAC [1 ]
71
- key [5 ] = address .MAC [0 ]
72
-
73
- return findMyStatus , key [:], nil
74
- }
0 commit comments