-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
153 lines (131 loc) · 4.84 KB
/
Copy pathmain.go
File metadata and controls
153 lines (131 loc) · 4.84 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
// Manage Bluetooth A2DP audio connections via binder.
//
// Demonstrates:
// - IBluetoothManager: GetState, RegisterAdapter
// - IBluetooth: GetProfile (A2DP profile ID = 2)
// - IBluetoothA2dp: GetConnectedDevices, GetActiveDevice,
// GetSupportedCodecTypes, GetConnectionState
// - AttributionSource for authenticated calls
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/bluetooth_audio_routing ./examples/bluetooth_audio_routing/
// adb push build/bluetooth_audio_routing /data/local/tmp/ && adb shell /data/local/tmp/bluetooth_audio_routing
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"
)
// A2DP profile ID in Android BluetoothProfile constants.
const profileA2DP = 2
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)
fmt.Printf("IBluetooth: handle %d\n", btAdapterBinder.Handle())
attr := shellAttribution()
// Check A2DP profile connection state at the adapter level.
a2dpState, err := btProxy.GetProfileConnectionState(ctx, profileA2DP, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetProfileConnectionState(A2DP): %v\n", err)
} else {
fmt.Printf("A2DP profile connection state: %d\n", a2dpState)
}
// Get A2DP profile binder.
a2dpBinder, err := btProxy.GetProfile(ctx, profileA2DP)
if err != nil || a2dpBinder == nil {
fmt.Println("IBluetoothA2dp: not available")
os.Exit(0)
}
a2dp := genBluetooth.NewBluetoothA2dpProxy(a2dpBinder)
fmt.Printf("IBluetoothA2dp: handle %d\n", a2dpBinder.Handle())
// Query supported codec types.
codecs, err := a2dp.GetSupportedCodecTypes(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetSupportedCodecTypes: %v\n", err)
} else {
fmt.Printf("Supported A2DP codec types: %d\n", len(codecs))
for i, c := range codecs {
fmt.Printf(" [%d] CodecType{%+v}\n", i, c)
}
}
// List connected A2DP devices.
connDevices, err := a2dp.GetConnectedDevices(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetConnectedDevices: %v\n", err)
} else {
fmt.Printf("Connected A2DP devices: %d\n", len(connDevices))
for i, dev := range connDevices {
fmt.Printf(" [%d] AddressType=%d\n", i, dev.AddressType)
connState, err := a2dp.GetConnectionState(ctx, dev, attr)
if err == nil {
fmt.Printf(" ConnectionState=%d\n", connState)
}
playing, err := a2dp.IsA2dpPlaying(ctx, dev, attr)
if err == nil {
fmt.Printf(" IsPlaying=%v\n", playing)
}
}
}
// Get active A2DP device.
activeDevice, err := a2dp.GetActiveDevice(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetActiveDevice: %v\n", err)
} else {
fmt.Printf("Active A2DP device: AddressType=%d\n", activeDevice.AddressType)
}
// Max connected audio devices.
maxDevices, err := btProxy.GetMaxConnectedAudioDevices(ctx, attr)
if err != nil {
fmt.Fprintf(os.Stderr, "GetMaxConnectedAudioDevices: %v\n", err)
} else {
fmt.Printf("Max connected audio devices: %d\n", maxDevices)
}
}