This is how I am detecting whether headset is connected or Disconnected. But I need to find when a credit
"Card reader" is connected to an android device. Because that also will use the same port as headset is plugged. so how to differentiate either connected device is a "card reader" or "headset". I have searched for that to find it specifically. But din't get any solution to find the "Card Reader" is plugged In/Out.
private static final String TAG = "plugstatus";
private PlugListener myReceiver;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new PlugListener();
}
@Override public void onResume() {
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(myReceiver, filter);
super.onResume();
}
private class PlugListener extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(TAG, "Headset unplugged");
break;
case 1:
Log.d(TAG, "Headset plugged");
break;
default:
Log.d(TAG, "Not able to detect plug state");
}
}
}
}
@Override public void onPause() {
unregisterReceiver(myReceiver);
super.onPause();
}
Basically we cann't detect Specifically whether it is headset or Card reader.