4. JAVA層封裝本地方法 package com.notioni.uart.manager;
import com.notioni.uart.manager.TtyNativeControl.ReceiveCallback; /* * 單例模式 */ public class UARTCTLManager {
private TtyNativeControl mTtyControl; private boolean mIsOpen = false; private static UARTCTLManager mManager; private UARTCTLManager(){ mTtyControl = new TtyNativeControl(); } public static UARTCTLManager getInstance(){ if(mManager == null){ mManager = new UARTCTLManager(); } return mManager; } /* * 打開驅動 */ public int openDevice(){ int o = mTtyControl.openTty(); o=1; if(o == 1){ mIsOpen = true; } return o; } /* * 關閉驅動 */ public int closeDevice(){ int c = mTtyControl.closeTty(); if(c == 1){ mIsOpen = false; } return c; } /* * 判斷驅動是否打開 */ public boolean isOpenDevice(){ return mIsOpen; } /* * 發(fā)送數(shù)據(jù) */ public int sendDataToDevice(byte[] data){ return mTtyControl.sendMsgToTty(data); } /* * 注入接收數(shù)據(jù)回調方法 */ public void receiveDataFromDevice(ReceiveCallback callback){ mTtyControl.receiveMsgFromTty(callback); } /** * 設置串口數(shù)據(jù)位,,校驗位,速率,,停止位 * @param databits 數(shù)據(jù)位 取值 位7或8 * @param event 校驗類型 取值N ,E, O,,S * @param speed 速率 取值 2400,4800,9600,115200 * @param stopBit 停止位 取值1 或者 2 */ public int configDevice(int databits,char event,int speed,int stopBit){ if(!mIsOpen){ return -1; } int re = mTtyControl.configTty(databits, event, speed, stopBit); return re; }
/** * 設置串口通信模式,打印串口信息 * @param mode 是否使用原始模式(Raw Mode)方式來通訊 取值0,1,2 說明:0=nothing,1=Raw mode,2=no raw mode * @param showLog 打印出串口信息Log 取值1,0 * @return */ public int setMode(int mode ,int showLog){ return mTtyControl.setMode(mode, showLog); }
/** * * 接收數(shù)據(jù)回調接口,,接收驅動送到的數(shù)據(jù)要實現(xiàn)這個回調接口 */ public interface ReceiveDataCallBack extends ReceiveCallback{
} }
5. JAVA調用 Activity package com.notioni.uart;
import com.notioni.uart.manager.TtyNativeControl; import com.notioni.uart.manager.UARTCTLManager;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;
public class UARTCTLActivity extends Activity implements View.OnClickListener{ private static final String TAG = "UARTCTLActivity"; private UARTCTLManager mUartctlManager; private Button mOpenOrCloseBtn; private Button mSendBtn; private EditText mSendText; private TextView mReceiveText;
private ReceiveData mReceiveData; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
mUartctlManager = UARTCTLManager.getInstance(); mReceiveData = new ReceiveData();
mOpenOrCloseBtn = (Button)findViewById(R.id.openOrcloseBtn); mSendBtn = (Button)findViewById(R.id.sendBtn); mSendText = (EditText)findViewById(R.id.sendMsg); mReceiveText = (TextView)findViewById(R.id.receiveMsg);
mOpenOrCloseBtn.setOnClickListener(this); mSendBtn.setOnClickListener(this); }
@Override protected void onResume() { super.onResume(); }
@Override public void onClick(View v) { switch(v.getId()){ case R.id.openOrcloseBtn://打開或關閉驅動 Log.i(TAG, "isOpen:"+mUartctlManager.isOpenDevice()); if(mUartctlManager.isOpenDevice()){ mUartctlManager.closeDevice(); mOpenOrCloseBtn.setText(R.string.open); }else{ int i =mUartctlManager.openDevice(); Log.i(TAG, "openDevice result:"+i); mOpenOrCloseBtn.setText(R.string.close); mUartctlManager.setMode(1, 1);//查看串口配置信息 } break; case R.id.sendBtn: openDevice(); int re = mUartctlManager.sendDataToDevice(getSendText()); Log.i(TAG, "send result:"+re); break; } } /* * 打開驅動 */ private void openDevice(){ if(!mUartctlManager.isOpenDevice()){ mUartctlManager.openDevice(); mUartctlManager.receiveDataFromDevice(mReceiveData); } } /* * 關閉驅動 */ public void closeDevice(){ if(mUartctlManager.isOpenDevice()){ mUartctlManager.closeDevice(); } }
/* * 取出待發(fā)送的數(shù)據(jù) */ private byte[] getSendText(){ String st = mSendText.getText().toString()+"\r"; if(st == null || "".equals(st)){ return null; } return st.getBytes(); }
/* * 接收數(shù)據(jù) */ class ReceiveData implements UARTCTLManager.ReceiveDataCallBack{ @Override public void onReceiveData(byte[] data, TtyNativeControl tty) { if(mReceiveText != null && data != null){ Log.w(TAG, "[onReceiveData] data:"+data.toString()); mReceiveText.setText(data.toString()); } }
}
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(1,1,0,R.string.config); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); if(item.getItemId() == 1){ Intent intent = new Intent(); intent.setClass(this, ConfigActivity.class); startActivity(intent); } return true; } @Override protected void onPause() { super.onPause(); // closeDevice(); } @Override protected void onDestroy() { closeDevice(); super.onDestroy(); } } 6. JAVA配置串口屬性 package com.notioni.uart;
import com.notioni.uart.manager.UARTCTLManager;
import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.util.Log; import android.widget.Toast;
public class ConfigActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener{ private static final String TAG = "ConfigActivity";
private static final String KEY_DATABITS = "tty_databits"; private static final String KEY_EVENT = "tty_event"; private static final String KEY_SPEED = "tty_speed"; private static final String KEY_STOPBITS = "tty_stopbits";
ListPreference dataBitsPrefer ; ListPreference eventPrefer ; ListPreference speedPrefer ; ListPreference stopBitsPrefer ;
Object dataBitsValues = null; Object eventValues = null; Object speedValues = null; Object stopbitsValues = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.config_options); PreferenceScreen prefSet = getPreferenceScreen(); dataBitsPrefer = (ListPreference)prefSet.findPreference(KEY_DATABITS); eventPrefer = (ListPreference)prefSet.findPreference(KEY_EVENT); speedPrefer = (ListPreference)prefSet.findPreference(KEY_SPEED); stopBitsPrefer = (ListPreference)prefSet.findPreference(KEY_STOPBITS);
dataBitsPrefer.setValueIndex(0); eventPrefer.setValueIndex(0); speedPrefer.setValueIndex(0); stopBitsPrefer.setValueIndex(0);
dataBitsPrefer.setOnPreferenceChangeListener(this); eventPrefer.setOnPreferenceChangeListener(this); speedPrefer.setOnPreferenceChangeListener(this); stopBitsPrefer.setOnPreferenceChangeListener(this); }
@Override public boolean onPreferenceChange(Preference preference, Object newValue) {
if(preference == dataBitsPrefer){ dataBitsValues = newValue; }else if(preference == eventPrefer){ eventValues = newValue; }else if(preference == speedPrefer){ speedValues = newValue; }else if(preference == stopBitsPrefer){ stopbitsValues = newValue; } Log.i(TAG, "newValue:"+newValue); return false; } @Override protected void onPause() { super.onPause();
UARTCTLManager mUartctlManager = UARTCTLManager.getInstance(); int databits=7,speed=9600,stopbits=1;//設置默認值 char event='N';//設置默認值 if(dataBitsValues != null){ databits = Integer.parseInt(dataBitsValues.toString()); } if(eventValues != null){ event = eventValues.toString().charAt(0); } if(speedValues != null){ speed = Integer.parseInt(speedValues.toString()); } if(stopbitsValues != null){ stopbits = Integer.parseInt(stopbitsValues.toString()); } if(!mUartctlManager.isOpenDevice()){ mUartctlManager.openDevice(); } Log.e(TAG, "databit="+databits+",event="+event+",speed="+speed+",stopbits="+stopbits); if(databits == -1 || speed == -1 || stopbits == -1 || event=='Q'){ Toast.makeText(this, "有參數(shù)沒有設置,不去配置串口!", Toast.LENGTH_LONG).show(); return; } mUartctlManager.configDevice(databits, event, speed, stopbits);
mUartctlManager.setMode(0, 1);//查看串口配置信息
// Log.e(TAG, "databit="+databits+",event="+event+",speed="+speed+",stopbits="+stopbits); } }
未完下接 Android通過JNI操作串口《四》 |
|
來自: 昵稱13517980 > 《Android_JNI_串口》