三、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;
-
-
@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";
阅读(1023) | 评论(0) | 转发(0) |