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
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
- Settings 添加JNI 控制
- Settings添加公司名称
- Android Settings添加选项
- Settings自己添加类
- Settings添加屏幕旋转功能
- 向Settings中添加一项
- android 添加jni函数
- JNI文件添加
- Launcher: 解决添加 Settings 快捷方式 bug_序
- android Settings添加多击事件
- android settings界面修改(添加等)
- 原生android中Settings模块添加步骤
- Android 7.0 settings中添加新菜单
- 添加系统属性:Settings.system和SystemProperties
- Android 7.0 settings中添加/删除菜单
- android 添加系统属性Settings.system
- settings
- settings
- linux下源码安装软件
- javaweb知识点总结
- 单独编译使用WebRTC的音频处理模块 - android
- COM编程小结
- 从零开始学jQuery插件开发
- Settings 添加JNI 控制
- windows+ubuntu双系统,ubuntu无法连接有线网问题
- vs2013 C/C++创建Java虚拟机执行Class字节码
- wampserver图标不显示正确颜色
- Oracle日常使用心得及sql语句
- MYSQL选取每组数据第N条信息
- boost::asio::io_service和io_service::work和boost::thread_group配合使用
- 修改framework-res.apk的内容
- 一张图解释NAT和NAPT技术