Development

The app is required to import SpenRemote package and follow the steps below.

Step 1: Initialize SPenRemote in onResume() of activity

  1. Check whether S Pen features are supported in the device
  2. Check if already connected
  3. Connect to the S Pen Framework using connect() API
  4. 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);
     }
 }
 

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);
        }
    }
};

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;
} 

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.

S Pen Remote SDK Complete Code200.15 KB