package com.yang.jniaccesshardware;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import com.yang.hardlibrary.HardControl;
public class MainActivity extends AppCompatActivity {
private boolean led_on = false;
private Button button;
private CheckBox led1;
private CheckBox led2;
private CheckBox led3;
private CheckBox led4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.BUTTON);
HardControl.ledOpen();
led1=(CheckBox)findViewById(R.id.led1);
led2=(CheckBox)findViewById(R.id.led2);
led3=(CheckBox)findViewById(R.id.led3);
led4=(CheckBox)findViewById(R.id.led4);
button.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
HardControl hardControl = new HardControl();//依赖硬件
led_on = !led_on;
if (led_on) {
button.setText("ALL OFF");
led1.setChecked(true);
led2.setChecked(true);
led3.setChecked(true);
led4.setChecked(true);
for (int i = 0; i < 4; i++)
HardControl.ledCtrl(i, 1);
}
else {
button.setText("ALL ON");
led1.setChecked(false);
led2.setChecked(false);
led3.setChecked(false);
led4.setChecked(false);
for (int i = 0; i < 4; i++)
HardControl.ledCtrl(i, 0);
}
}
}
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.led1:
if (checked)
{
Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(1, 1);
}
else
{
Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(1, 0);
}
break;
case R.id.led2:
if (checked)
{
Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(2, 1);
}
else
{
Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(2, 0);
}
break;
case R.id.led3:
if (checked)
{
Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(3, 1);
}
else
{
Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(3, 0);
}
break;
case R.id.led4:
if (checked)
{
Toast.makeText(getApplicationContext(),"led1_on",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(4, 1);
}
else
{
Toast.makeText(getApplicationContext(),"led1_off",Toast.LENGTH_LONG).show();
HardControl.ledCtrl(4, 0);
}
break;
}
}
}
package com.yang.hardlibrary;
public class HardControl {
public static native int ledCtrl(int which, int status);
public static native int ledOpen();
public static native void ledClose();//申明三个本地方法。
static {
try {
System.loadLibrary("hardcontrol");
} catch (Exception e) {
e.printStackTrace();
}
}
}
#include <jni.h> /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <android/log.h> /* liblog */
//__android_log_print(ANDROID_LOG_DEBUG, "JNIDemo", "native add ...");
#if 0
typedef struct {
char *name; /* Java里调用的函数名 */
char *signature; /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
void *fnPtr; /* C语言实现的本地函数 */
} JNINativeMethod;
#endif
static jint fd;
jint ledOpen(JNIEnv *env, jobject cls)
{
fd = open("/dev/leds", O_RDWR);
__android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledOpen : %d", fd);
if (fd >= 0)
return 0;
else
return -1;
}
void ledClose(JNIEnv *env, jobject cls)
{
__android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledClose ...");
close(fd);
}
jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status)
{
int ret = ioctl(fd, status, which);
__android_log_print(ANDROID_LOG_DEBUG, "LEDDemo", "native ledCtrl : %d, %d, %d", which, status, ret);
return ret;
}
static const JNINativeMethod methods[] = {
{"ledOpen", "()I", (void *)ledOpen},
{"ledClose", "()V", (void *)ledClose},
{"ledCtrl", "(II)I", (void *)ledCtrl},
};
/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
JNIEnv *env;
jclass cls;
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
return JNI_ERR; /* JNI version not supported */
}
cls = (*env)->FindClass(env, "com/yang/hardlibrary/HardControl");
if (cls == NULL) {
return JNI_ERR;
}
/* 2. map java hello <-->c c_hello */
if ((*env)->RegisterNatives(env, cls, methods, sizeof(methods)/sizeof(methods[0])) < 0)
return JNI_ERR;
return JNI_VERSION_1_4;
}