Skip to content

Commit 495867f

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
docs: update README Quick Start to GPS listener example
Replace the GetLastLocation snippet with a live GPS callback example that demonstrates ILocationListener, NewLocationListenerStub, and RegisterLocationListener — showing real data flowing from Android to Go via binder IPC. Add gps_location to the examples table in spec2readme.
1 parent fa47ac8 commit 495867f

2 files changed

Lines changed: 79 additions & 21 deletions

File tree

README.md

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,68 @@ Includes a complete AIDL compiler that parses Android Interface Definition Langu
2222

2323
<!-- BEGIN GENERATED QUICK_START -->
2424

25-
**Go library**`go get github.com/xaionaro-go/binder` and call any service:
25+
**Go library**`go get github.com/xaionaro-go/binder` — live GPS location via binder IPC:
2626

2727
```go
2828
package main
2929

3030
import (
3131
"context"
3232
"fmt"
33-
"log"
33+
"math"
34+
"os"
35+
"time"
3436

3537
"github.com/xaionaro-go/binder/android/location"
38+
androidos "github.com/xaionaro-go/binder/android/os"
3639
"github.com/xaionaro-go/binder/binder"
3740
"github.com/xaionaro-go/binder/binder/versionaware"
3841
"github.com/xaionaro-go/binder/kernelbinder"
3942
"github.com/xaionaro-go/binder/servicemanager"
4043
)
4144

45+
// gpsListener receives location callbacks from the LocationManager.
46+
type gpsListener struct{ fixCh chan location.Location }
47+
48+
func (l *gpsListener) OnLocationChanged(_ context.Context, locs []location.Location, _ androidos.IRemoteCallback) error {
49+
for _, loc := range locs { select { case l.fixCh <- loc: default: } }
50+
return nil
51+
}
52+
func (l *gpsListener) OnProviderEnabledChanged(_ context.Context, _ string, _ bool) error { return nil }
53+
func (l *gpsListener) OnFlushComplete(_ context.Context, _ int32) error { return nil }
54+
4255
func main() {
4356
ctx := context.Background()
44-
driver, _ := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
45-
defer driver.Close(ctx)
46-
transport, _ := versionaware.NewTransport(ctx, driver, 0)
57+
drv, _ := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
58+
defer drv.Close(ctx)
59+
transport, _ := versionaware.NewTransport(ctx, drv, 0)
4760
sm := servicemanager.New(transport)
4861

4962
lm, _ := location.GetLocationManager(ctx, sm)
50-
loc, err := lm.GetLastLocation(ctx, location.FusedProvider, location.LastLocationRequest{}, binder.DefaultCallerIdentity().PackageName)
51-
if err != nil {
52-
log.Fatal(err)
63+
64+
impl := &gpsListener{fixCh: make(chan location.Location, 1)}
65+
listener := location.NewLocationListenerStub(impl)
66+
67+
request := location.LocationRequest{
68+
Provider: location.GpsProvider, IntervalMillis: 1000,
69+
ExpireAtRealtimeMillis: math.MaxInt64, DurationMillis: math.MaxInt64,
70+
}
71+
pkg := binder.DefaultCallerIdentity().PackageName
72+
_ = lm.RegisterLocationListener(ctx, location.GpsProvider, request, listener, pkg, "gps")
73+
defer lm.UnregisterLocationListener(ctx, listener)
74+
75+
select {
76+
case loc := <-impl.fixCh:
77+
fmt.Printf("Lat: %.6f Lon: %.6f Alt: %.1f m Accuracy: %.1f m\n",
78+
loc.LatitudeDegrees, loc.LongitudeDegrees, loc.AltitudeMeters, loc.HorizontalAccuracyMeters)
79+
case <-time.After(30 * time.Second):
80+
fmt.Fprintln(os.Stderr, "timed out")
5381
}
54-
fmt.Printf("Lat: %f, Lon: %f, Alt: %f\n",
55-
loc.LatitudeDegrees, loc.LongitudeDegrees, loc.AltitudeMeters)
5682
}
5783
```
5884

85+
Full runnable example: [`examples/gps_location/`](examples/gps_location/)
86+
5987
Or query power state:
6088

6189
```go
@@ -273,6 +301,7 @@ More examples: [`examples/`](examples/)
273301
| [`camera_connect`](examples/camera_connect/) | Camera device connection with callback stub |
274302
| [`device_info`](examples/device_info/) | Device properties, build info |
275303
| [`display_info`](examples/display_info/) | Display IDs, brightness, night mode |
304+
| [`gps_location`](examples/gps_location/) | Live GPS fix via ILocationListener callback |
276305
| [`list_services`](examples/list_services/) | Enumerate all binder services, ping each |
277306
| [`package_query`](examples/package_query/) | Package list, installation info |
278307
| [`power_status`](examples/power_status/) | Power supply state, charging info |
@@ -1797,6 +1826,6 @@ A [weekly workflow](.github/workflows/check-aosp-updates.yml) checks for new AOS
17971826
│ ├── hardware/ HAL interfaces
17981827
│ └── ... 666 packages total
17991828
├── com/ AOSP com.android.* service proxies
1800-
├── examples/ 13 runnable examples
1829+
├── examples/ 14 runnable examples
18011830
└── .github/workflows/ CI configuration
18021831
```

