Android底层字符传递给上层应用举例

Android底层字符传递给上层应用举例

作者:任程明,华清远见嵌入式培训讲师

1、Android的硬件抽象层读取相应的串口设备的数据:

static int uart_read_drv(struct uart_control_device_t *dev, char *buf, int count)
        {
                int count1 = 0;
                count1 = read(fd, buf, count);
                if(count1 < 0)
                {
                        perror("read");
                }
                return 0;
        }

2、 Android的JNI层经过回调之后,可以通过自定义的相应的结构体方法读取到硬件抽象层的数据,并反馈到上层:

static jchar uart_read(JNIEnv* env, jobject thiz) {
                char s_buf[100]={0};
                if (sUartDevice) {
                        sUartDevice->uart_read_hal(sUartDevice, s_buf, 1);
                }else{
                        LOGI("sUartDevice is null");
                }
                if (s_buf == 0)
                LOGI("JNI: Lost info");
                else
                {
                        LOGI("***********JNI:info length is : %d", strlen(s_buf));
                }
                return s_buf[0];
        }

3、Android的JNI和上层函数的对应描述:

static const JNINativeMethod gMethods[] = {
        ….
        { "_uart_data_update",
                "()C",
                (void*)uart_read },
                ….
        };

4、Android的Framework层定义相应的本地接口方法:

private static native char _uart_data_update();

通过类的方法进行封装:
        public class Uart {
                static {
                System.loadLibrary("uart_runtime");
                Log.i("**********************Java Service", "loadLibrary sucess");
                }
                 public Uart(){
                        _init();
                }
                public char GpsDataUpdate() {
                char s = _uart_data_update();
                        return s;
                }
                private static native boolean _init();
                private static native char _uart_data_update();
                 }

}

5、.Java上层App进行调用,并获得数据:

static Uart uartuart = new Uart();
        char s = uartuart.GpsDataUpdate();

转载自:http://www.embedu.org/Column/Column520.htm

你可能感兴趣的:(android)