android 串口通讯2-对JNI封装JAVA的类,方便别人的调用

来源:互联网 发布:ubuntu 14.04安装ssh 编辑:程序博客网 时间:2024/05/22 19:33

前面忘记最后在jni目录,执行ndk-build了,执行这个后就会有workspace/uart/libs/armeabi下得到libuart.so了。


今天的目标是在uart/src/com/android/uart建一个Uartjni.java封装类,

内容如下:

package com.android.uart;
import android.util.Log;
public class Uartjni {
    static
    {
        try
        {
            System.loadLibrary("uart");
            Log.i("JIN","Trying to load libuart.so");
        }
        catch(UnsatisfiedLinkError ule)
        {
            Log.e("JIN","WARNING:could not load libuart.so");
        }
    }


    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // open命令函数
    // * Input:        // /dev/ttyS6,/dev/ttyS7, /dev/rfid_rc522_dev
    // * Output:       //
    // * Return:       // 大于0成功。小于0失败
    // * Others:       // 无
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native int openUart(String str);  


    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // close命令函数
    // * Called By:    // 
    // * Input:        // 
    // * Output:       //
    // * Return:       // 
    // * Others:       // 无
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native void closeUart();
    


    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // 设置波特率命令函数
    // * Called By:    // { "1200", "2400", "4800", "9600","19200","38400","57600","115200","230400","921600"};
    // * Input:        // 
    // * Output:       //
    // * Return:       // 返回大于0或者小于0的数据,小于0证明失败,这个时候JNI会自动close设备,如果需要打开则需要重新OPEN
    // * Others:       // 
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native int setUart(int baudrate);
    


    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // 写命令函数
    // * Called By:    // 其中str为字符型数据
    // * Input:        // 
    // * Output:       //
    // * Return:       // 
    // * Others:       // 
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native int sendMsgUart(String str);
    
    


    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // 读命令函数
    // * Called By:    // 
    // * Input:        // 
    // * Output:       //
    // * Return:       // NULL或者字符型数据 
    // * Others:       // 
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native String  receiveMsgUart();
    
    


    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // 写16进制
    // * Called By:    //  
    // * Input:        // 
    // * Output:       //
    // * Return:       //
    // * Others:       // java中比如:int wfliao[] = new int[10]; 然后for(int i=0;i< 10; i++)  wfliao[i]=0x12 ........
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native int  sendHexUart(int[] arr);
    
    
    //------------------------------------------------------------------------------------------------------------
    ///*
    // * Function:     // 485 写16进制
    // * Called By:    // 
    // * Input:        // 
    // * Output:       //
    // * Return:       //
    // * Others:       // java中直接 传整形数,0----255的范围
    // */
    // -----------------------------------------------------------------------------------------------------------
    public static native int  send485HexUart(int number);
    


}

0 0
原创粉丝点击