tools/cmd/spec2readme/main.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func discoverExamples(dir string) ([]exampleInfo, error) {
121121
"softap_wifi_hal": "WiFi chip info, AP interface state",
122122
"softap_tether_offload": "Tethering offload config, stats",
123123
"camera_connect": "Camera device connection with callback stub",
124+
"gps_location": "Live GPS fix via ILocationListener callback",
124125
}
125126

126127
var examples []exampleInfo
@@ -168,40 +169,68 @@ func extractDocComment(path string) string {
168169
}
169170

170171
func renderQuickStart() string {
171-
return `**Go library** — ` + "`go get github.com/xaionaro-go/binder`" + ` and call any service:
172+
return `**Go library** — ` + "`go get github.com/xaionaro-go/binder`" + ` — live GPS location via binder IPC:
172173
173174
` + "```go" + `
174175
package main
175176
176177
import (
177178
"context"
178179
"fmt"
179-
"log"
180+
"math"
181+
"os"
182+
"time"
180183
181184
"github.com/xaionaro-go/binder/android/location"
185+
androidos "github.com/xaionaro-go/binder/android/os"
182186
"github.com/xaionaro-go/binder/binder"
183187
"github.com/xaionaro-go/binder/binder/versionaware"
184188
"github.com/xaionaro-go/binder/kernelbinder"
185189
"github.com/xaionaro-go/binder/servicemanager"
186190
)
187191
192+
// gpsListener receives location callbacks from the LocationManager.
193+
type gpsListener struct{ fixCh chan location.Location }
194+
195+
func (l *gpsListener) OnLocationChanged(_ context.Context, locs []location.Location, _ androidos.IRemoteCallback) error {
196+
for _, loc := range locs { select { case l.fixCh <- loc: default: } }
197+
return nil
198+
}
199+
func (l *gpsListener) OnProviderEnabledChanged(_ context.Context, _ string, _ bool) error { return nil }
200+
func (l *gpsListener) OnFlushComplete(_ context.Context, _ int32) error { return nil }
201+
188202
func main() {
189203
ctx := context.Background()
190-
driver, _ := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
191-
defer driver.Close(ctx)
192-
transport, _ := versionaware.NewTransport(ctx, driver, 0)
204+
drv, _ := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
205+
defer drv.Close(ctx)
206+
transport, _ := versionaware.NewTransport(ctx, drv, 0)
193207
sm := servicemanager.New(transport)
194208
195209
lm, _ := location.GetLocationManager(ctx, sm)
196-
loc, err := lm.GetLastLocation(ctx, location.FusedProvider, location.LastLocationRequest{}, binder.DefaultCallerIdentity().PackageName)
197-
if err != nil {
198-
log.Fatal(err)
210+
211+
impl := &gpsListener{fixCh: make(chan location.Location, 1)}
212+
listener := location.NewLocationListenerStub(impl)
213+
214+
request := location.LocationRequest{
215+
Provider: location.GpsProvider, IntervalMillis: 1000,
216+
ExpireAtRealtimeMillis: math.MaxInt64, DurationMillis: math.MaxInt64,
217+
}
218+
pkg := binder.DefaultCallerIdentity().PackageName
219+
_ = lm.RegisterLocationListener(ctx, location.GpsProvider, request, listener, pkg, "gps")
220+
defer lm.UnregisterLocationListener(ctx, listener)
221+
222+
select {
223+
case loc := <-impl.fixCh:
224+
fmt.Printf("Lat: %.6f Lon: %.6f Alt: %.1f m Accuracy: %.1f m\n",
225+
loc.LatitudeDegrees, loc.LongitudeDegrees, loc.AltitudeMeters, loc.HorizontalAccuracyMeters)
226+
case <-time.After(30 * time.Second):
227+
fmt.Fprintln(os.Stderr, "timed out")
199228
}
200-
fmt.Printf("Lat: %f, Lon: %f, Alt: %f\n",
201-
loc.LatitudeDegrees, loc.LongitudeDegrees, loc.AltitudeMeters)
202229
}
203230
` + "```" + `
204231
232+
Full runnable example: [` + "`examples/gps_location/`" + `](examples/gps_location/)
233+
205234
Or query power state:
206235
207236
` + "```go" + `

0 commit comments

Comments
 (0)