Development
The app is required to import SpenRemote
package and follow the steps below.
Step 1: Initialize SPenRemote in onResume() of activity
- Check whether S Pen features are supported in the device
- Check if already connected
- Connect to the S Pen Framework using
connect()
API - If the connection is successful, register
SpenEventListener
for each units
public class GameActivity extends Activity {
…
private SpenUnitManager mSpenUnitManager = null;
private boolean mButtonPressed = false;
…
@Override
protected void onResume() {
…
initSpenRemote(); //initialize Spen Remote
}
private void initSpenRemote() {
//Step1-1: Check whether S Pen features are supported in the device
SpenRemote spenRemote = SpenRemote.getInstance();
if (!spenRemote.isFeatureEnabled(SpenRemote.FEATURE_TYPE_AIR_MOTION)) {
return;
}
if (!spenRemote.isFeatureEnabled(SpenRemote.FEATURE_TYPE_BUTTON)) {
return;
}
//Step1-2: Check if already connected
if (spenRemote.isConnected()) {
return;
}
//Step1-3: Connect to the S Pen Framework
mSpenUnitManager = null;
spenRemote.connect(this, new SpenRemote.ConnectionResultCallback() {
@Override
public void onSuccess(SpenUnitManager spenUnitManager) {
//Step1-4: If connection is successful, register SpenEventListener for each units.
mSpenUnitManager = spenUnitManager;
initSpenEventListener();
}
@Override
public void onFailure(int e) {
}
});
}
}
private void initSpenEventListener() {
SpenUnit buttonUnit = mSpenUnitManager.getUnit(SpenUnit.TYPE_BUTTON);
if (buttonUnit != null) {
mSpenUnitManager.registerSpenEventListener(mSpenButtonEventListener, buttonUnit);
}
SpenUnit airMotionUnit = mSpenUnitManager.getUnit(SpenUnit.TYPE_AIR_MOTION);
if (airMotionUnit != null) {
mSpenUnitManager.registerSpenEventListener(mSpenAirMotionEventListener, airMotionUnit);
}
}
Learn more by watching the video
Step 2: Handle SpenEvent on EventListener
Register Event Listener to SpenUnit
using registerSpenEventListener
method of SPenUnitManager
instance. The data of SpenEvent
passed to the SpenEventListener
is hidden. You can refer to the data by casting it as a ButtonEvent
or AirMotionEvent
class.
private SpenEventListener mSpenButtonEventListener = new SpenEventListener() {
@Override
public void onEvent(SpenEvent spenEvent) {
ButtonEvent buttonEvent = new ButtonEvent(spenEvent);
if (buttonEvent.getAction() == ButtonEvent.ACTION_DOWN) {
mButtonPressed = true;
} else {
mButtonPressed = false;
}
}
};
private SpenEventListener mSpenAirMotionEventListener = new SpenEventListener() {
@Override
public void onEvent(SpenEvent spenEvent) {
AirMotionEvent motionEvent = new AirMotionEvent(spenEvent);
if (mButtonPressed) {
mPaddleController.move(motionEvent.getDeltaX() * 400f);
}
}
};
Learn more by watching the video
Step 3: Release SpenRemote in onPause() of activity
In this part, SpenRemote
will be released during onPause()
state. disconnect()
is used to disconnect from the S Pen Framework.
@Override
protected void onPause() {
…
releaseSpenRemote();
}
private void releaseSpenRemote() {
SpenRemote spenRemote = SpenRemote.getInstance();
if (spenRemote.isConnected()) {
spenRemote.disconnect(this);
}
mSpenUnitManager = null;
}
Learn more by watching the video
Now everything is ready. Install the game on your device and launch it. The paddle should be controlled by S Pen. The game app screen should look like below.
You're done!
Congratulations! You have successfully achieved the goal of this Code Lab activity. Now, you can handle raw data of the S Pen and implement it on an app by yourself! But, if you're having trouble, you may check out the link below.