1

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

mgibson
  • 6,103
  • 4
  • 34
  • 49

1 Answers1

2

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.

zapl
  • 63,179
  • 10
  • 123
  • 154
  • Cheers, that sounds promising. Good point, I asked that in a separate question. – mgibson Mar 26 '13 at 17:20
  • @mgibson it seems that you can access GPIOs by exporting them via sysfs e.g. https://groups.google.com/forum/?fromgroups=#!topic/android-porting/PJderXD-G_4 or http://stackoverflow.com/questions/8723911/linux-userspace-gpio-interrupts-using-sysfs . Requires access to the device firmware sources. You could also add a special permission (Android hardware permissions are mapped to linux group memberships) so only your contentprovider app can read/write those values. – zapl Mar 26 '13 at 17:41
  • Again, that's some good info thanks! Do you think it is feasible then to use a ContentProvider as a way to share hardware methods between various developers who don't need to see the source code? Or is a compiled .jar file more appropriate once the GPIO functions have been exposed with sysfs? Essentially looking for something similar to the Android hardware libraries e.g. Camera etc. – mgibson Mar 27 '13 at 10:25
  • @mgibson You could put some code using your ContentProvider into a jar and let people use it without need to know that it is a ContentProvider. Android's [`ContactsContract`](http://developer.android.com/reference/android/provider/ContactsContract.html) for example is an API on top of a ContentProvider. ContentProvider is a simple way to communicate cross-process. But you could use a `Service` (I think Camera etc do that) or other methods to communicate cross-process (e.g. broadcasting intents, communicating through a file, pipe, network interface...). – zapl Mar 27 '13 at 14:18