Android平台讀寫i2c裝置開發筆記三

來源:互聯網
上載者:User

三、app調用服務介面訪問硬體

上主要代碼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;                //在從裝置中讀取資料                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();                 //在從裝置的子地址處開始寫入資料                iicService.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);          }      }  }

工程eeprom放置在源碼目錄package/app/下

編譯命令:mmm package/app/eeprom

打包,燒寫韌體至開發板,啟動就可以看到該應用的表徵圖了。

小結:

整個調用流程為:app<---AIDL訪問服務<---JNI本地方法實現<---HALso檔案<---硬體

一個問題,這種方法改動了android原生api,畢竟是訪問了硬體。如果想做通用app又想使用c/c++提高效率,直接進行NDK開發,功能編譯成庫檔案打進app應用的工程中。

另一個問題,硬體訪問會遭遇到許可權問題。如果做通用app,需要裝置root了,然後在代碼裡添加許可權修改操作,例如:"chmod 777 "+getPackageCodePath(); "chmod 777 /dev/i2c-1";

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.