Android platform read and write I2C Device Development NOTE 3

Source: Internet
Author: User

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 ";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.