UART_JNI

package com.src.Uart; import android.app.Activity;import android.content.Intent;import android.R.drawable;import android.view.View;import android.view.Window;import android.view.Gravity; import android.view.View.OnClickListener; import android.widget.Button;import android.widget.TextView;import android.widget.EditText;import java.lang.String;import java.lang.Process;import java.lang.Thread;import android.util.Log;import java.util.Timer;import java.util.TimerTask;import java.util.Calendar;import java.util.Date;import java.io.FileDescriptor;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileInputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import android.os.ServiceManager;import android.os.RemoteException;import android.os.Handler;import android.os.Message;import android.os.Bundle;import android.os.SystemClock;import com.mediatek.audioprofile.AudioProfileManager;import android.media.AudioFormat;import android.media.AudioManager;import android.media.AudioRecord;import android.media.AudioTrack;import android.media.MediaRecorder;import android.app.Service; public class Uartjni extends Activity {final private static String TAG = "UartjniAPP"; private FileDescriptor mFd_Mcu;private FileInputStream mFileInputStream_Mcu;private FileOutputStream mFileOutputStream_Mcu;private ReadThread mReadMcuThread; final private int UartMcu = 0;StringBuffer McuCmdBuf = new StringBuffer();Timer timer = new Timer(); private Thread playmic;AudioRecord audioRecord;AudioTrack audioTrack;AudioManager mAudioManager;static{ Log.i(TAG, "======================= kandy UartTest loadLibrary ====================================="); System.loadLibrary( "Uartjni" ); }@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mAudioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE); Button BtnRobotFoward = (Button) this.findViewById(R.id.BtnRobotFoward); Button BtnRobotBack = (Button) this.findViewById(R.id.BtnRobotBack); Button BtnRobotLeft = (Button) this.findViewById(R.id.BtnRobotLeft); Button BtnRobotRight = (Button) this.findViewById(R.id.BtnRobotRight); Button BtnRobotUpHand = (Button) this.findViewById(R.id.BtnRobotUpHand); Button BtnRobotResetHand = (Button) this.findViewById(R.id.BtnRobotResetHand); Button BtnRobotUpHead = (Button) this.findViewById(R.id.BtnRobotUpHead); Button BtnRobotDownHead = (Button) this.findViewById(R.id.BtnRobotDownHead); Button BtnRobotHeadLeft = (Button) this.findViewById(R.id.BtnRobotHeadLeft); Button BtnRobotHeadRight = (Button) this.findViewById(R.id.BtnRobotHeadRight); Button BtnStop = (Button) this.findViewById(R.id.BtnStop); Button btnExit = (Button) this.findViewById(R.id.Exit); Button BtnCharge = (Button) this.findViewById(R.id.BtnCharge); Button BtnVolAdd = (Button) this.findViewById(R.id.BtnVolAdd); Button BtnVolDec = (Button) this.findViewById(R.id.BtnVolDec); Button BtnPoweroff = (Button) this.findViewById(R.id.BtnPoweroff); Button BtnAudioPlay = (Button) this.findViewById(R.id.BtnAudioPlay ); Log.i(TAG, "========= will open the uart ==========="); if( true == OpenUartMcuPort() ) { //open uart port for Mcu Log.i(TAG, "========= uart ready !==========="); } else { Log.i(TAG, "========= uart is not ready !==========="); } try { mReadMcuThread = new ReadThread(); mReadMcuThread.start(); } catch (Exception e) { e.printStackTrace(); } setTimerTask(); // playmic = new Thread(new playRecord()); //kandy // playmic.start(); } class playRecord implements Runnable{ static final int frequency = 44100; static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO; static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; @Overridepublic void run(){ int recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding); int plyBufSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding); audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, recBufSize); audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfiguration, audioEncoding, plyBufSize, AudioTrack.MODE_STREAM); byte[] recBuf = new byte[recBufSize]; audioRecord.startRecording(); /* audioTrack.play(); while(true){ int readLen = audioRecord.read(recBuf, 0, recBufSize); audioTrack.write(recBuf, 0, readLen); // break; } */ // audioTrack.stop(); // audioRecord.stop();}} private boolean IsSameCmd(String str) { if( McuCmdBuf.indexOf(str)!=-1 ) { return true; } return false; } private class ReadThread extends Thread { @Override public void run() { while(true) { try { if (mFileInputStream_Mcu == null) return; byte[] b=new byte[mFileInputStream_Mcu.available()]; int size = mFileInputStream_Mcu.read(b);//Log.i(TAG, "Recv size = "+ String.valueOf(size) ); String UartData=new String(b); if( size > 0) { McuCmdBuf.delete (0,McuCmdBuf.length()); McuCmdBuf.append( UartData ); Log.i(TAG, "===>>McuData:"+ McuCmdBuf.toString() ); if( IsSameCmd("MotorMoveLeft") ){Log.i(TAG, "===>>Get cmd: MotorMoveLeft");}if( IsSameCmd("McuCmd_AndroidPowerOff") ) //Received power off cmd from MCU{Log.i(TAG, "===>>android is ready to power off\n"); Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, true); shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(shutdown);} } Thread.sleep(10); } catch (Exception e) { e.printStackTrace(); } } } } public boolean OpenUartMcuPort(){ mFd_Mcu = UartOpen("/dev/ttyMT1",115200); if(mFd_Mcu == null){ Log.i(TAG, "=========open uart ==>> fail to open uart port ============"); return false; } else{ Log.i(TAG, "=========open uart ==>> open uart port successfully !============"); } mFileInputStream_Mcu = new FileInputStream(mFd_Mcu); mFileOutputStream_Mcu = new FileOutputStream(mFd_Mcu); return true;} private String log_uarttype(int uarttype) { if(uarttype == UartMcu) return "UART_MCU"; else return "Others"; } private void WriteUartByte(int UartType , byte data) {try { if( UartType == UartMcu) { // Log.i(TAG, "=========WriteUartByte: UartType = " + log_uarttype(UartType) ); mFileOutputStream_Mcu.write(data); mFileOutputStream_Mcu.flush(); }} catch (IOException e) {Log.e(TAG, e.getMessage());}} private void WriteUartByteArray(int UartType , byte[] data) { try { if( UartType == UartMcu) { // Log.i(TAG, "=========WriteUartByte: UartType = " + log_uarttype(UartType) ); mFileOutputStream_Mcu.write(data); mFileOutputStream_Mcu.flush(); }} catch (IOException e) {Log.e(TAG, e.getMessage());}} private void SendByteCmdToMcu(byte[] data) { WriteUartByteArray(UartMcu , data); } public void WriteUartString(int UartType , String UartData) {//Log.i(TAG, "========= WriteUartString: " + UartData ); byte[] ByteData = UartData.getBytes(); try { if( UartType == UartMcu) { // Log.i(TAG, "========= UartType == UartMcu"); mFileOutputStream_Mcu.write(ByteData); // mFileOutputStream_Mcu.flush(); }} catch (IOException e) {Log.e(TAG, e.getMessage());} }private void SendCmdToMcu(String str) { WriteUartString(UartMcu,str); Log.i(TAG, "========= SendCmdToMcu: " + str );}private byte[] StrToByte(String[] UartData) {byte[] ByteData = new byte[UartData.length];for (int i = 0; i < UartData.length; i++) {ByteData[i] = (byte) Integer.parseInt(UartData[i], 16);}return ByteData;}public void UartReceiveString_Mcu() { try { if (mFileInputStream_Mcu == null) return; byte[] b=new byte[mFileInputStream_Mcu.available()]; int size = mFileInputStream_Mcu.read(b); String UartData=new String(b); if(size >0) { Log.i(TAG, "===>>Received data: "+ UartData ); } //System.out.println(UartData); } catch (IOException e) { Log.e(TAG, e.getMessage()); }}private void setTimerTask() { timer.schedule(new TimerTask() { @Override public void run() { WriteUartString(UartMcu,"Android_HeartTick\n"); //Send heart tick package to MCU Log.i(TAG, "===>> Android_HeartTick" ); } }, 0, 10000); } public void BtnHandler(View v){ switch(v.getId()){ case R.id.BtnRobotFoward: // SendCmdToMcu("RobotMotorCmd_WalkForward\n"); SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x01,0x05,0x0D,0x0A} ); break; case R.id.BtnRobotBack: // SendCmdToMcu("RobotMotorCmd_WalkBack\n"); SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x02,0x06,0x0D,0x0A} ); break; case R.id.BtnRobotLeft: // SendCmdToMcu("RobotMotorCmd_WalkLeft\n");SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x03,0x07,0x0D,0x0A} ); break; case R.id.BtnRobotRight: // SendCmdToMcu("RobotMotorCmd_WalkRight\n");SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x04,0x08,0x0D,0x0A} ); break; case R.id.BtnRobotUpHand: // SendCmdToMcu("RobotMotorCmd_HandUp\n");SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x0B,0x0F,0x0D,0x0A} ); break; case R.id.BtnRobotResetHand: //SendCmdToMcu("RobotMotorCmd_HandReset\n");SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x0C,0x10,0x0D,0x0A} ); break; case R.id.BtnRobotUpHead: //SendCmdToMcu("RobotMotorCmd_HeadUp\n");SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x0D,0x11,0x0D,0x0A} ); break; case R.id.BtnRobotDownHead: // SendCmdToMcu("RobotMotorCmd_HeadDown\n");SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x0F,0x13,0x0D,0x0A} ); break; case R.id.BtnRobotHeadLeft: // SendCmdToMcu("RobotMotorCmd_HeadLeft\n"); SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x10,0x14,0x0D,0x0A} ); break; case R.id.BtnRobotHeadRight: // SendCmdToMcu("RobotMotorCmd_HeadRight\n"); SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x11,0x15,0x0D,0x0A} ); break; case R.id.BtnCharge: SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x13,0x17,0x0D,0x0A} ); break; case R.id.BtnVolAdd: mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,AudioManager.FX_FOCUS_NAVIGATION_UP); break; case R.id.BtnVolDec: mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FX_FOCUS_NAVIGATION_UP); break; case R.id.BtnStop: // SendCmdToMcu("RobotMotorCmd_MotorStop\n"); SendByteCmdToMcu( new byte[]{0x5A,0x50,0x04,0x02,0x01,0x01,0x12,0x16,0x0D,0x0A} ); break; case R.id.BtnAudioPlay: Log.i(TAG, "====== start Mic play ======");SelectMic2(); break; case R.id.BtnPoweroff: Log.i(TAG, "====== the device will power off ======"); Intent shutdown = new Intent(Intent.ACTION_REQUEST_SHUTDOWN); shutdown.putExtra(Intent.EXTRA_KEY_CONFIRM, true); shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(shutdown); break; } }public void UartStopHandler(View v){ switch(v.getId()){ case R.id.BtnRobotBack: UartStop_handler(); break; } }public void UartStart_handler(){ byte[] cmd2 = new byte[]{0x1C,0x1D,0x1D,0x1E,1,0x1F}; // WriteUartByteArray(UartMcu,cmd2); WriteUartString(UartMcu,"====Hello,Mcu!\n"); }public void UartStop_handler(){}public void btnExtHandler(View v){ UartClose(); // audioTrack.stop(); // audioRecord.stop(); this.finish();}@Override protected void onDestroy() { super.onDestroy(); timer.cancel(); // audioTrack.stop(); // audioRecord.stop();UartClose();} public static native FileDescriptor UartOpen(String path,int baudrate);public static native void UartClose();public static native void SelectMic2();public static native void SysSleep();}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dxmcu

谢谢鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值