-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRokidTelemetryApp.java
More file actions
166 lines (139 loc) · 5.82 KB
/
RokidTelemetryApp.java
File metadata and controls
166 lines (139 loc) · 5.82 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
154
155
156
157
158
159
160
161
162
163
164
165
166
// Android app code to access Rokid Max2 telemetry without root
// This would be compiled into an APK and installed
package com.rokid.telemetry;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.*;
import android.os.Bundle;
import android.util.Log;
import java.nio.ByteBuffer;
import java.util.HashMap;
public class RokidTelemetryActivity extends Activity {
private static final String TAG = "RokidTelemetry";
private static final String ACTION_USB_PERMISSION = "com.rokid.telemetry.USB_PERMISSION";
private static final int ROKID_VID = 0x04D2;
private static final int ROKID_PID = 0x2002;
private UsbManager usbManager;
private UsbDevice rokidDevice;
private UsbDeviceConnection connection;
private UsbInterface hidInterface;
private UsbEndpoint hidInEndpoint;
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if(device != null){
connectToDevice(device);
}
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(usbReceiver, filter);
findRokidDevice();
}
private void findRokidDevice() {
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
if (device.getVendorId() == ROKID_VID && device.getProductId() == ROKID_PID) {
Log.d(TAG, "Found Rokid Max2!");
rokidDevice = device;
// Request permission
PendingIntent permissionIntent = PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_USB_PERMISSION), 0);
usbManager.requestPermission(device, permissionIntent);
break;
}
}
}
private void connectToDevice(UsbDevice device) {
connection = usbManager.openDevice(device);
if (connection == null) {
Log.e(TAG, "Cannot open device");
return;
}
// Find HID interface (interface 2)
for (int i = 0; i < device.getInterfaceCount(); i++) {
UsbInterface intf = device.getInterface(i);
if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_HID) {
hidInterface = intf;
Log.d(TAG, "Found HID interface: " + i);
break;
}
}
if (hidInterface == null) {
Log.e(TAG, "No HID interface found");
return;
}
// Claim interface
if (!connection.claimInterface(hidInterface, true)) {
Log.e(TAG, "Cannot claim interface");
return;
}
// Find IN endpoint
for (int i = 0; i < hidInterface.getEndpointCount(); i++) {
UsbEndpoint ep = hidInterface.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
ep.getDirection() == UsbConstants.USB_DIR_IN) {
hidInEndpoint = ep;
Log.d(TAG, "Found HID IN endpoint: 0x" +
Integer.toHexString(ep.getAddress()));
break;
}
}
// Start reading sensor data
startSensorReading();
}
private void startSensorReading() {
new Thread(() -> {
byte[] buffer = new byte[64];
// Try to enable sensors via control transfer
byte[] enableCmd = new byte[]{0x01, 0x00};
int ret = connection.controlTransfer(
0x21, // Host to device, class, interface
0x09, // SET_REPORT
0x0301, // Feature report, ID 1
2, // Interface 2
enableCmd, enableCmd.length, 1000);
Log.d(TAG, "Enable sensors: " + ret);
// Continuous read loop
while (true) {
int transferred = connection.bulkTransfer(
hidInEndpoint, buffer, buffer.length, 100);
if (transferred > 0) {
parseSensorData(buffer, transferred);
}
}
}).start();
}
private void parseSensorData(byte[] data, int length) {
// Parse IMU data based on discovered format
ByteBuffer bb = ByteBuffer.wrap(data);
bb.order(ByteOrder.LITTLE_ENDIAN);
if (length >= 24) {
short accelX = bb.getShort();
short accelY = bb.getShort();
short accelZ = bb.getShort();
short gyroX = bb.getShort();
short gyroY = bb.getShort();
short gyroZ = bb.getShort();
Log.d(TAG, String.format("IMU: A[%d,%d,%d] G[%d,%d,%d]",
accelX, accelY, accelZ, gyroX, gyroY, gyroZ));
// Send to UI or process further
runOnUiThread(() -> updateSensorDisplay(
accelX, accelY, accelZ, gyroX, gyroY, gyroZ));
}
}
}