Android驱动使用JNI调用(3)

四、JNI类
 在Android工程中添加一个类,包名:com.hellojnipwzh 类名:HardwareControlClass,具体内容如下:
 package com.hellojnipwzh;
 
import android.util.Log;
 
public class HardwareControlClass {
 
  static public native String stringFromJNI();
  static public native int Init();
  static public native int IOCTLLED(int ledID, int ledState ,int fd);
 
 static {
      try {
      System.loadLibrary("ledjni");
      } catch (UnsatisfiedLinkError e) {
          Log.d("HardwareControler", "HardwareControler ibrary not found!");
      }
  }
 }
 五、接下来就是在Android应用层代码中通过类来访问驱动了
 package com.hellojnipwzh;
 
import android.os.Bundle;
 import android.app.Activity;
 import android.util.Log;
 import android.view.Menu;
 import android.view.View;
 import android.widget.TextView;
 
public class HelloJniPwzh extends Activity {
 
 int fd = -1;
  TextView tv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_hello_jni_pwzh);
 
 
 
  fd = HardwareControlClass.Init();
  if (-1 == fd)
    Log.d("JNI", "open device error!");
   
  tv = (TextView)findViewById(R.id.tvid);
  String tstr = "ssss";
  tstr = HardwareControlClass.stringFromJNI();
  tv.setText(tstr);
  }
 
 @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_hello_jni_pwzh, menu);
  return true;
  }
 
  public void onclickLedOn1(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(1, 1, fd);
  }
  public void onclickLedOff1(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(1, 0, fd);
  }
 
  public void onclickLedOn2(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(2, 1, fd);
  }
  public void onclickLedOff2(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(2, 0, fd);
  }
 
  public void onclickLedOn3(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(3, 1, fd);
  }
  public void onclickLedOff3(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(3, 0, fd);
  }
 
  public void onclickLedOn4(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(4, 1, fd);
  }
  public void onclickLedOff4(View v)
  {
  if (fd != -1)
    HardwareControlClass.IOCTLLED(4, 0, fd);
  }
 
}

至此JNI的调用过程也已经完成,接下来就是将Android软件安装到Tiny210上,然后点击下按钮,你就会发现你可以通过Android软件来控制Linux驱动程序了。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1831a820672fc4e66ed56136ead48874.html