-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
135 lines (114 loc) · 3.98 KB
/
main.go
File metadata and controls
135 lines (114 loc) · 3.98 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
// Enumerate paired/bonded Bluetooth devices and query adapter info.
//
// Demonstrates:
// - IBluetoothManager: GetState
// - RegisterAdapter -> IBluetooth: GetName, GetBondedDevices,
// GetSupportedProfiles, GetRemoteName, GetRemoteAlias, GetBondState
// - AttributionSource for authenticated calls
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/bluetooth_inventory ./examples/bluetooth_inventory/
// adb push build/bluetooth_inventory /data/local/tmp/ && adb shell /data/local/tmp/bluetooth_inventory
package main
import (
"context"
"fmt"
"os"
genBluetooth "github.com/AndroidGoLab/binder/android/bluetooth"
"github.com/AndroidGoLab/binder/android/content"
"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 }
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 (adapter may be off)")
os.Exit(0)
}
btProxy := genBluetooth.NewBluetoothProxy(btAdapterBinder)
attr := shellAttribution()
// Adapter name.
name, err := btProxy.GetName(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetName: %v\n", err)
} else {
fmt.Printf("Adapter name: %s\n", name)
}
// Supported profiles.
profiles, err := btProxy.GetSupportedProfiles(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetSupportedProfiles: %v\n", err)
} else {
fmt.Printf("Supported profiles: %v\n", profiles)
}
// Connection state.
connState, err := btProxy.GetAdapterConnectionState(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetAdapterConnectionState: %v\n", err)
} else {
fmt.Printf("Adapter connection state: %d\n", connState)
}
// Bonded (paired) devices.
devices, err := btProxy.GetBondedDevices(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetBondedDevices: %v\n", err)
os.Exit(1)
}
fmt.Printf("Bonded devices: %d\n", len(devices))
for i, dev := range devices {
fmt.Printf(" [%d] AddressType=%d\n", i, dev.AddressType)
// Query remote device info.
remoteName, err := btProxy.GetRemoteName(ctx, dev, attr)
if err == nil {
fmt.Printf(" Name: %s\n", remoteName)
}
alias, err := btProxy.GetRemoteAlias(ctx, dev, attr)
if err == nil {
fmt.Printf(" Alias: %s\n", alias)
}
bondState, err := btProxy.GetBondState(ctx, dev, attr)
if err == nil {
fmt.Printf(" Bond state: %d\n", bondState)
}
}
}