Settings 添加JNI 控制

来源:互联网 发布:烟台正浩网络老总 编辑:程序博客网 时间:2024/05/20 21:46
首先,在Settings的跟目录下创建jni文件夹,在文件夹中添加Android.mk、*.c、*.h,三个文件

android.mk的写法如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_MODULE    := libwifiledctrl
LOCAL_SRC_FILES := WifiLedCtrl.c
LOCAL_LDLIBS    := -lm -llog
include $(BUILD_SHARED_LIBRARY)

在跟目录下的android.mk也要做修改,主要添加

LOCAL_JNI_SHARED_LIBRARIES := libwifiledctrl

include $(call all-makefiles-under, jni)

2.packages/apps/Settings/jni/WifiLedCtrl.c,代码如下:

/*
 * Copyright 2009-2011 Cedric Priscal
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h>
#include "WifiLedCtrl.h"
//#include <cutils/log.h>
#include "android/log.h"
static const char *TAG="wifi-led";
//#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO,  TAG, fmt, ##args)
//#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
//#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
#define  LOG_TAG    "wifi-led"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define IDCARD_CTRL_DEV "/dev/idcard_ctrl"
#define FINGER_CTRL_DEV "/dev/fg_ctrl"

#define LED_WIFI_CTRL_DEV "/dev/led_wifi_ctrl"


#define IDCARD_CTRL_POWER_ON              _IOWR('A',0,int)
#define IDCARD_CTRL_POWER_OFF             _IOWR('A',1,int)
#define FINGER_CTRL_POWER_ON              _IOWR('A',2,int)
#define FINGER_CTRL_POWER_OFF             _IOWR('A',3,int)

#define LED_R_CTRL_POWER_ON        _IOWR('A',0,int)
#define LED_G_CTRL_POWER_ON        _IOWR('A',1,int)
#define LED_B_CTRL_POWER_ON        _IOWR('A',2,int)

#define LED_R_CTRL_POWER_OFF    _IOWR('A',3,int)
#define LED_G_CTRL_POWER_OFF    _IOWR('A',4,int)
#define LED_B_CTRL_POWER_OFF    _IOWR('A',5,int)




JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_redpoweron (JNIEnv *env, jobject thiz)
{
    int fd;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    if (fd == -1)
    {
        LOGI("wifi-poweron-red: fd is : %d ", fd);
    }
    if(ioctl(fd, LED_R_CTRL_POWER_ON, 0) < 0) {
      //  close(fd);
    }
    LOGI("wifi-poweron-red: fd is : %d ", fd);
       close(fd);
       return fd;
}
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_redpoweroff (JNIEnv *env, jobject thiz)
{
    int fd;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    if (fd == -1)
    {
        LOGI("wifi-poweroff-red: fd is : %d ", fd);
    }

    if(ioctl(fd, LED_R_CTRL_POWER_OFF, 0) < 0) {
      //  close(fd);
    }
    LOGI("wifi-poweroff-red: fd is : %d ", fd);
       close(fd);
       return fd;
}
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_readbuf (JNIEnv *env, jobject thiz)
{
    int fd;
    int buf;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    read(fd,&buf,4);

    return buf;
}


JNIEXPORT jint JNICALL
Java_com_android_settings_wifi_WifiLedCtrl_greenpoweron(JNIEnv *env, jobject thiz) {

    int fd;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    if (fd == -1)
    {
        LOGI("wifi-poweron-green: fd is : %d ", fd);
    }
    if(ioctl(fd, LED_G_CTRL_POWER_ON, 0) < 0) {
        //  close(fd);
    }
    LOGI("wifi-poweron-green: fd is : %d ", fd);
    close(fd);
    return fd;

}

JNIEXPORT jint JNICALL
Java_com_android_settings_wifi_WifiLedCtrl_greenpoweroff(JNIEnv *env, jobject thiz) {

    int fd;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    if (fd == -1)
    {
        LOGI("wifi-poweroff-green: fd is : %d ", fd);
    }

    if(ioctl(fd, LED_G_CTRL_POWER_OFF, 0) < 0) {
        //  close(fd);
    }
    LOGI("wifi-poweroff-green: fd is : %d ", fd);
    close(fd);
    return fd;

}

JNIEXPORT jint JNICALL
Java_com_android_settings_wifi_WifiLedCtrl_bluepoweron(JNIEnv *env, jobject thiz) {

    int fd;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    if (fd == -1)
    {
        LOGI("wifi-poweron-blue: fd is : %d ", fd);
    }
    if(ioctl(fd, LED_B_CTRL_POWER_ON, 0) < 0) {
        //  close(fd);
    }
    LOGI("wifi-poweron-blue: fd is : %d ", fd);
    close(fd);
    return fd;

}

JNIEXPORT jint JNICALL
Java_com_android_settings_wifi_WifiLedCtrl_bluepoweroff(JNIEnv *env, jobject thiz) {

    int fd;
    fd = open(LED_WIFI_CTRL_DEV, O_RDWR);
    if (fd == -1)
    {
        LOGI("wifi-poweroff-blue: fd is : %d ", fd);
    }

    if(ioctl(fd, LED_B_CTRL_POWER_OFF, 0) < 0) {
        //  close(fd);
    }
    LOGI("wifi-poweroff-blue: fd is : %d ", fd);
    close(fd);
    return fd;

}

3.packages/apps/Settings/jni/WifiLedCtrl.h,代码如下:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_android_settings_HighPressure */

