We know that java could through JNI to call native C/C++ function.

SimpleJNI is Android example code.

I have modified for implementing BT enable/disable, just through JNI.

 

Step 1. modify Android which in SimpleJNI/jni this folder


LOCAL_MODULE_TAGS := samples

# This is the target being built.
LOCAL_MODULE:= libsimplejni


# All of the source files that we will compile.
LOCAL_SRC_FILES:= \
  native.cpp

# All of the shared libraries we link against.
LOCAL_SHARED_LIBRARIES := \
    libutils \
    libbluedroid libdbus

# No static libraries.
LOCAL_STATIC_LIBRARIES := \

# Also need the JNI headers.
LOCAL_C_INCLUDES += \
    $(JNI_H_INCLUDE) \
    external/dbus \
    system/bluetooth/bluez-clean-headers \
    external/bluez/libs/include \
    system/bluetooth/bluedroid/include


# No special compiler flags.
LOCAL_CFLAGS +=

#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
LOCAL_LDLIBS += -lpthread -ldl
#---------------------------------------------------------

# Don't prelink this library.  For more efficient code, you may want
# to add this library to the prelink map and set this to true. However,
# it's difficult to do this for applications that are not supplied as
# part of a system image.

LOCAL_PRELINK_MODULE := false

include $(BUILD_SHARED_LIBRARY)

 

Step2. Add the native function into your java code


static jint
add(JNIEnv *env, jobject thiz, jint a, jint b) {
int result = a + b;
    LOGI("%d + %d = %d", a, b, result);
    return result;
}

//enable bt JNI function
static jint
enableBT(JNIEnv *env, jobject thiz) {
    LOGV(__FUNCTION__);
    return bt_enable();
}

//disable bt JNI function
static jint
disableBT(JNIEnv *env, jobject thiz) {
    LOGV(__FUNCTION__);
    return bt_disable();
}


static const char *classPathName = "com/example/android/simplejni/Native";

static JNINativeMethod methods[] = {
  {"add", "(II)I", (void*)add },

  //Add you JNI native function
  {"enableBT", "()I", (void*)enableBT },
  {"disableBT", "()I", (void*)disableBT },

};

 

Step3. Compile SimpleJNI


Enter android this folder and type command #make SimpleJNI

It will generate two files - SimpleJNI.apk and libsimplejni.so

Put these files into your devices and execute the AP.

You will see the log will call into C/CPP function for bluetooh

 

chow it.........


 

arrow
arrow
    全站熱搜

    owenhuangtw 發表在 痞客邦 留言(2) 人氣()