Game Development Reference
In-Depth Information
public void run() {
DiscoverAndConnect();
}
}).start();
}
This fragment starts a thread that invokes the native method DiscoverAndConnect . This
in turn invokes the C function in Listing 8-9, thus starting the discovery and connection
process in the background. To tell the main loop in Listing 8-9 to do something such as
rumble, turn on a LED, or quit, a signal is turned on within the JNI interface, as shown
in Listing 8-10.
Listing 8-10. JNI Functions to Toggle Rumble, Get Status, Turn On LEDs, or Disconnect Actions
JNIEXPORT void JNICALL Java_game_engine_controller_Wiimote_ToggleRumble
(JNIEnv * env, jclass cls)
{
sig_toggle_rumble = 1;
}
JNIEXPORT void JNICALL Java_game_engine_controller_Wiimote_Status
(JNIEnv * env, jclass cls)
{
sig_get_status = 1;
}
JNIEXPORT void JNICALL Java_game_engine_controller_Wiimote_SetLed
(JNIEnv * env, jclass cls, jint led)
{
LOGD("Set LED %2X", led);
sig_setled = led;
}
JNIEXPORT void JNICALL Java_game_engine_controller_Wiimote_DisconnectAll
(JNIEnv * env, jclass cls)
{
if ( nmotes == 0 ) {
LOGE("No wiimotes to disconnect");
return;
}
sig_disconnect = 1; // Send disconnect
sig_exit = 1; // quit main loop
}
When any of the JNI functions in Listing 8-10 gets called from Java, a signal is set, which
is in turn checked by the main loop of the connection function in Listing 8-9. For example,
the user presses the Rumble button in the Android app, so the rumble() method in the
Wiimote Java class gets called, which then invokes the JNI C function Java_game_engine_
controller_Wiimote_ToggleRumble . This function enables the rumble signal. Finally, the
connect main loop sees this signal is enabled and invokes the corresponding WiiC system
call to request a rumble report through the WiiC Data Pipe socket connected to the device.
 
Search WWH ::




Custom Search