@@ -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
2828package main
2929
3030import (
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+
4255func 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+
5987Or 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```
0 commit comments