找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3154|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

安卓jni學(xué)習(xí)筆記

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:639532 發(fā)表于 2019-11-11 10:28 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
Jni study

2019-2-18
在MTK6735平臺(tái)下,在eclipse.exe中做幾個(gè)按鈕調(diào)用JNI層,控制GPIO7的LED,亮滅閃爍。

eclipse使用教程:https://jingyan.baidu.com/article/0a52e3f4e241a0bf62ed72a7.html
eclipse 開發(fā) jni:https://blog.csdn.net/dg_summer/article/details/52880232
解決NDK開發(fā)中Eclipse報(bào)錯(cuò)“Unresolved inclusion jni.h”的最終方法:https://blog.csdn.net/xiaogazhang/article/details/46888781


一、JAVA的編寫
E:/%E6%9C%89%E9%81%93%E4%BA%91%E7%AC%94%E8%AE%B0/weixinobU7VjiHeyJ5xDKs8qyaJ-NAI2_E/aa8aec0db74e478ba5a48d8f7dd1bb64/482.png

    1、fragment_main.xml 如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="example.test.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="0dp"
        android:text="GPIO7 control"
        android:textSize="50sp" />
   
     <TextView
        android:id="@+id/tv_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="59dp"
        android:layout_marginTop="250dp"
        android:minHeight="100dip"
        android:minWidth="120dip"
        android:text="LED_ON" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:minHeight="100dip"
        android:minWidth="120dip"
        android:text="LED_OFF" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginLeft="56dp"
        android:layout_toRightOf="@+id/textView1"
        android:minHeight="100dip"
        android:minWidth="120dip"
        android:text="LED_HZ" />

</RelativeLayout>

    2、MainActivity.java 如下
package example.test;


import example.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


public class MainActivity extends Activity implements OnClickListener {

        private TextView tv_text;
       
       
        // 聲明自定義本地庫方法接口
        public native int LEDON();//void不能寫
        public native int LEDOFF();
        public native int LEDONOFF();
        // 自動(dòng)加載本地庫文件,如文件名全稱為 gpio.so
        static{
                System.loadLibrary("gpio");
        }
       
       
       
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加載一個(gè)布局
        setContentView(R.layout.fragment_main);     
        
        // 找到按鈕
        Button btn_call = (Button) findViewById(R.id.button1);
        Button btn_call1 = (Button) findViewById(R.id.button2);
        Button btn_call2 = (Button) findViewById(R.id.button3);

        // 給button按鈕設(shè)置一個(gè)點(diǎn)擊事件
        btn_call.setOnClickListener(this);
        btn_call1.setOnClickListener(this);
        btn_call2.setOnClickListener(this);
      
        }

         // 當(dāng)點(diǎn)擊按鈕的時(shí)候執(zhí)行
    public void onClick(View v)
    {
        switch (v.getId())
        {
                case R.id.button1: LEDOFF(); System.out.println("1按鈕被點(diǎn)擊了");  break;
                case R.id.button2: LEDON(); System.out.println("2按鈕被點(diǎn)擊了");  break;
                case R.id.button3: LEDONOFF(); System.out.println("3按鈕被點(diǎn)擊了"); break;
                default:
                    break;
        }
    }
      
}

3、JNI層編寫
          Android.mk:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS :=-llog
LOCAL_MODULE    := gpio
LOCAL_SRC_FILES := gpio.cpp

include $(BUILD_SHARED_LIBRARY)

        gpio.c:
#include <jni.h>

//#include <termios.h>
//#include <unistd.h>
//#include <sys/types.h>
//#include <sys/stat.h>
#include <fcntl.h>
//#include <string.h>
//#include <stdio.h>
//#include <stdlib.h>
//#include <dirent.h>
//#include "mtk_gpio.h"
//#include <sys/ioctl.h>
#include <linux/ioctl.h>
//#include <errno.h>
#include <android/log.h>

#define TAG "BSK_MTK_GPIO"//過濾信息用的
#define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定義LOGD類型
#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO,TAG ,__VA_ARGS__) // 定義LOGI類型
#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN,TAG ,__VA_ARGS__) // 定義LOGW類型
#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG ,__VA_ARGS__) // 定義LOGE類型
#define ALOGF(...) __android_log_print(ANDROID_LOG_FATAL,TAG ,__VA_ARGS__) // 定義LOGF類型

