在android打开LED方法,主要是编译库文件1

#include <stdio.h>  
#include <errno.h>  
#include <fcntl.h>  
#include <unistd.h>  
#include <stdint.h>  
#include <assert.h>  
//#include <cutils/log.h>  
#include <string.h>  
#include <jni.h>  
#include "android_runtime/AndroidRuntime.h"  
  
/* 
*解决warning: "LOG_TAG" redefined:如下 
*/  
#ifdef LOG_TAG   
#undef LOG_TAG   
#define LOG_TAG "led"   
#endif  
 
#define  LED_DEBUG  1  
  
//#define LOGE ALOGE  
//#define LOGD ALOGD  
#if LED_DEBUG  
#  define  D(...)   ALOGD(__VA_ARGS__)  
#else  
#  define  D(...)   ((void)0)  
  
/******************添加打印信息**********************/  
#include <android/log.h>  
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "keymatch", __VA_ARGS__)  
//LOGD("要看到的调试信息^_^")  
/******************添加打印信息**********************/  
#endif  
char *led_pin = "/sys/class/gpio_sw/PG11/data";  
static const char *kClassName = "com/android/led_jni/led_jni";  
/*  
 * sys_pin_output 
 */  
static void sys_pin_output(char *pin_name, int value)  
{  
    //D("opening gps  by  jiangdou\n");  
#if 1                                                     
    int fd = open(pin_name, O_RDWR);  
    if(fd <= 0){  
      // D("error in opening gpio node \n");  
        return;  
    }  
      
   // D("output %d in pin \n");  
    if(value){  
        write(fd, "1", strlen("1"));  
    }else{  
        write(fd, "0", strlen("0"));  
    }  
      
    close(fd);  
#endif  
}  
/*  
 * JNI led_on 
 */  
jint led_onNative(JNIEnv *env, jobject obj, int led)  
{  
    D("LED STUB: set %d on",led);  
    sys_pin_output(led_pin, 1); //open led  
    return 0;  
}  
/*  
 * JNI led_off 
 */  
jint led_offNative(JNIEnv *env, jobject obj, int led)  
{  
    D("LED STUB: set %d off",led);  
    sys_pin_output(led_pin,0);  //off led  
    return 0;  
  
}  
/*  
 * JNI registration. 
 */  
static JNINativeMethod gMethods[] = {  
    /* name, signature, funcPtr */  
    {"led_on",    "(I)I",                   (void*)led_onNative},  
    {"led_off",    "(I)I",                (void*)led_offNative}  
};  
#if 0  
static int register_android_led_test(JNIEnv *env)    
{    
    return android::AndroidRuntime::registerNativeMethods(env, "com/android/led_jni/led_jni", gMethods, NELEM(gMethods));    
}    
  
jint JNI_OnLoad(JavaVM *vm, void *reserved)    
{    
    JNIEnv *env = NULL;    
    if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK) {    
        printf("Error GetEnv\n");    
    return -1;    
    }    
    assert(env != NULL);    
    if (register_android_led_test(env) < 0) {    
        printf("register_android_test_hello error.\n");    
    return -1;    
    }    
    return JNI_VERSION_1_4;    
}  
#endif  
jint JNI_OnLoad(JavaVM *vm, void *reserved)  
{  
    JNIEnv *env = NULL;  
    jclass cls;  
   
    if (vm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {  
        D("current JNI not support JNI_VERSION_1_4");  
        return JNI_ERR;  
    }  
    cls = env->FindClass( kClassName);  
    if (cls == NULL) {  
        D("can not find class %s", kClassName);  
        return JNI_ERR;  
    }  
    if (env->RegisterNatives( cls, gMethods, sizeof(gMethods)/sizeof(gMethods[0])) != JNI_OK) {  
        D("can not register native methods");  
        return JNI_ERR;  
    }  
    return JNI_VERSION_1_4;  
}  

2,编写android.mk 

include $(CLEAR_VARS)  
LOCAL_PRELINK_MODULE := false  
LOCAL_SRC_FILES := led_jni.cpp  
LOCAL_MODULE := libled_jni  
LOCAL_SHARED_LIBRARIES := libandroid_runtime  
include $(BUILD_SHARED_LIBRARY)

3,编译命令:make libled_jni    -j8

4,eclipse工程建立app

//package com.example.led;  
import com.android.led_jni.led_jni;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.view.View;  
import android.widget.Button;  
  
public class MainActivity extends Activity {  
      
    /* 
     static {   
            System.loadLibrary("led_jni");     
        }   
    */  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
         //加载libledunders.so    
         
        final led_jni ledon = new led_jni();    //  实例化函数  
          
        Button bledon = (Button)findViewById(R.id.bledon);  
        Button bledoff = (Button)findViewById(R.id.bledoff);  
                //Button bledon = (Button)findViewById(R.id.bledon);  
        bledon.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO 自动生成的方法存根  
            ledon.led_on(1);    //打开LED  
              
            }  
        });  
          
        bledoff.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO 自动生成的方法存根  
                ledon.led_off(0);   //关闭LED  
              
                  
            }  
        });  
          
  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
}  


5,led_jni.java

public class led_jni {  
      
      
    static {    
            
        System.loadLibrary("led_jni");    
            
        }    
            
             
            
        public native int led_on(int a);    
        public native int led_off(int b);  
            
          
  
}  

6,xml布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/layout"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/led_test" />  
  
    <Button  
        android:id="@+id/bledon"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/ledon" />  
  
    <Button  
        android:id="@+id/bledoff"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/ledoff" />  
  
</LinearLayout></pre><br>  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值