Skip to content

Commit 08a1dcc

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
feat: replace gomobile examples with real gomobile bind AAR build
Replace fake println-based examples with a real gomobile-bindable package that produces an AAR via `gomobile bind`. The sensorbridge package: - Wraps sensor.Manager with int64 handle transport (gomobile bind cannot export unsafe.Pointer or uintptr) - Builds to sensorbridge.aar with generated Java classes: Bridge(long managerHandle), accelerometerName(), handle(), etc. - Includes Makefile for `make` to build the AAR - Uses separate go.mod to avoid polluting the main module Verified: `gomobile bind` produces 1.5MB AAR with correct Java API (sensorbridge/Bridge.class, sensorbridge/Sensorbridge.class).
1 parent cb11a97 commit 08a1dcc

7 files changed

Lines changed: 119 additions & 197 deletions

File tree

examples/gomobile/sensor-bridge/main.go

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.aar
2+
*-sources.jar
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
ANDROID_HOME ?= $(HOME)/Android/Sdk
2+
GOMOBILE ?= $(shell go env GOPATH)/bin/gomobile
3+
API_LEVEL ?= 35
4+
5+
AAR := sensorbridge.aar
6+
7+
.PHONY: all clean install-tools
8+
9+
all: $(AAR)
10+
11+
$(AAR): sensorbridge.go
12+
ANDROID_HOME=$(ANDROID_HOME) $(GOMOBILE) bind \
13+
-target=android/arm64 \
14+
-androidapi=$(API_LEVEL) \
15+
-o $@ .
16+
17+
install-tools:
18+
go install golang.org/x/mobile/cmd/gomobile@latest
19+
go install golang.org/x/mobile/cmd/gobind@latest
20+
$(GOMOBILE) init
21+
22+
clean:
23+
rm -f $(AAR) sensorbridge-sources.jar
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/AndroidGoLab/ndk/examples/gomobile/sensorbridge
2+
3+
go 1.25.0
4+
5+
require (
6+
github.com/AndroidGoLab/ndk v0.0.3
7+
golang.org/x/mobile v0.0.0-20260312152759-81488f6aeb60
8+
)
9+
10+
replace github.com/AndroidGoLab/ndk => ../../..
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/mobile v0.0.0-20260312152759-81488f6aeb60 h1:MOzyaj0wu2xneBkzkg9LHNYjDBB4W5vP043A2SYQRPA=
2+
golang.org/x/mobile v0.0.0-20260312152759-81488f6aeb60/go.mod h1:th6VJvzjMbrYF8SduQY5rpD0HG0GleGxjadkqSxFs3k=
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Package sensorbridge exposes NDK sensor access to Java/Kotlin
2+
// via gomobile bind.
3+
//
4+
// Build with:
5+
//
6+
// gomobile bind -target=android -androidapi=26 ./examples/gomobile/sensorbridge
7+
//
8+
// This produces sensorbridge.aar + sensorbridge-sources.jar.
9+
//
10+
// Java usage:
11+
//
12+
// import sensorbridge.Sensorbridge;
13+
// import sensorbridge.Bridge;
14+
//
15+
// long ptr = nativeGetSensorManager(); // from JNI
16+
// Bridge bridge = Sensorbridge.newBridge(ptr);
17+
// String name = bridge.accelerometerName();
18+
// bridge.close();
19+
package sensorbridge
20+
21+
import (
22+
"github.com/AndroidGoLab/ndk/sensor"
23+
)
24+
25+
// Bridge wraps an ASensorManager for cross-language use.
26+
// Unexported fields are invisible to Java/Kotlin.
27+
type Bridge struct {
28+
mgr *sensor.Manager
29+
}
30+
31+
// NewBridge creates a Bridge from a raw ASensorManager* passed as int64
32+
// (Java long). gomobile bind does not support unsafe.Pointer or uintptr,
33+
// so native handles must be transported as int64.
34+
func NewBridge(managerHandle int64) *Bridge {
35+
return &Bridge{
36+
mgr: sensor.NewManagerFromUintPtr(uintptr(managerHandle)),
37+
}
38+
}
39+
40+
// Handle returns the underlying ASensorManager* as int64 for passing
41+
// back to Java/JNI code.
42+
func (b *Bridge) Handle() int64 {
43+
return int64(b.mgr.UintPtr())
44+
}
45+
46+
// AccelerometerName returns the name of the default accelerometer,
47+
// or empty string if unavailable.
48+
func (b *Bridge) AccelerometerName() string {
49+
return b.sensorName(sensor.Accelerometer)
50+
}
51+
52+
// GyroscopeName returns the name of the default gyroscope,
53+
// or empty string if unavailable.
54+
func (b *Bridge) GyroscopeName() string {
55+
return b.sensorName(sensor.Gyroscope)
56+
}
57+
58+
// LightSensorName returns the name of the default light sensor,
59+
// or empty string if unavailable.
60+
func (b *Bridge) LightSensorName() string {
61+
return b.sensorName(sensor.Light)
62+
}
63+
64+
// SensorName returns the name of a sensor by its Android type code,
65+
// or empty string if the sensor is not available.
66+
func (b *Bridge) SensorName(sensorType int32) string {
67+
return b.sensorName(sensor.Type(sensorType))
68+
}
69+
70+
func (b *Bridge) sensorName(t sensor.Type) string {
71+
s := b.mgr.DefaultSensor(t)
72+
if s.UintPtr() == 0 {
73+
return ""
74+
}
75+
return s.Name()
76+
}
77+
78+
// Close is a no-op — the ASensorManager is a singleton owned by the
79+
// system. Provided so the Bridge satisfies Java's Closeable pattern.
80+
func (b *Bridge) Close() {
81+
b.mgr = nil
82+
}

examples/gomobile/window-interop/main.go

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)