-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
183 lines (161 loc) · 5.48 KB
/
main.go
File metadata and controls
183 lines (161 loc) · 5.48 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Query Bluetooth adapter status and scan for BLE devices via binder.
//
// Demonstrates:
// - IBluetoothManager proxy: adapter state
// - RegisterAdapter → IBluetooth proxy: GetBluetoothGatt, GetBluetoothScan
// - IBluetoothScan proxy: RegisterScanner, StartScan, StopScan
// - BLE scan callback delivery
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/bluetooth_status ./examples/bluetooth_status/
// adb push build/bluetooth_status /data/local/tmp/ && adb shell /data/local/tmp/bluetooth_status
package main
import (
"context"
"fmt"
"os"
"time"
genBluetooth "github.com/AndroidGoLab/binder/android/bluetooth"
genLE "github.com/AndroidGoLab/binder/android/bluetooth/le"
"github.com/AndroidGoLab/binder/android/content"
genOs "github.com/AndroidGoLab/binder/android/os"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
type noopManagerCallback struct{}
func (noopManagerCallback) OnBluetoothServiceUp(context.Context, binder.IBinder) error { return nil }
func (noopManagerCallback) OnBluetoothServiceDown(context.Context) error { return nil }
func (noopManagerCallback) OnBluetoothOn(context.Context) error { return nil }
func (noopManagerCallback) OnBluetoothOff(context.Context) error { return nil }
type scanSpy struct {
registeredCh chan int32
results chan genLE.ScanResult
}
func (s *scanSpy) OnScannerRegistered(_ context.Context, status, scannerID int32) error {
if status != 0 {
fmt.Fprintf(os.Stderr, "scanner registration failed: status=%d\n", status)
}
select {
case s.registeredCh <- scannerID:
default:
}
return nil
}
func (s *scanSpy) OnScanResult(_ context.Context, result genLE.ScanResult) error {
select {
case s.results <- result:
default:
}
return nil
}
func (s *scanSpy) OnBatchScanResults(context.Context, []genLE.ScanResult) error { return nil }
func (s *scanSpy) OnFoundOrLost(context.Context, bool, genLE.ScanResult) error { return nil }
func (s *scanSpy) OnScanManagerErrorCallback(context.Context, int32) error { return nil }
func shellAttribution() content.AttributionSource {
return content.AttributionSource{
AttributionSourceState: content.AttributionSourceState{
Pid: int32(os.Getpid()),
Uid: int32(os.Getuid()),
PackageName: "com.android.shell",
},
}
}
func main() {
ctx := context.Background()
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer driver.Close(ctx)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "transport: %v\n", err)
os.Exit(1)
}
defer transport.Close(ctx)
sm := servicemanager.New(transport)
btMgrSvc, err := sm.GetService(ctx, servicemanager.ServiceName("bluetooth_manager"))
if err != nil {
fmt.Fprintf(os.Stderr, "bluetooth_manager: %v\n", err)
os.Exit(1)
}
mgr := genBluetooth.NewBluetoothManagerProxy(btMgrSvc)
state, _ := mgr.GetState(ctx)
fmt.Printf("Bluetooth state: %d\n", state)
callback := genBluetooth.NewBluetoothManagerCallbackStub(noopManagerCallback{})
btAdapterBinder, err := mgr.RegisterAdapter(ctx, callback)
if err != nil || btAdapterBinder == nil {
fmt.Println("IBluetooth: not available")
os.Exit(0)
}
btProxy := genBluetooth.NewBluetoothProxy(btAdapterBinder)
fmt.Printf("IBluetooth: handle %d\n", btAdapterBinder.Handle())
gattBinder, err := btProxy.GetBluetoothGatt(ctx)
if err == nil && gattBinder != nil {
fmt.Printf("IBluetoothGatt: handle %d\n", gattBinder.Handle())
}
scanBinder, err := btProxy.GetBluetoothScan(ctx)
if err != nil || scanBinder == nil {
fmt.Println("IBluetoothScan: not available")
os.Exit(0)
}
scanProxy := genBluetooth.NewBluetoothScanProxy(scanBinder)
fmt.Printf("IBluetoothScan: handle %d\n", scanBinder.Handle())
spy := &scanSpy{
registeredCh: make(chan int32, 1),
results: make(chan genLE.ScanResult, 100),
}
scanCallback := genLE.NewScannerCallbackStub(spy)
if err := scanProxy.RegisterScanner(ctx, scanCallback, genOs.WorkSource{}, shellAttribution()); err != nil {
fmt.Fprintf(os.Stderr, "registerScanner: %v\n", err)
os.Exit(1)
}
var scannerID int32
select {
case scannerID = <-spy.registeredCh:
fmt.Printf("Scanner registered: id=%d\n", scannerID)
case <-time.After(5 * time.Second):
fmt.Fprintln(os.Stderr, "scanner registration timed out")
os.Exit(1)
}
ss := genLE.ScanSettings{
CallbackType: 1,
MatchMode: 1,
NumOfMatchesPerFilter: 3,
Phy: 255,
}
if err := scanProxy.StartScan(ctx, scannerID, ss, nil, shellAttribution()); err != nil {
fmt.Fprintf(os.Stderr, "startScan: %v\n", err)
os.Exit(1)
}
fmt.Println("Scanning for BLE devices (3s)...")
deadline := time.After(3 * time.Second)
count := 0
rssiMin, rssiMax := int32(0), int32(-128)
loop:
for {
select {
case result := <-spy.results:
count++
if result.Rssi < rssiMin {
rssiMin = result.Rssi
}
if result.Rssi > rssiMax {
rssiMax = result.Rssi
}
case <-deadline:
break loop
}
}
fmt.Printf("BLE scan results: %d callbacks in 3s\n", count)
if count > 0 {
fmt.Printf("RSSI range: %d to %d dBm\n", rssiMin, rssiMax)
}
if err := scanProxy.StopScan(ctx, scannerID, shellAttribution()); err != nil {
fmt.Fprintf(os.Stderr, "stopScan: %v\n", err)
}
}