Search code examples
androidembeddedhardwarereusability

Android - Is it possible to have a content provider of hardware interacting methods?


The company I work for is made a custom piece of hardware that runs Android. It will be connected to quite a few peripheral components. A key point is that this device is for another company.

I know with a ContentProvider, data can be shared between applications. However, what I am wondering is whether methods that interact with the hardware (GPIO interaction) can be stored in some way that they can be used by any application.

For example, say the device has a certain GPIO pin mapped to an LED. You create a method that makes the LED flash, you use it in your application, then give the device to someone else and want the code hidden but use the LED flashing method again. This is not necessary but could allow the other company to build another app complementing the one we provide with the device.

Any insight would helpful.

Cheers


Solution

  • You can use ContentProvider like a REST webinterface

    From apps:

    Uri ledUri = Uri.parse("content://your.app/led");
    ContentResolver cr = getContentResolver();
    
    // "write" data
    ContentValues cv = new ContentValues();
    cv.put("state", 1);
    cr.insert(ledUri, cv);
    
    // read data
    int newState = 0;
    Cursor c = cr.query(ledUri, new String[] { "state" }, null, null, null);
    if (c.moveToFirst()) {
        newState = c.getInt(0);
    }
    

    Inside your provider, instead of writing data into a database you simply set / read GPIO states. Roughly like

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (uri.toString().equals("content://your.app/led")) {
            int requestedState = values.getAsInteger("state");
            set_gpio_state(requestedState);
        }
    }
    

    How to acccess GPIOs from Java is another question since they are (AFAIK) only accessible on kernel level.