Search code examples
androidsamsung-mobile-sdkstylus-pen

API to check if SPEN is attached in phone or not?


I wonder if there is a way to programmatically detect if the pen is physically in the phone (Samsung Note 8/9)? Like getting readings directly from the sensor?

I am aware that there is the SpenPenDetachmentListener which catches detach and attach events. But I need to get a status of the pen without actually taking the pen out.

I also tried using the InputDevicesManager to detect the pen as an input device, yet this won't tell whether the pen is attached or not.

Any idea would be appreciated!


Solution

  • From SPEN sdk 2.2 onwards you can detect when spen is detached. You need to setup SPEN sdk and use below method to listen to detach event.

    mSPenEventLibrary.registerSPenDetachmentListener( mContext, new SPenDetachmentListener()
    {
        @Override
        public void onSPenDetached(boolean bDetached) 
            {
            if( bDetached ) 
                       Toast.makeText( mContext, " SPen Detached",
                                     Toast.LENGTH_SHORT ).show();
            else 
                       Toast.makeText(mContext, "S Pen Inserted", Toast.LENGTH_SHORT).show();
        }
    } );
    

    You can read more documentation from here