Search code examples
androidbluetoothandroid-cameraandroid-wifiandroid-hardware

Block Android device's Hardware Access Programatically


I have to write an application that would work as 'Device Hardware Admin'. The user should be able to enable or disable any feature of hardware according to his preferences. I am adding the screenshot of application UI below:

Application UI

These settings are stored in a local database of application. What I have managed to do is as follows:

Step 1: I have registered broadcast receiver for detecting all these actions.

<receiver android:name=".HardwareActionListener" >
    <intent-filter>
        <action android:name="android.intent.action.CAMERA_BUTTON" >
        </action>

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.hardware.action.NEW_PICTURE" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.media.action.IMAGE_CAPTURE" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.UMS_CONNECTED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.UMS_DISCONNECTED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED_ACTION" >
        </action>
    </intent-filter>
    <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
</receiver>

I am getting broadcasts for Wifi and bluetooth properly. I am not getting broadcasts on Camera action or USB connection and disconnection.

Question 1: Is anything wrong in intent filters I have used for Camera and USB detection?

Step 2: I want to block the user from using these hardwares. I managed to disable bluetooth or wifi just after user started it by detecting the action in my receiver class's onReceive() method.

if (intent.getAction().equalsIgnoreCase(
                "android.bluetooth.adapter.action.STATE_CHANGED")) {

    int extraBTState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR);
    switch (extraBTState) {
        case BluetoothAdapter.STATE_CONNECTING:
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
                        .getDefaultAdapter();
            mBluetoothAdapter.disable();
        break;
        case BluetoothAdapter.STATE_ON:

            BluetoothAdapter mBluetoothAdapter1 = BluetoothAdapter
                        .getDefaultAdapter();
            mBluetoothAdapter1.disable();
        break;
        case BluetoothAdapter.STATE_CONNECTED:

            BluetoothAdapter mBluetoothAdapter2 = BluetoothAdapter
                        .getDefaultAdapter();
            mBluetoothAdapter2.disable();
        break;
        case BluetoothAdapter.STATE_OFF:
            Toast.makeText(context, "Bluetooth access not allowed",
                        Toast.LENGTH_SHORT).show();
        break;
    }

} else if (intent.getAction().equalsIgnoreCase(
                "android.net.wifi.WIFI_STATE_CHANGED")) {
    int extraWifiState = intent.getIntExtra(
                    WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);
    switch (extraWifiState) {
        case WifiManager.WIFI_STATE_DISABLED:
            Toast.makeText(context, "Wifi enabling not allowed",
                        Toast.LENGTH_SHORT).show();
        break;

        case WifiManager.WIFI_STATE_ENABLED:

            WifiManager wifiManager1 = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
            wifiManager1.setWifiEnabled(false);
        break;
        case WifiManager.WIFI_STATE_ENABLING:

            WifiManager wifiManager2 = (WifiManager) context
                        .getSystemService(Context.WIFI_SERVICE);
            wifiManager2.setWifiEnabled(false);
        break;

    }

} 

So it detects that Bluetooth or WiFi is turned on or connected and turns it off or disconnects it programmatically.

But requirement is that user shouldn't be able to start Bluetooth or WiFi at all. That means disable the button of WiFi and Bluetooth.

Question 2: Is that possible?

Any help or suggestions would be appreciated.


Solution

  • Relevant APIs are available for android-14 and higher: http://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#setCameraDisabled(android.content.ComponentName, boolean)