11package com .darmawanrobotics .x30_controller ;
22
33import android .content .Context ;
4+ import android .os .Build ;
45import android .os .Handler ;
56import android .os .Looper ;
67
1213import com .skydroid .rcsdk .common .callback .KeyListener ;
1314import com .skydroid .rcsdk .common .error .SkyException ;
1415import com .skydroid .rcsdk .key .RemoteControllerKey ;
16+ import com .skydroid .rcsdk .utils .RCSDKUtils ;
1517
1618import java .util .concurrent .Executors ;
1719import java .util .concurrent .ScheduledExecutorService ;
2224 * The sticks, keys and switches of a Skydroid handheld (H12, H12 Pro, H16, ...) through
2325 * Skydroid's RC SDK (rcsdk-*.aar in android/app/libs). The H12 answers a request for its
2426 * channel values (asked every PERIOD_MS, one request at a time); the H16 pushes them.
27+ *
28+ * On any other device (a phone, a tablet) the SDK is never started: it knows the handhelds by
29+ * their model, and on an unknown one it would only fail. Every call into it is guarded, so a
30+ * problem of the SDK reports a state and never stops the app.
2531 */
2632public final class SkydroidSticks {
2733 /** Channel values (about 1050..1950, 1500 centred) and the link state, on the main thread. */
@@ -63,13 +69,42 @@ public SkydroidSticks(Context context) {
6369 this .context = context .getApplicationContext ();
6470 }
6571
66- /** True: this build has Skydroid's SDK. */
72+ /** True: this build has Skydroid's SDK and runs on a Skydroid handheld . */
6773 public static boolean available () {
68- return true ;
74+ try {
75+ return RCSDKUtils .getDeviceType () != DeviceType .UNKNOWN ;
76+ } catch (Throwable error ) {
77+ return false ;
78+ }
79+ }
80+
81+ /** The handheld the SDK recognizes (H12, H12Pro, H16, ...), UNKNOWN on any other device. */
82+ public static String device () {
83+ try {
84+ return RCSDKUtils .getDeviceType ().name ();
85+ } catch (Throwable error ) {
86+ return "UNKNOWN" ;
87+ }
6988 }
7089
71- public synchronized void start (Listener listener ) {
90+ /**
91+ * Read the sticks for listener. force: also on a model the SDK does not recognize (the H12's
92+ * sticks chosen in the app's settings), for a handheld with an unexpected model name.
93+ */
94+ public synchronized void start (Listener listener , boolean force ) {
7295 this .listener = listener ;
96+ if (!force && !available ()) {
97+ report ("not a Skydroid handheld (" + Build .MODEL + ")" );
98+ return ;
99+ }
100+ try {
101+ connect ();
102+ } catch (Throwable error ) {
103+ report ("failed: " + error );
104+ }
105+ }
106+
107+ private void connect () {
73108 if (!initialized ) {
74109 initialized = true ;
75110 RCSDKManager .INSTANCE .initSDK (context , new SDKManagerCallBack () {
@@ -100,16 +135,27 @@ public synchronized void stop() {
100135 poller .shutdownNow ();
101136 poller = null ;
102137 }
103- KeyManager .INSTANCE .cancelListen (pushed );
138+ if (!initialized ) return ; // never started: nothing to release
139+ try {
140+ KeyManager .INSTANCE .cancelListen (pushed );
141+ RCSDKManager .INSTANCE .disconnectRC (); // else the port stays taken
142+ } catch (Throwable error ) {
143+ // the app goes on; the SDK is started again with the next listener
144+ }
104145 reading = false ;
105- RCSDKManager .INSTANCE .disconnectRC (); // else the port stays taken
106146 }
107147
108148 private synchronized void read () {
109149 if (reading ) return ;
110150 reading = true ;
111- if (RCSDKManager .INSTANCE .getDeviceType () == DeviceType .H16 ) {
112- KeyManager .INSTANCE .listen (RemoteControllerKey .INSTANCE .getKeyH16Channels (), pushed );
151+ try {
152+ if (RCSDKManager .INSTANCE .getDeviceType () == DeviceType .H16 ) {
153+ KeyManager .INSTANCE .listen (RemoteControllerKey .INSTANCE .getKeyH16Channels (), pushed );
154+ return ;
155+ }
156+ } catch (Throwable error ) {
157+ report ("failed: " + error );
158+ reading = false ;
113159 return ;
114160 }
115161 poller = Executors .newSingleThreadScheduledExecutor ();
@@ -121,7 +167,11 @@ private void ask() {
121167 long last = asked .get ();
122168 if (last != 0 && now - last < STUCK_MS ) return ; // the previous answer is still coming
123169 asked .set (now );
124- KeyManager .INSTANCE .get (RemoteControllerKey .INSTANCE .getKeyChannels (), answer );
170+ try {
171+ KeyManager .INSTANCE .get (RemoteControllerKey .INSTANCE .getKeyChannels (), answer );
172+ } catch (Throwable error ) {
173+ asked .set (0 ); // a scheduled task that throws is never run again: keep asking
174+ }
125175 }
126176
127177 private void deliver (int [] channels ) {
0 commit comments