Android平台读写i2c设备开发笔记三

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



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Linux 内核的 I2C 驱动中,通常会提供设备读写节点,以便用户空间程序可以通过文件系统接口来访问 I2C 设备。这些节点一般位于 `/dev` 目录下,命名规则为 `i2c-X`,其中 X 是 I2C 控制器的编号。 例如,如果系统中有一个名为 `i2c-1` 的 I2C 控制器,那么对应的设备节点为 `/dev/i2c-1`。 在 Android 上层,可以使用 Java 的 `FileInputStream` 和 `FileOutputStream` 类来读写 I2C 设备。需要先打开对应的设备节点,然后通过文件流进行读写操作。 以下是一个简单的读写示例: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class I2CExample { private static final String I2C_DEVICE = "/dev/i2c-1"; private static final int I2C_ADDRESS = 0x50; public static void main(String[] args) { try { // 打开 I2C 设备节点 FileInputStream input = new FileInputStream(I2C_DEVICE); FileOutputStream output = new FileOutputStream(I2C_DEVICE); // 写入数据 byte[] writeBuffer = { 0x00, 0x01, 0x02 }; output.write(writeBuffer); // 读取数据 byte[] readBuffer = new byte[3]; input.read(readBuffer); // 关闭文件流 input.close(); output.close(); System.out.println("Read data: " + toHexString(readBuffer)); } catch (IOException e) { e.printStackTrace(); } } private static String toHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02X ", b)); } return sb.toString(); } } ``` 以上示例中,我们打开了 `/dev/i2c-1` 设备节点,并且使用地址 `0x50` 进行了一次写操作和一次读操作。读取到的数据会以十六进制字符串的形式输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值