3. The app calls a service interface to access hardware
The main code on eepromactivity. Java
Package com. zkgd. eeprom; import android. app. activity; import android. OS. bundle; import android. OS. serviceManager; import android. OS. IIICService; import android. OS. remoteException; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class EEPROMActivity extends Activity implements OnClickListener {private final static String LOG_TAG = "com. zkgd. eeprom "; private IIICService iicService = null; private EditText valueText = null; private Button readButton = null; private Button writeButton = null; private Button clearButton = null; int len = 1; /** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); iicService = IIICService. stub. asInterface (ServiceManager. getService ("iic"); valueText = (EditText) findViewById (R. id. edit_value); readButton = (Button) findViewById (R. id. button_read); writeButton = (Button) findViewById (R. id. button_write); clearButton = (Button) findViewById (R. id. button_clear); readButton. setOnClickListener (this); writeButton. setOnClickListener (this); clearButton. setOnClickListener (this); Log. I (LOG_TAG, "Activity Created");} public void onClick (View v) {if (v. equals (readButton) {try {len = 1; // read data from the device String val = iicService. getVal (0x50, len); valueText. setText (val);} catch (RemoteException e) {Log. e (LOG_TAG, "Remote Exception while reading value from device. ") ;}} else if (v. equals (writeButton) {try {String val = valueText. getText (). toString (); len = val. length (); // write data to iicService from the sub-address of the device. setVal (val, 0x50, 0x10, len);} catch (RemoteException e) {Log. e (LOG_TAG, "Remote Exception while writing value to device. ") ;}} else if (v. equals (clearButton) {String text = ""; valueText. setText (text );}}}
Project EEPROM is placed in the source code directory package/APP/
Compile command: Mmm package/APP/EEPROM
Package, burn the firmware to the Development Board, and start the application to view the icon.
Summary:
The entire call process is: app <--- aidl Access Service <--- JNI local method implementation <--- halso file <--- hardware
One problem is that this method changes the android native API. After all, it accesses the hardware. If you want to make a general app and want to use C/C ++ to improve efficiency, you can directly develop the ndk and compile the function into library files into the app project.
Another problem is hardware access permissions. If you do a generic app, you need to root the device, and then add permission modification operations in the Code, such as: "chmod 777" + getpackagecodepath (); "chmod 777/dev/i2c-1 ";