新增离线语音,新增离线语音配置语音界面
This commit is contained in:
@ -18,7 +18,7 @@ LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
TARGET_PLATFORM := android-8
|
||||
TARGET_PLATFORM := android-3
|
||||
LOCAL_MODULE := serial_port
|
||||
LOCAL_SRC_FILES := SerialPort.c
|
||||
LOCAL_LDLIBS := -llog
|
||||
|
||||
1
app/jni/Application.mk
Normal file
1
app/jni/Application.mk
Normal file
@ -0,0 +1 @@
|
||||
APP_ABI := armeabi armeabi-v7a x86
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2009 Cedric Priscal
|
||||
* 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.
|
||||
@ -22,52 +22,14 @@
|
||||
#include <string.h>
|
||||
#include <jni.h>
|
||||
|
||||
#include "SerialPort.h"
|
||||
|
||||
#include "android/log.h"
|
||||
static const char *TAG="serial_port";
|
||||
#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)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: com_android_barcode_SerialPort
|
||||
* Method: open
|
||||
* Signature: (Ljava/lang/String;I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
|
||||
(JNIEnv *, jclass, jstring, jint);
|
||||
|
||||
/*
|
||||
* Class: com_android_barcode_SerialPort
|
||||
* Method: write
|
||||
* Signature: (ILjava/lang/String;I)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_write
|
||||
(JNIEnv *, jobject, jint, jstring, jint);
|
||||
|
||||
/*
|
||||
* Class: com_android_barcode_SerialPort
|
||||
* Method: read
|
||||
* Signature: (II)[B
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL Java_com_android_barcode_SerialPort_read
|
||||
(JNIEnv *, jobject, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: com_android_barcode_SerialPort
|
||||
* Method: close
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_com_android_barcode_SerialPort_close
|
||||
(JNIEnv *, jobject, jint);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
static speed_t getBaudrate(jint baudrate)
|
||||
{
|
||||
switch(baudrate) {
|
||||
@ -107,15 +69,16 @@ static speed_t getBaudrate(jint baudrate)
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: cedric_serial_SerialPort
|
||||
* Class: android_serialport_SerialPort
|
||||
* Method: open
|
||||
* Signature: (Ljava/lang/String;)V
|
||||
* Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
|
||||
(JNIEnv *env, jobject thiz, jstring path, jint baudrate)
|
||||
JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open
|
||||
(JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
|
||||
{
|
||||
int fd;
|
||||
speed_t speed;
|
||||
jobject mFileDescriptor;
|
||||
|
||||
/* Check arguments */
|
||||
{
|
||||
@ -123,6 +86,7 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
|
||||
if (speed == -1) {
|
||||
/* TODO: throw an exception */
|
||||
LOGE("Invalid baudrate");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,8 +94,8 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
|
||||
{
|
||||
jboolean iscopy;
|
||||
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
|
||||
LOGD("Opening serial port %s", path_utf);
|
||||
fd = open(path_utf, O_RDWR | O_SYNC);
|
||||
LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
|
||||
fd = open(path_utf, O_RDWR | flags);
|
||||
LOGD("open() fd = %d", fd);
|
||||
(*env)->ReleaseStringUTFChars(env, path, path_utf);
|
||||
if (fd == -1)
|
||||
@ -139,6 +103,7 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
|
||||
/* Throw an exception */
|
||||
LOGE("Cannot open port");
|
||||
/* TODO: throw an exception */
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,136 +116,52 @@ JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_open
|
||||
LOGE("tcgetattr() failed");
|
||||
close(fd);
|
||||
/* TODO: throw an exception */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cfmakeraw(&cfg);
|
||||
cfsetispeed(&cfg, speed);
|
||||
cfsetospeed(&cfg, speed);
|
||||
/*******************************/
|
||||
cfg.c_cflag &= ~CSIZE;
|
||||
cfg.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
|
||||
cfg.c_oflag &= ~OPOST;
|
||||
//'8' bit
|
||||
cfg.c_cflag |= CS8;
|
||||
//'N' PARENB
|
||||
cfg.c_cflag &= ~PARENB;
|
||||
cfg.c_iflag &= ~INPCK;
|
||||
//'1' STOP
|
||||
cfg.c_cflag &= ~CSTOPB;
|
||||
|
||||
cfg.c_cc[VTIME] = 15;
|
||||
cfg.c_cc[VMIN] = 0;
|
||||
tcflush(fd, TCIFLUSH);
|
||||
/*********************************/
|
||||
|
||||
if (tcsetattr(fd, TCSANOW, &cfg))
|
||||
{
|
||||
LOGE("tcsetattr() failed");
|
||||
close(fd);
|
||||
/* TODO: throw an exception */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return fd;
|
||||
}
|
||||
/*
|
||||
* Class: cedric_serial_SerialPort
|
||||
* Method: write
|
||||
* Signature: ()V
|
||||
*/
|
||||
|
||||
JNIEXPORT jint JNICALL Java_com_android_barcode_SerialPort_write
|
||||
(JNIEnv *env, jobject obj, jint fd, jstring str, jint len)
|
||||
{
|
||||
jboolean iscopy;
|
||||
const char *buff_utf = (*env)->GetStringUTFChars(env, str, &iscopy);
|
||||
int wlen = 0;
|
||||
wlen = write(fd, buff_utf, len);
|
||||
if(wlen > 0)
|
||||
{
|
||||
LOGD("Write serial port %s\n", buff_utf);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGE("Write failed\n");
|
||||
}
|
||||
(*env)->ReleaseStringUTFChars(env, str, buff_utf);
|
||||
LOGD("Write finish!\n");
|
||||
return wlen;
|
||||
}
|
||||
/*
|
||||
* Class: cedric_serial_SerialPort
|
||||
* Method: read
|
||||
* Signature: ()V
|
||||
*/
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL Java_com_android_barcode_SerialPort_read
|
||||
(JNIEnv *env, jobject obj, jint fd, jint len)
|
||||
{
|
||||
int reval;
|
||||
int nread = 0;
|
||||
char buff[len];
|
||||
char result[len];
|
||||
struct timeval tv;
|
||||
jbyteArray jba;
|
||||
fd_set rfds;
|
||||
|
||||
while(1)
|
||||
{
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 100000;
|
||||
if ((reval = select(1 + fd, &rfds, NULL, NULL, &tv)) > 0)
|
||||
{
|
||||
if(FD_ISSET(fd, &rfds))
|
||||
{
|
||||
int temp = read(fd, buff + nread, len);
|
||||
nread += temp;
|
||||
LOGD("%i us is used", 100000 - tv.tv_usec);
|
||||
LOGD("wo have data %d\n", temp);
|
||||
}
|
||||
}
|
||||
else if(reval == 0 && nread != 0) //timeout means
|
||||
{
|
||||
// int ffd;
|
||||
buff[nread]=0;
|
||||
// ffd = open("/data/ndk", O_WRONLY | O_TRUNC);
|
||||
// write(ffd, buff, nread);
|
||||
// close(ffd);
|
||||
int i, j;
|
||||
for(i = 0, j = 0; i < nread; i++)
|
||||
{
|
||||
if(buff[i] != 0)
|
||||
{
|
||||
result[j] = buff[i];
|
||||
j++;
|
||||
}
|
||||
// LOGD("0x%2x ", buff[i]);
|
||||
}
|
||||
//LOGD("\n");
|
||||
//result[j] = 0;
|
||||
jba = (*env)->NewByteArray(env, j);
|
||||
(*env)->SetByteArrayRegion(env, jba, 0, j, result);
|
||||
/* jba = (*env)->NewByteArray(env, nread);
|
||||
(*env)->SetByteArrayRegion(env, jba, 0, nread, buff);*/
|
||||
LOGD("readlength=%d\n, reallength=%d\n, all data received!\n", nread, j);
|
||||
return jba;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Create a corresponding file descriptor */
|
||||
{
|
||||
jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor");
|
||||
jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()V");
|
||||
jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I");
|
||||
mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor);
|
||||
(*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd);
|
||||
}
|
||||
|
||||
return mFileDescriptor;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: cedric_serial_SerialPort
|
||||
* Method: close
|
||||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_com_android_barcode_SerialPort_close
|
||||
(JNIEnv *env, jobject obj, jint fd)
|
||||
JNIEXPORT void JNICALL Java_android_1serialport_1api_SerialPort_close
|
||||
(JNIEnv *env, jobject thiz)
|
||||
{
|
||||
close(fd);
|
||||
jclass SerialPortClass = (*env)->GetObjectClass(env, thiz);
|
||||
jclass FileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor");
|
||||
|
||||
jfieldID mFdID = (*env)->GetFieldID(env, SerialPortClass, "mFd", "Ljava/io/FileDescriptor;");
|
||||
jfieldID descriptorID = (*env)->GetFieldID(env, FileDescriptorClass, "descriptor", "I");
|
||||
|
||||
jobject mFd = (*env)->GetObjectField(env, thiz, mFdID);
|
||||
jint descriptor = (*env)->GetIntField(env, mFd, descriptorID);
|
||||
|
||||
LOGD("close(fd = %d)", descriptor);
|
||||
close(descriptor);
|
||||
}
|
||||
|
||||
|
||||
29
app/jni/SerialPort.h
Normal file
29
app/jni/SerialPort.h
Normal file
@ -0,0 +1,29 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class android_serialport_api_SerialPort */
|
||||
|
||||
#ifndef _Included_android_serialport_api_SerialPort
|
||||
#define _Included_android_serialport_api_SerialPort
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: android_serialport_api_SerialPort
|
||||
* Method: open
|
||||
* Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
|
||||
*/
|
||||
JNIEXPORT jobject JNICALL Java_android_1serialport_1api_SerialPort_open
|
||||
(JNIEnv *, jclass, jstring, jint, jint);
|
||||
|
||||
/*
|
||||
* Class: android_serialport_api_SerialPort
|
||||
* Method: close
|
||||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_android_1serialport_1api_SerialPort_close
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
3
app/jni/gen_SerialPort_h.sh
Normal file
3
app/jni/gen_SerialPort_h.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
javah -o SerialPort.h -jni -classpath ../src android_serialport_api.SerialPort
|
||||
|
||||
Reference in New Issue
Block a user