//#define BSK_LED_ON                _IOW('L',  1, unsigned long)
//#define BSK_LED_OFF                _IOW('L',  2, unsigned long)

extern "C" {
        JNIEXPORT jint  JNICALL  Java_example_test_MainActivity_LEDON(JNIEnv * env, jobject obj);
        JNIEXPORT jint  JNICALL  Java_example_test_MainActivity_LEDOFF(JNIEnv * env, jobject obj);
        JNIEXPORT jint  JNICALL  Java_example_test_MainActivity_LEDONOFF(JNIEnv * env, jobject obj);

//        JNIEXPORT jint  JNICALL  Java_example_gpio_MainActivity_setmode(JNIEnv * env, jobject obj, jint port,jint mode);
//        JNIEXPORT int JNICALL Java_com_bsk_xp6_MainActivity_Setthreshold(JNIEnv * env, jobject obj, jint channel);
};


JNIEXPORT jint  JNICALL  Java_example_test_MainActivity_LEDON(JNIEnv * env, jobject obj)
{
        int fd,ret;

        fd = open("/dev/led_device_file", O_RDWR);
        if(fd < 0)
        {
                ALOGD("open led_drv fb = %d\n",fd);
                return -1;
        }

        ret = ioctl(fd, 0x01, 1);//D1--on
        if(ret < 0)
                ALOGD("ioctl ret = %d\n",ret);

        close(fd);

        ALOGD("LED OFF !!!!!!!!!!!!!!!!!\n");
        return 0;
}


JNIEXPORT jint  JNICALL  Java_example_test_MainActivity_LEDOFF(JNIEnv * env, jobject obj)
{
        int fd,ret;

        fd = open("/dev/led_device_file", O_RDWR);
        if(fd < 0)
        {
                ALOGD("open led_drv\n");
                return -1;
        }

        ret = ioctl(fd, 0x02, 1);//D1--off
        if(ret < 0)
                ALOGD("ioctl ret = %d\n",ret);

        close(fd);

        ALOGD("LED ON !!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
        return 0;
}

JNIEXPORT jint  JNICALL  Java_example_test_MainActivity_LEDONOFF(JNIEnv * env, jobject obj)
{
        int fd,ret,i;

        fd = open("/dev/led_device_file", O_RDWR);
        if(fd < 0)
        {
                ALOGD("open led_drv\n");
                return -1;
        }


        ret = ioctl(fd, 0x01, 1);
        if(ret < 0)
                ALOGD("ioctl ret = %d\n",ret);

        for(i=1; i<20; i++)
        {
                usleep(200*1000);
                ioctl(fd, i%3, 1);
        }

        close(fd);

        ALOGD("LED ON OFF !!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
        return 0;
}

    PS:   cmd        --->                cd App工程目錄        --->        “ndk-build ”(該命令使cpp生成so)

二、調(diào)試JNI的內(nèi)核調(diào)試打印手段

1、靜態(tài),輸入以下命令  -》調(diào)試(按鍵按下) -ctrl+c退出 -》 查看輸出信息
adb shell cat /proc/kmsg >1.txt

2、動(dòng)態(tài),命令過濾你的內(nèi)核調(diào)試信息然后輸出
grep "MTK*" /proc/kmsg
        或者
cat /proc/kmsg | grep tttttttttttttttttttttt

三、其它問題描述

1、生成Android App時(shí)最少版本4.0以上就不會(huì)生成appcompat_v7項(xiàng)目(為了兼容低版本產(chǎn)生的)       
2、eclipse編譯apk的時(shí)候遇到問題,但沒有提示,新建android工程提示:Failed to load properties file for project
    解決:
                    Properties->Java ->Build Path->class path Varable
                    添加變量ANDROID_SDK_HOME,指向sdk目錄。
     
                    但是編譯時(shí)候卡住了,,,,
                    后來把workspace(工作空間)里的隱藏文件刪了,所有工程重新導(dǎo)入,ok了。









評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表