#ifndef _Included_com_android_settings_wifi_WifiLedCtrl
#define _Included_com_android_settings_wifi_WifiLedCtrl
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_android_settings_wifi_WifiLedCtrl
 * Method:    poweron
 * Signature: ()V
 */
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_redpoweron
  (JNIEnv *, jclass);

/*
 * Class:     com_android_settings_wifi_WifiLedCtrl
 * Method:    poweroff
 * Signature: ()V
 */
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_redpoweroff
  (JNIEnv *, jclass);

/*
 * Class:     com_android_settings_wifi_WifiLedCtrl
 * Method:    poweron
 * Signature: ()V
 */
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_greenpoweron
        (JNIEnv *, jclass);

/*
 * Class:     com_android_settings_wifi_WifiLedCtrl
 * Method:    poweroff
 * Signature: ()V
 */
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_greenpoweroff
        (JNIEnv *, jclass);

/*
 * Class:     com_android_settings_wifi_WifiLedCtrl
 * Method:    bluepoweron
 * Signature: ()V
 */
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_bluepoweron
        (JNIEnv *, jclass);

/*
 * Class:     com_android_settings_wifi_WifiLedCtrl
 * Method:    bluepoweroff
 * Signature: ()V
 */
JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_bluepoweroff
        (JNIEnv *, jclass);


JNIEXPORT jint JNICALL Java_com_android_settings_wifi_WifiLedCtrl_readbuf
        (JNIEnv *, jclass);


#ifdef __cplusplus
}
#endif
#endif

4.在java文件中添加packages/apps/Settings/src/com/android/settings/wifi/WifiLedCtrl.java,代码如下:

package com.android.settings.wifi;

public class WifiLedCtrl {
    
    static {
        System.loadLibrary("wifiledctrl");
    }

    private static final String TAG = "WifiLedCtrl";

    /*
     * Do not remove or rename the field mFd: it is used by native method close();
     */

    public static void redOn(){
        closeLed();

        redpoweron();
    }
    public static void greenOn(){
        closeLed();

        greenpoweron();
    }
    public static void blueOn(){
        closeLed();

        bluepoweron();
    }
    public static void closeLed(){
        redpoweroff();
        greenpoweroff();
        bluepoweroff();
    }

    // JNI
    public native static int  redpoweron();
    public native static int  redpoweroff();

    public native static int  greenpoweron();

    public native static int  greenpoweroff();

    public native static int  bluepoweron();

    public native static int  bluepoweroff();

    public native static int  readbuf();

}

此功能是控制wifi灯的显示,
wifi指示灯要求:
1,wifi 打开 但未连接,一直显示红色;2,已连接但信号很弱 绿灯常亮;3,已连接 信号很强,蓝灯常亮。

最后文件添加完成后,make -j8 2>&1 | tee build.log 编译后会形成so文件

out/target/product/hct6735_66t_c_l1/system/priv-app/Settings/lib/arm64/libwifiledctrl.so
0 0
原创粉丝点击