androidi2c上层接口_Android平台读写i2c设备开发笔记三

本文介绍了如何在Android平台上进行I2C设备的读写操作。通过创建一个Activity,实现点击按钮读取和写入数据到I2C设备,使用IIICService接口与设备交互,处理RemoteException。示例代码展示了从设备的特定地址读取和写入数据的过程。
摘要由CSDN通过智能技术生成

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

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值