-2

I know there is something for WiFi and Bluetooth by using intent filter -> ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECT_REQUESTED, and ACTION_ACL_DISCONNECTED...But is there something in general to check if any kind of connected device has been removed. For example: I have connected my Android device to my Mac, now the Smartphone is locked and i remove the connection to my Mac and i can trigger a local push or sound or something in this direction...

Update

Tried the answer from @Rajesh Gopu which looks good but unfortunately didn´t work :(

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter filter = new IntentFilter();
    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiver, filter); //register
    Toast.makeText(getApplicationContext(), "ON CREATE", Toast.LENGTH_LONG).show();

}

// BroadcastReceiver when insert/remove the device USB plug into/from a USB port
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {//get Event
        Toast.makeText(getApplicationContext(), "ON RECEIVE", Toast.LENGTH_LONG).show();

        String action = intent.getAction();
        System.out.println("BroadcastReceiver Event");
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            Toast.makeText(getApplicationContext(), "CONNECTED", Toast.LENGTH_LONG).show();
            System.out.println("BroadcastReceiver USB Connected");
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            Toast.makeText(getApplicationContext(), "DISCONNECTED", Toast.LENGTH_LONG).show();
            System.out.println("BroadcastReceiver USB Disconnected");
        }
    }
};
}
BigPun86
  • 2,480
  • 2
  • 25
  • 49
  • "I have connected my Android device to my Mac" -- connected it... how? – CommonsWare Oct 20 '16 at 12:26
  • Doesn´t really matter. I mean any kind of connection/disconnection listeners... Bluetooth and WiFi is easy to listen if it has been disconnected, but how about connected with the usb cable for example. – BigPun86 Oct 20 '16 at 12:31
  • "Doesn´t really matter" -- yes, it does. For example, there is no listener to determine whether or not you have connected your device to your Mac by a piece of string. APIs of this nature in Android tend to be focused on the connection technology, not the target of the connection. "how about connected with the usb cable for example" -- that would likely depend upon the specific use case for the cable (`adb`? MTP for mounting external storage as a volume? USB host/accessory APIs? something else?). – CommonsWare Oct 20 '16 at 12:35
  • 1
    I wouldn't worry about a device being removed. Only connected. There is simply no need to tell if a device is removed. If your app didn't detect a change in the first place then it dosnt matter. – NightSkyCode Oct 20 '16 at 13:19

1 Answers1

2

Use Broadcaster receiver , Android system informs the internel events by brodcasting it . THis can be received by App's by registering the action with broadcastrecever .

Below is an exmaple to recive usb connect/disconnect event.

 IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter); //register  

  // BroadcastReceiver when insert/remove the device USB plug into/from a USB port  
  BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  public void onReceive(Context context, Intent intent) {//get Event
    String action = intent.getAction();
    System.out.println("BroadcastReceiver Event");
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
        System.out.println("BroadcastReceiver USB Connected");
    } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
        System.out.println("BroadcastReceiver USB Disconnected");
    }
  }

}

Rajesh Gopu
  • 863
  • 9
  • 33