Chinaunix首页 | 论坛 | 博客
  • 博客访问: 670444
  • 博文数量: 207
  • 博客积分: 1743
  • 博客等级: 上尉
  • 技术积分: 2044
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-20 14:36
文章分类

全部博文(207)

文章存档

2016年(24)

2015年(10)

2014年(50)

2013年(45)

2012年(78)

分类: Android平台

2016-01-25 14:54:21

三、app调用服务接口访问硬件

上主要代码EEPROMActivity.java  

  1. package com.zkgd.eeprom;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.ServiceManager;  
  6. import android.os.IIICService;  
  7. import android.os.RemoteException;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13.   
  14. public class EEPROMActivity extends Activity  implements OnClickListener{  
  15. private final static String LOG_TAG = "com.zkgd.eeprom";    
  16.       
  17.     private IIICService iicService = null;    
  18.     
  19.     private EditText valueText = null;    
  20.     private Button readButton = null;    
  21.     private Button writeButton = null;    
  22.     private Button clearButton = null;    
  23.     int len = 1;  
  24.     /** Called when the activity is first created. */    
  25.     @Override    
  26.     public void onCreate(Bundle savedInstanceState) {    
  27.         super.onCreate(savedInstanceState);    
  28.         setContentView(R.layout.main);    
  29.     
  30.         iicService = IIICService.Stub.asInterface(    
  31.         ServiceManager.getService("iic"));    
  32.             
  33.         valueText = (EditText)findViewById(R.id.edit_value);    
  34.         readButton = (Button)findViewById(R.id.button_read);    
  35.         writeButton = (Button)findViewById(R.id.button_write);    
  36.         clearButton = (Button)findViewById(R.id.button_clear);    
  37.     
  38.     readButton.setOnClickListener(this);    
  39.     writeButton.setOnClickListener(this);    
  40.     clearButton.setOnClickListener(this);    
  41.             
  42.         Log.i(LOG_TAG, "Activity Created");    
  43.     }    
  44.         
  45.     public void onClick(View v) {    
  46.         if(v.equals(readButton)) {    
  47.         try {    
  48.         len = 1;  
  49.                 //在从设备中读取数据  
  50.                 String val =  iicService.getVal(0x50,len);      
  51.                 valueText.setText(val);    
  52.         } catch (RemoteException e) {    
  53.             Log.e(LOG_TAG, "Remote Exception while reading value from device.");    
  54.         }           
  55.         }    
  56.         else if(v.equals(writeButton)) {    
  57.         try {    
  58.                 String val = valueText.getText().toString();    
  59.                 len = val.length();   
  60.                 //在从设备的子地址处开始写入数据  
  61.                 iicService.setVal(val,0x50,0x10,len);    
  62.         } catch (RemoteException e) {    
  63.             Log.e(LOG_TAG, "Remote Exception while writing value to device.");    
  64.         }    
  65.         }    
  66.         else if(v.equals(clearButton)) {    
  67.             String text = "";    
  68.             valueText.setText(text);    
  69.         }    
  70.     }    
  71. }  
工程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";

阅读(928) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~