下面代码主要实现接口定义,实现从.so库文件接口函数在JAVA里面的声明;
package com.friendlyarm.AndroidSDK;
import android.util.Log; public class HardwareControler
{
/* Serial Port */
static public native int openSerialPort( String devName, long baud, int dataBits, int stopBits ); /* LED */
static public native int setLedState( int ledID, int ledState ); /* PWM */
static public native int PWMPlay(int frequency);
static public native int PWMStop(); /* ADC */
static public native int readADC(); /* I2C */
static public native int openI2CDevice();
static public native int writeByteDataToI2C(int fd, int pos, byte byteData);
static public native int readByteDataFromI2C(int fd, int pos); /* 通用接口 */
static public native int write(int fd, byte[] data);
static public native int read(int fd, byte[] buf, int len);
static public native int select(int fd, int sec, int usec);
static public native void close(int fd); static {
try {
System.loadLibrary("friendlyarm-hardware");
} catch (UnsatisfiedLinkError e) {
Log.d("HardwareControler", "libfriendlyarm-hardware library not found!");
}
}
}
APK的界面部分主要是下面代码实现:
package com.friendlyarm.LEDDemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener; import com.friendlyarm.AndroidSDK.HardwareControler; public class LEDTestingActivity extends Activity implements OnClickListener { private Button btnLED1On;
private Button btnLED1Off;
private Button btnLED2On;
private Button btnLED2Off;
private Button btnLED3On;
private Button btnLED3Off;
private Button btnLED4On;
private Button btnLED4Off; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); btnLED1On = (Button)findViewById(R.id.btnLED1On);
btnLED1Off = (Button)findViewById(R.id.btnLED1Off);
btnLED2On = (Button)findViewById(R.id.btnLED2On);
btnLED2Off = (Button)findViewById(R.id.btnLED2Off);
btnLED3On = (Button)findViewById(R.id.btnLED3On);
btnLED3Off = (Button)findViewById(R.id.btnLED3Off);
btnLED4On = (Button)findViewById(R.id.btnLED4On);
btnLED4Off = (Button)findViewById(R.id.btnLED4Off);
btnLED1On.setOnClickListener(this);
btnLED1Off.setOnClickListener(this);
btnLED2On.setOnClickListener(this);
btnLED2Off.setOnClickListener(this);
btnLED3On.setOnClickListener(this);
btnLED3Off.setOnClickListener(this);
btnLED4On.setOnClickListener(this);
btnLED4Off.setOnClickListener(this);
} public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLED1On:
HardwareControler.setLedState(0,1);
break;
case R.id.btnLED1Off:
HardwareControler.setLedState(0,0);
break;
case R.id.btnLED2On:
HardwareControler.setLedState(1,1);
break;
case R.id.btnLED2Off:
HardwareControler.setLedState(1,0);
break;
case R.id.btnLED3On:
HardwareControler.setLedState(2,1);
break;
case R.id.btnLED3Off:
HardwareControler.setLedState(2,0);
break;
case R.id.btnLED4On:
HardwareControler.setLedState(3,1);
break;
case R.id.btnLED4Off:
HardwareControler.setLedState(3,0);
break;
default:
break;
}
}
}
还有一个库文件;
后面再弄上库文件的源代码;现在还没写出来,哈哈;