forked from muka/go-bluetooth
/
adapter_filter.go
113 lines (98 loc) · 2.69 KB
/
adapter_filter.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package adapter
import "github.com/muka/go-bluetooth/util"
const (
DiscoveryFilterTransportAuto = "auto"
DiscoveryFilterTransportBrEdr = "bredr"
DiscoveryFilterTransportLE = "le"
)
// Filter applied to discovery
type DiscoveryFilter struct {
// Filter by service UUIDs, empty means match
// _any_ UUID.
//
// When a remote device is found that advertises
// any UUID from UUIDs, it will be reported if:
// - Pathloss and RSSI are both empty.
// - only Pathloss param is set, device advertise
// TX pwer, and computed pathloss is less than
// Pathloss param.
// - only RSSI param is set, and received RSSI is
// higher than RSSI param.
UUIDs []string
// RSSI threshold value.
//
// PropertiesChanged signals will be emitted
// for already existing Device objects, with
// updated RSSI value. If one or more discovery
// filters have been set, the RSSI delta-threshold,
// that is imposed by StartDiscovery by default,
// will not be applied.
RSSI int16
// Pathloss threshold value.
//
// PropertiesChanged signals will be emitted
// for already existing Device objects, with
// updated Pathloss value.
Pathloss uint16
// Transport parameter determines the type of
// scan.
//
// Possible values:
// "auto" - interleaved scan
// "bredr" - BR/EDR inquiry
// "le" - LE scan only
//
// If "le" or "bredr" Transport is requested,
// and the controller doesn't support it,
// org.bluez.Error.Failed error will be returned.
// If "auto" transport is requested, scan will use
// LE, BREDR, or both, depending on what's
// currently enabled on the controller.
Transport string
// Disables duplicate detection of advertisement
// data.
//
// When enabled PropertiesChanged signals will be
// generated for either ManufacturerData and
// ServiceData everytime they are discovered.
DuplicateData bool
}
func (a *DiscoveryFilter) uuidExists(uuid string) bool {
for _, uiid1 := range a.UUIDs {
if uiid1 == uuid {
return true
}
}
return false
}
// Add an UUID to filter if it does not exists
func (a *DiscoveryFilter) AddUUIDs(uuids ...string) {
for _, uuid := range uuids {
if !a.uuidExists(uuid) {
a.UUIDs = append(a.UUIDs, uuid)
}
}
}
// ToMap convert to a format compatible with SetDiscoveryFilter method call
func (a *DiscoveryFilter) ToMap() map[string]interface{} {
m := make(map[string]interface{})
util.StructToMap(a, m)
if len(a.UUIDs) == 0 {
delete(m, "UUIDs")
}
if a.RSSI == 0 {
delete(m, "RSSI")
}
if a.Pathloss == 0 {
delete(m, "Pathloss")
}
return m
}
// initialize a new DiscoveryFilter
func NewDiscoveryFilter() DiscoveryFilter {
return DiscoveryFilter{
// defaults
DuplicateData: true,
Transport: DiscoveryFilterTransportAuto,
}
}