Back to project page BluetoothSppPro.
The source code is released under:
Apache License
If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mobi.dzs.android.BLE_SPP_PRO; // w w w . ja v a2s .com import mobi.dzs.android.util.CHexConver; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.res.Configuration; import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.ScrollView; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.widget.Toast; public class actCmdLine extends BaseCommActivity{ /**???:????-??*/ private final static byte TYPE_RXD = 0x01; /**???:????-?????*/ private final static byte TYPE_TXD = 0x02; /**???:???????????-???????*/ private final static byte MEMU_SET_END_FLG = 0x21; /**???:????? ???????????*/ private final static String SUB_KEY_END_FLG = "SUB_KEY_END_FLG"; /**???:??????????????(??????)*/ private final static String SUB_KEY_MODULE_IS_USED = "SUB_KEY_MODULE_IS_USED"; /**???????????*/ private String msEndFlg = msEND_FLGS[0]; /**??:???*/ private AutoCompleteTextView mactvInput = null; /**??:???????*/ private TextView mtvDataView = null; /**??:?????????*/ private ScrollView msvCtl = null; /** * ?????? * */ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.act_cmd_line); /*????*/ this.mactvInput = (AutoCompleteTextView)this.findViewById(R.id.actCmdLine_actv_input); this.mtvDataView = (TextView)this.findViewById(R.id.actCmdLine_tv_data_view); this.msvCtl = (ScrollView)this.findViewById(R.id.actCmdLine_sv_Scroll); /*????????????*/ this.mactvInput.setOnEditorActionListener(new OnEditorActionListener(){ @Override public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2){ /*??????????????????*/ if (EditorInfo.IME_ACTION_SEND == arg1 || EditorInfo.IME_ACTION_DONE == arg1 || EditorInfo.IME_ACTION_UNSPECIFIED == arg1 ) { String sCmd = mactvInput.getText().toString().trim(); if (sCmd.length() > 0){ mactvInput.setText(""); //????? if (mBSC.Send(sCmd.concat(msEndFlg)) >= 0){ append2DataView(TYPE_TXD, sCmd); //?????? addAutoComplateVal(sCmd, mactvInput); //???????? }else{ Toast.makeText(actCmdLine.this, //???? ???? getString(R.string.msg_msg_bt_connect_lost), Toast.LENGTH_LONG).show(); mtvDataView.append(getString(R.string.msg_msg_bt_connect_lost)); mactvInput.setEnabled(false);//???????? } refreshTxdCount(); //??????????? } return true; }else return false; } }); this.initCtl(); //?????? this.loadAutoComplateCmdHistory(this.getLocalClassName(), this.mactvInput); //????????????? this.loadProfile(); //????? this.enabledBack(); //?????? this.initIO_Mode(); //??????????? this.usedDataCount(); //???????????????? //????????????????? new receiveTask() .executeOnExecutor(FULL_TASK_EXECUTOR); } /** * ??? * */ @Override public void onDestroy(){ super.onDestroy(); this.mBSC.killReceiveData_StopFlg(); //???????? this.saveAutoComplateCmdHistory(this.getLocalClassName()); //???????????????????? } /** * ??????????? * */ @Override public void onConfigurationChanged(Configuration newConfig){ super.onConfigurationChanged(newConfig); this.mactvInput.setInputType(InputType.TYPE_NULL); //???????? } /** * add top menu * */ @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); //??? MenuItem miClear = menu.add(0, MEMU_CLEAR, 0, getString(R.string.menu_clear)); miClear.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); //????? MenuItem miSetStopFlg = menu.add(0, MEMU_SET_END_FLG, 0, getString(R.string.menu_set_stop_flg)); miSetStopFlg.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //?????? MenuItem miSaveFile = menu.add(0, MEMU_SAVE_TO_FILE, 0, getString(R.string.menu_save_to_file)); miSaveFile.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //?????? //???????? MenuItem miClearHistory = menu.add(0, MEMU_CLEAR_CMD_HISTORY, 0, getString(R.string.menu_clear_cmd_history)); miClearHistory.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //?????? //?????? MenuItem miHelper = menu.add(0, MEMU_HELPER, 0, getString(R.string.menu_helper)); miHelper.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //?????? return super.onCreateOptionsMenu(menu); } /** * ???????????????? * */ @Override public boolean onMenuItemSelected(int featureId, MenuItem item){ switch(item.getItemId()){ case android.R.id.home: // app icon in action bar clicked; go home this.mBSC.killReceiveData_StopFlg(); //???????? this.mbThreadStop = true; //?????? this.setResult(Activity.RESULT_CANCELED); //???????? this.finish(); return true; case MEMU_CLEAR: //????? this.mtvDataView.setText(""); return true; case MEMU_SET_END_FLG: //????? this.selectEndFlg(); return true; case MEMU_SAVE_TO_FILE: //?????? this.saveData2File(); return true; case MEMU_CLEAR_CMD_HISTORY: //???????? this.clearAutoComplate(this.mactvInput); return true; case MEMU_HELPER: //???????? if (this.getString(R.string.language).toString().equals("zh-rCN")) this.mtvDataView.setText(this.getStringFormRawFile(R.raw.cmd_line_cn) +"\n\n"); else if (this.getString(R.string.language).toString().equals("zh-rTW")) this.mtvDataView.setText(this.getStringFormRawFile(R.raw.cmd_line_tw) +"\n\n"); else this.mtvDataView.setText(this.getStringFormRawFile(R.raw.cmd_line_en) +"\n"); return true; default: return super.onMenuItemSelected(featureId, item); } } /** * ?????????? * @param keyCode * @param event * @param data */ @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if (KeyEvent.KEYCODE_BACK == keyCode){ //????????? this.mbThreadStop = true; //?????? this.setResult(Activity.RESULT_CANCELED, null); this.finish(); return true; }else return super.onKeyDown(keyCode, event); } /** * ??????????? * @return void * */ private void initCtl(){ this.refreshRxdCount(); this.refreshTxdCount(); } /** * ???????????SD?????? * @return void * */ private void saveData2File(){ if (this.mtvDataView.length() > 0) this.save2SD(this.mtvDataView.getText().toString().trim()); } /** * ?????????????? * @param byte b ????????? TYPE_RXD:?? / TYPE_TXD:????? * @param String sData ?????????? * @return void * */ private void append2DataView(byte b, String sData){ StringBuilder sbTmp = new StringBuilder(); if (TYPE_RXD == b) sbTmp.append("Rxd>"); else sbTmp.append("Txd>"); sbTmp.append(sData); sbTmp.append("\t("); sbTmp.append(sData.length() + this.msEndFlg.length()); sbTmp.append("B)"); sbTmp.append("\n"); this.mtvDataView.append(sbTmp.toString()); } /** * ????????????? * @param byte bMode ??????? IO_MODE_HEX / IO_MODE_STRING * @return void * */ private void selectEndFlg(){ final AlertDialog adCtrl; final RadioGroup rgEndFlg; final RadioButton rb_rn, rb_n, rb_other; final EditText etVal; final AlertDialog.Builder builder = new AlertDialog.Builder(this); //?????? builder.setTitle(this.getString(R.string.dialog_title_end_flg));//???? LayoutInflater inflater = LayoutInflater.from(this); //???????? final View view = inflater.inflate(R.layout.dialog_end_flg, null); rgEndFlg = (RadioGroup)view.findViewById(R.id.rg_end_flg); rb_rn =(RadioButton)view.findViewById(R.id.rb_end_flg_set_rn); rb_n =(RadioButton)view.findViewById(R.id.rb_end_flg_set_n); rb_other =(RadioButton)view.findViewById(R.id.rb_end_flg_set_other); etVal =(EditText)view.findViewById(R.id.et_end_flg_val); builder.setView(view);//???? builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ //?????????? String sHexEndFlg = etVal.getText().toString().trim(); if (sHexEndFlg.isEmpty()){ msEndFlg = new String(); mBSC.setReceiveStopFlg(msEndFlg); //??????? mDS.setVal(getLocalClassName(), SUB_KEY_END_FLG, sHexEndFlg).saveStorage(); showEndFlg(); //????????????????? }else if (CHexConver.checkHexStr(sHexEndFlg)){ msEndFlg = CHexConver.hexStr2Str(sHexEndFlg); mBSC.setReceiveStopFlg(msEndFlg); //??????? //???????????? mDS.setVal(getLocalClassName(), SUB_KEY_END_FLG, sHexEndFlg).saveStorage(); showEndFlg(); //????????????????? }else{ Toast.makeText(actCmdLine.this, getString(R.string.msg_not_hex_string), Toast.LENGTH_SHORT).show(); } } }); adCtrl = builder.create(); adCtrl.show(); /*??????????*/ etVal.setText(CHexConver.str2HexStr(msEndFlg)); //????????? if (msEndFlg.equals(msEND_FLGS[0])) rb_rn.setChecked(true); else if (msEndFlg.equals(msEND_FLGS[1])) rb_n.setChecked(true); else rb_other.setChecked(true); /*????????????*/ rgEndFlg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup arg0, int arg1){ if (rb_rn.getId() == arg1){ msEndFlg = msEND_FLGS[0]; etVal.setEnabled(false); //??????? }else if (rb_n.getId() == arg1){ msEndFlg = msEND_FLGS[1]; etVal.setEnabled(false); //??????? }else etVal.setEnabled(true); //????? etVal.setText(CHexConver.str2HexStr(msEndFlg));//??HEX??? } }); /*??????????????*/ etVal.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable arg0){ String sEndFlg = etVal.getText().toString().trim(); if (sEndFlg.isEmpty() || CHexConver.checkHexStr(sEndFlg)){ etVal.setTextColor(android.graphics.Color.BLACK); adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); }else{ adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); etVal.setTextColor(android.graphics.Color.RED); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); } /** * ???????????? * @param String sModelName ???????? * @return void * */ private void loadProfile(){ String sHexEndFlg = this.mDS.getStringVal(this.getLocalClassName(), SUB_KEY_END_FLG); //???????????????false?????????true boolean bModuleIsUsed = this.mDS.getBooleanVal(this.getLocalClassName(), SUB_KEY_MODULE_IS_USED); if (!bModuleIsUsed){ //????????(\r\n) this.msEndFlg = msEND_FLGS[0]; this.mDS.setVal(this.getLocalClassName(), SUB_KEY_MODULE_IS_USED, true) //???????? .setVal(getLocalClassName(), SUB_KEY_END_FLG, CHexConver.str2HexStr(msEndFlg))//??????????? .saveStorage(); } else if (sHexEndFlg.isEmpty()) this.msEndFlg = ""; //???????? else this.msEndFlg = CHexConver.hexStr2Str(sHexEndFlg); this.showEndFlg(); //????????????????? this.mBSC.setReceiveStopFlg(this.msEndFlg); //??????? } /** * ????????????????? * @return void * */ private void showEndFlg(){ if(msEndFlg.equals(msEND_FLGS[0])){ this.mtvDataView.setText( String.format( this.getString(R.string.actCmdLine_msg_helper), this.getString(R.string.dialog_end_flg_rn))); }else if(msEndFlg.equals(msEND_FLGS[1])){ this.mtvDataView.setText( String.format( this.getString(R.string.actCmdLine_msg_helper), this.getString(R.string.dialog_end_flg_n))); }else{ String sTmp = null; if(msEndFlg.isEmpty()) sTmp = getString(R.string.msg_helper_endflg_nothing); else{ sTmp = String.format(getString(R.string.actCmdLine_msg_helper), "("+ CHexConver.str2HexStr(msEndFlg) +")"); } this.mtvDataView.setText(sTmp); } } /** * ?????????? * @return void * */ private void autoScroll(){ //????????? int iOffset = this.mtvDataView.getMeasuredHeight() - this.msvCtl.getHeight(); if (iOffset > 0) this.msvCtl.scrollTo(0, iOffset); } //---------------- /*???????(????????)*/ private class receiveTask extends AsyncTask<String, String, Integer>{ /**???:????*/ private final static byte CONNECT_LOST = 0x01; /**???:????????*/ private final static byte THREAD_END = 0x02; /** * ????????????? */ @Override public void onPreExecute(){ mtvDataView.append(getString(R.string.msg_receive_data_wating)); mtvDataView.append("\n"); mbThreadStop = false; } /** * ???????? */ @Override protected Integer doInBackground(String... arg0){ mBSC.Receive(); //??????????????????? while(!mbThreadStop){ if (!mBSC.isConnect()) return (int)CONNECT_LOST; //?????????? this.publishProgress(mBSC.ReceiveStopFlg()); } return (int)THREAD_END; } /** * ????????? */ @Override public void onProgressUpdate(String... progress){ if (null != progress[0]){ //???????????? append2DataView(TYPE_RXD, progress[0]); //?????????? autoScroll(); //??????????? refreshRxdCount(); //??????????? } } /** * ????????????????? */ @Override public void onPostExecute(Integer result){ if (CONNECT_LOST == result) //?????? mtvDataView.append(getString(R.string.msg_msg_bt_connect_lost)); else mtvDataView.append(getString(R.string.msg_receive_data_stop));//???????? mactvInput.setEnabled(false); refreshHoldTime(); //??????????????-????? } } }