Android Open Source - BluetoothSppPro act Key Board






From Project

Back to project page BluetoothSppPro.

License

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.

Java Source Code

package mobi.dzs.android.BLE_SPP_PRO;
/* w w w . j  a v a 2 s.co m*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import mobi.dzs.android.bluetooth.BluetoothSppClient;
import mobi.dzs.android.control.button.ButtonPassListener;
import mobi.dzs.android.control.button.RepeatingButton;
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.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class actKeyBoard extends BaseCommActivity{
  /**???????????*/
  private String msEndFlg = msEND_FLGS[0];
  /**???:???????????-???????*/
  private final static byte MEMU_SET_END_FLG = 0x21;
  /**???:???????????-????*/
  private final static byte MENU_SET_KEY_BOARD = 0x22;
  /**???:???????????-?????????????*/
  private final static byte MENU_SET_LONG_PASS_REPEAT = 0x24;
  /**???:????? ???????????*/
  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 final static String SUB_KEY_BTN_NAME = "SUB_KEY_BTN_NAME";
  /**???:?????-??????*/
  private final static String SUB_KEY_BTN_DOWN_VAL = "SUB_KEY_BTN_VAL";
  /**???:?????-??????*/
  private final static String SUB_KEY_BTN_HOLD_VAL = "SUB_KEY_BTN_HOLD_VAL";
  /**???:?????-??????*/
  private final static String SUB_KEY_BTN_UP_VAL = "SUB_KEY_BTN_UP_VAL";
  /**???:?????-????????????*/
  private final static String SUB_KEY_BTN_REPEAT_FREQ = "SUB_KEY_BTN_REPEAT_FREQ";
  /**???:????????????(ms)*/
  private final static int BTN_REPEAT_MIN_FREQ = 50;
  /** ?????????????? DOWN???HOLD????????UP?? */
  public enum TIRGGER_TYPE{DOWN, HOLD, UP};
  
  /**???????????*/
  private TextView mtvSendView = null;
  /**????????*/
  private TextView mtvRecView = null;
  /**??????????*/
  private TextView mtvRecAreaTitle = null;
  /**??????????*/
  private ScrollView msvSendView = null;
  /**???????*/
  private ScrollView msvRecView = null;
  /**???????*/
  private RelativeLayout mrlSendArea= null;
  
  /**???????*/
  private final static int miBTN_CNT = 12;
  /**?????????*/
  private RepeatingButton[] mbtns =  new RepeatingButton[miBTN_CNT];
  /** ???????????? (HashMap('down'='', 'hold'='', 'up'=''))*/
  private final List<HashMap<String, String>> mlBtnSendVal = new ArrayList<HashMap<String, String>>();
  
  /**????????????????*/
  private boolean mbSetMode = false;  
  /**??????????????*/
  private int miRepateFreq = 500;
  /**?????????????????*/
  private boolean mbHideSendArea = false;
  /**?????????*/
  private class CRL implements ButtonPassListener{
    @Override
    public void onRepeat(View v, long duration, int repeatcount){ //??
      if(mbSetMode)
        return;//??????????????????
      else
        onBtnClick_Array(v, TIRGGER_TYPE.HOLD);
    }
    @Override
    public void onDown(View v){ //??
      onBtnClick_Array(v, TIRGGER_TYPE.DOWN);
    }
    @Override
    public void onUp(View v){ //??
      if(mbSetMode)
        return;//??????????????????
      else
        onBtnClick_Array(v, TIRGGER_TYPE.UP);
    }
  };  
  public CRL mCRL = new CRL();
  
  /**
   * ??????
   * */
  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_keyboard);
    
    this.mtvSendView = (TextView)this.findViewById(R.id.actKeyBoard_tv_send_data_show);
    this.mtvRecView = (TextView)this.findViewById(R.id.actKeyBoard_tv_receive_show);
    this.mtvRecAreaTitle = (TextView)this.findViewById(R.id.actKeyBoard_tv_receive_area_title);
    this.msvSendView = (ScrollView)this.findViewById(R.id.actKeyBoard_sv_send_data_scroll);
    this.msvRecView = (ScrollView)this.findViewById(R.id.actKeyBoard_sv_receive_data_scroll);
    this.mrlSendArea = (RelativeLayout)this.findViewById(R.id.actKeyBoard_rl_send_area);
    this.mtvRecView.setText(""); //?????????
    this.mtvRecAreaTitle.append("\t\t(");//???????
    this.mtvRecAreaTitle.append(getString(R.string.tips_click_to_hide));//???????
    this.mtvRecAreaTitle.append(":"+ getString(R.string.tv_send_area_title));//???????
    this.mtvRecAreaTitle.append(")");//??????????
    this.mtvRecAreaTitle.setOnClickListener(new TextView.OnClickListener(){ //??????????????????
      @Override
      public void onClick(View v){
        if (v.getId() == R.id.actKeyBoard_tv_receive_area_title){
          String sTitle = getString(R.string.tv_receive_area_title);
          TextView tv = ((TextView)v);
          if (mbHideSendArea){
            sTitle += "\t\t("+ getString(R.string.tips_click_to_hide);
            sTitle += ":"+ getString(R.string.tv_send_area_title) +")";
            tv.setText(sTitle);
            mrlSendArea.setVisibility(View.VISIBLE);
          }else{
            sTitle += "\t\t("+ getString(R.string.tips_click_to_show);
            sTitle += ":"+ getString(R.string.tv_send_area_title) +")";
            tv.setText(sTitle);
            mrlSendArea.setVisibility(View.GONE);
          }
          mrlSendArea.refreshDrawableState(); //????????
          mbHideSendArea = !mbHideSendArea;
        }
      }
    });
    
    /*?????????????????*/
    String sTitle = getString(R.string.tv_receive_area_title);
    sTitle += "\t\t("+ getString(R.string.tips_click_to_show);
    sTitle += ":"+ getString(R.string.tv_send_area_title) +")";
    this.mtvRecAreaTitle.setText(sTitle);
    this.mrlSendArea.setVisibility(View.GONE);
    this.mrlSendArea.refreshDrawableState(); //????????
    this.mbHideSendArea = true; //??????????
    
      //??????
      mbtns[0] = (RepeatingButton)findViewById(R.id.btn_keyboard_1);//????RepeatingButton??
      mbtns[1] = (RepeatingButton)findViewById(R.id.btn_keyboard_2);//????RepeatingButton??
      mbtns[2] = (RepeatingButton)findViewById(R.id.btn_keyboard_3);//????RepeatingButton??
      mbtns[3] = (RepeatingButton)findViewById(R.id.btn_keyboard_4);//????RepeatingButton??
      mbtns[4] = (RepeatingButton)findViewById(R.id.btn_keyboard_5);//????RepeatingButton??
      mbtns[5] = (RepeatingButton)findViewById(R.id.btn_keyboard_6);//????RepeatingButton??
      mbtns[6] = (RepeatingButton)findViewById(R.id.btn_keyboard_7);//????RepeatingButton??
      mbtns[7] = (RepeatingButton)findViewById(R.id.btn_keyboard_8);//????RepeatingButton??
      mbtns[8] = (RepeatingButton)findViewById(R.id.btn_keyboard_9);//????RepeatingButton??
      mbtns[9] = (RepeatingButton)findViewById(R.id.btn_keyboard_10);//????RepeatingButton??
      mbtns[10] = (RepeatingButton)findViewById(R.id.btn_keyboard_11);//????RepeatingButton??
      mbtns[11] = (RepeatingButton)findViewById(R.id.btn_keyboard_12);//????RepeatingButton??
      this.loadBtnProfile(); //???????????
      
    this.enabledBack(); //??????
    this.initIO_Mode(); //???????????
    this.usedDataCount(); //????????????????
    this.loadProfile(); //?????
    
    //?????????????????
    new receiveTask()
      .executeOnExecutor(FULL_TASK_EXECUTOR);
  }
  
    /**??????????*/
    @Override
    public void onResume(){
      super.onResume();
      Display display = this.getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      
      int iHeight = (size.x / 3) * 2 / 3; //????;
      
      for(int i=0; i<miBTN_CNT; i++){
      LayoutParams btnPara = mbtns[i].getLayoutParams();
      btnPara.height = iHeight;//?????????
      mbtns[i].setLayoutParams(btnPara);
      }
    }
    
  /**
   * ???
   * */
    @Override
    public void onDestroy(){
      super.onDestroy();
      this.mBSC.killReceiveData_StopFlg(); //????????
    }
    
  /**
   * ???????????
   * */
  @Override
  public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);
  }
  
  /**
   * 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 miSetKeyboard = menu.add(1, MENU_SET_KEY_BOARD, 0, getString(R.string.menu_set_key_board_start));
        miSetKeyboard.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        //?????????
        MenuItem miSetStopFlg = menu.add(1, MEMU_SET_END_FLG, 1, getString(R.string.menu_set_stop_flg));
        miSetStopFlg.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        //?????????????
        MenuItem miSetBtnLongPassFreq = menu.add(1, MENU_SET_LONG_PASS_REPEAT,3, getString(R.string.menu_set_button_long_pass_freq));
        miSetBtnLongPassFreq.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        //??IO???
        MenuItem miIoMode = menu.add(0, MEMU_IO_MODE, 4, getString(R.string.menu_io_mode));
        miIoMode.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //??????
        //??????
        MenuItem miSaveFile = menu.add(1, MEMU_SAVE_TO_FILE, 5, getString(R.string.menu_save_to_file));
        miSaveFile.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); //??????
        //??????
        MenuItem miHelper = menu.add(1, MEMU_HELPER, 6, 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.mtvSendView.setText("");
            this.mtvRecView.setText("");
            return true;
          case MENU_SET_KEY_BOARD: //????
            if (this.mbSetMode){  
              item.setTitle(R.string.menu_set_key_board_start);
              this.mtvSendView.setText(R.string.actKeyBoard_tv_Init);
            }else{  
              item.setTitle(R.string.menu_set_key_board_end);
              this.mtvSendView.setText(R.string.actKeyBoard_tv_set_keyboard_helper);
            }
            this.mbSetMode = !this.mbSetMode;//????????????
            return true;
          case MENU_SET_LONG_PASS_REPEAT: //????????????
            this.selectRepeatFreq();
            return true;
          case MEMU_SET_END_FLG: //?????
            this.selectEndFlg();
            return true;
          case MEMU_IO_MODE: //??IO???
            this.setIOModeDialog();
            return true;
          case MEMU_SAVE_TO_FILE: //??????
            this.saveData2File();
            return true;
          case MEMU_HELPER: //????????
            if (this.getString(R.string.language).toString().equals("zh-rCN"))
              this.mtvRecView.setText(this.getStringFormRawFile(R.raw.key_board_cn) +"\n\n");
            else if (this.getString(R.string.language).toString().equals("zh-rTW"))
              this.mtvRecView.setText(this.getStringFormRawFile(R.raw.key_board_tw) +"\n\n");
            else
              this.mtvRecView.setText(this.getStringFormRawFile(R.raw.key_board_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);
    }
    
    /**
     * ?????????????
     * @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(actKeyBoard.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) {
      }
      });
    }
    
    /**
     * ???????????SD??????
     * @return void
     * */
    private void saveData2File(){
      this.save2SD(this.mtvRecView.getText().toString().trim());
    }
  
  /**
   * ???????????
   * */
  private void loadBtnProfile(){
    String sTmp;
    final String sModel = this.getLocalClassName();
      //?????????
      for(int i=0; i<miBTN_CNT; i++){
        String sName = this.mDS.getStringVal(sModel, SUB_KEY_BTN_NAME.concat(String.valueOf(i)));
        String sDown = this.mDS.getStringVal(sModel, SUB_KEY_BTN_DOWN_VAL.concat(String.valueOf(i)));
        String sHold = this.mDS.getStringVal(sModel, SUB_KEY_BTN_HOLD_VAL.concat(String.valueOf(i)));
        String sUp = this.mDS.getStringVal(sModel, SUB_KEY_BTN_UP_VAL.concat(String.valueOf(i)));
        
        if (!sName.isEmpty())//????????
          mbtns[i].setText(sName);
        //????3?????
        HashMap<String, String> mhBtnSend = new HashMap<String, String>();
        mhBtnSend.put("DOWN", (sDown.isEmpty())?"":sDown);
        mhBtnSend.put("HOLD", (sHold.isEmpty())?"":sHold);
        mhBtnSend.put("UP", (sUp.isEmpty())?"":sUp);
        this.mlBtnSendVal.add(mhBtnSend);
      }
      
      for (int i=0; i<miBTN_CNT; i++)//?????????????
      this.mbtns[i].bindListener(mCRL, 500);
    
    //???????????????
    int iRepeat = this.mDS.getIntVal(this.getLocalClassName(), SUB_KEY_BTN_REPEAT_FREQ);
    if (0 == iRepeat) //????
      this.setBtnRepeatFreq(500);
    else
      this.setBtnRepeatFreq(iRepeat);
    
    sTmp = String.format(getString(R.string.actKeyBoard_msg_repeat_freq_set)+"\n", iRepeat);
    this.mtvRecView.append(sTmp);//??????????????
  }
    
    /**
     * ?????????????
     * @param long interval ??
     * @return void
     * */
    private void setBtnRepeatFreq(int interval){
      this.miRepateFreq = interval;
    for (int i=0; i<miBTN_CNT; i++)
      this.mbtns[i].setRepeatFreq(interval);
    this.mDS.setVal(this.getLocalClassName(), SUB_KEY_BTN_REPEAT_FREQ, interval).saveStorage();
    }
  
    /**
     * ????????????
     * @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(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.mtvRecView.append(
            String.format(
                this.getString(R.string.actKeyBoard_msg_helper_endflg), 
                this.getString(R.string.dialog_end_flg_rn)));
      }else if(msEndFlg.equals(msEND_FLGS[1])){
        this.mtvRecView.append(
            String.format(
                this.getString(R.string.actKeyBoard_msg_helper_endflg), 
                this.getString(R.string.dialog_end_flg_n)));
      }else{
        if (this.msEndFlg.isEmpty())
          this.mtvRecView.append(getString(R.string.msg_helper_endflg_nothing));
        else{
          this.mtvRecView.append(
            String.format(
                getString(R.string.actKeyBoard_msg_helper_endflg),
                "("+ CHexConver.str2HexStr(msEndFlg) +")"
                )
              );
        }
      }
    }
    
    /**
     * ???????????????
     * @param View v ?????????????
     * @return void
     * */
    public void onBtnClick_Array(View v, TIRGGER_TYPE tt){
      int iBtnId = v.getId();
      for(int i=0; i<miBTN_CNT; i++){
        if (mbtns[i].getId() == iBtnId){ //???????????
          if (mbSetMode) //?????
            this.setBtnKeyboard(i);
          else{
            if (TIRGGER_TYPE.DOWN == tt && !this.mlBtnSendVal.get(i).get("DOWN").isEmpty()){
              this.Send(this.mlBtnSendVal.get(i).get("DOWN"));//??????????
            }else if (TIRGGER_TYPE.HOLD == tt && !this.mlBtnSendVal.get(i).get("HOLD").isEmpty()){
              this.Send(this.mlBtnSendVal.get(i).get("HOLD"));//??????????
            }else if (TIRGGER_TYPE.UP == tt && !this.mlBtnSendVal.get(i).get("UP").isEmpty()){
              this.Send(this.mlBtnSendVal.get(i).get("UP"));//??????????
            }
          }
          break;
        }
      }
    }
    
    /**
     * ??????????????
     * @return void
     * */
    private void Send(String sData){
    if (!sData.equals("")){
      String sSend = sData;
      int iRet = 0;
      if (!this.msEndFlg.isEmpty()) //????????????
        iRet = this.mBSC.Send(sSend.concat(this.msEndFlg));
      else
        iRet = this.mBSC.Send(sSend);
      
      if (iRet >= 0){ //???????
        if (View.VISIBLE == this.mrlSendArea.getVisibility()){ //??????????????TextView
          if (iRet == 0)
            this.mtvSendView.append(sSend.concat("(fail) "));
          else
            this.mtvSendView.append(sSend.concat("(succeed) "));
        }
      }else{  //????
        Toast.makeText(actKeyBoard.this, //???? ????
             getString(R.string.msg_msg_bt_connect_lost),
             Toast.LENGTH_LONG).show();
        this.mtvRecView.append(this.getString(R.string.msg_msg_bt_connect_lost) + "\n");
      }
      this.refreshTxdCount();//????????
      this.autoScroll(); //???????
    }
    }
    
    /**
     * ????????????
     * @param int iId ???ID?????
     * @return void
     * */
    private void setBtnKeyboard(int iId){
      final AlertDialog adCtrl;
      final String sModel = this.getLocalClassName();
      final int iBtnSite = iId;

      final AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(this.getString(R.string.dialog_title_keyboard_set));//????
      LayoutInflater inflater = LayoutInflater.from(this);
      //??xml?? ???final ???????????
      final View view = inflater.inflate(R.layout.dialog_set_keyboard,null);
      final EditText etBtnName =(EditText)view.findViewById(R.id.et_keyboard_set_BtnName);
      final EditText etDownVal =(EditText)view.findViewById(R.id.et_keyboard_set_btn_down_value);
      final EditText etHoldVal =(EditText)view.findViewById(R.id.et_keyboard_set_btn_hold_value);
      final EditText etUpVal =(EditText)view.findViewById(R.id.et_keyboard_set_btn_up_value);
      //????
      etBtnName.setText(mbtns[iBtnSite].getText().toString());//???????
      etDownVal.setText(this.mlBtnSendVal.get(iBtnSite).get("DOWN")); //????
      etHoldVal.setText(this.mlBtnSendVal.get(iBtnSite).get("HOLD")); //????
      etUpVal.setText(this.mlBtnSendVal.get(iBtnSite).get("UP")); //????
      builder.setView(view);
      builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener(){
          @Override
          public void onClick(DialogInterface dialog, int which){
            String sBtnName = etBtnName.getText().toString().trim();
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("DOWN", etDownVal.getText().toString().trim());
            hm.put("HOLD", etHoldVal.getText().toString().trim());
            hm.put("UP", etUpVal.getText().toString().trim());
            mbtns[iBtnSite].setText(sBtnName);//????????
            mlBtnSendVal.set(iBtnSite, hm);//???????
            /*????????*/
            mDS.setVal(sModel, SUB_KEY_BTN_NAME.concat(String.valueOf(iBtnSite)), sBtnName)
            .setVal(sModel, SUB_KEY_BTN_DOWN_VAL.concat(String.valueOf(iBtnSite)), hm.get("DOWN"))
            .setVal(sModel, SUB_KEY_BTN_HOLD_VAL.concat(String.valueOf(iBtnSite)), hm.get("HOLD"))
            .setVal(sModel, SUB_KEY_BTN_UP_VAL.concat(String.valueOf(iBtnSite)), hm.get("UP"))
            .saveStorage();
          }
      });
      adCtrl = builder.create();
      adCtrl.show();
      
      //?????????
      etDownVal.addTextChangedListener(new TextWatcher(){
      @Override
      public void afterTextChanged(Editable s){
        String sSend = etDownVal.getText().toString().trim();
        if (sSend.length() == 0 && etUpVal.getText().toString().trim().isEmpty() &&
          etHoldVal.getText().toString().trim().isEmpty())
        {//????????????
          adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
          return;
        }
        if (BluetoothSppClient.IO_MODE_HEX == mbtOutputMode){  //16?????????????
          if (CHexConver.checkHexStr(sSend)){
            etDownVal.setTextColor(android.graphics.Color.BLACK);
            adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
          }else{  //HEX????????
            adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            etDownVal.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){
      }
      });
      etHoldVal.addTextChangedListener(new TextWatcher(){
      @Override
      public void afterTextChanged(Editable s){
        String sSend = etHoldVal.getText().toString().trim();
        if (sSend.length() == 0 && etUpVal.getText().toString().trim().isEmpty() &&
          etDownVal.getText().toString().trim().isEmpty())
        {//????????????
          adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
          return;
        }
        if (BluetoothSppClient.IO_MODE_HEX == mbtOutputMode){  //16?????????????
          if (CHexConver.checkHexStr(sSend)){
            etHoldVal.setTextColor(android.graphics.Color.BLACK);
            adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
          }else{  //HEX????????
            adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            etHoldVal.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){
      }
      });
      etUpVal.addTextChangedListener(new TextWatcher(){
      @Override
      public void afterTextChanged(Editable s){
        String sSend = etUpVal.getText().toString().trim();
        if (sSend.length() == 0 && etHoldVal.getText().toString().trim().isEmpty() &&
          etDownVal.getText().toString().trim().isEmpty())
        {//????????????
          adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
          return;
        }
        if (BluetoothSppClient.IO_MODE_HEX == mbtOutputMode){  //16?????????????
          if (CHexConver.checkHexStr(sSend)){
            etUpVal.setTextColor(android.graphics.Color.BLACK);
            adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
          }else{  //HEX????????
            adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
            etUpVal.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){
      }
      });
    }
    
    /**
     * ????????
     * @return void
     * */
    private void selectRepeatFreq(){
      final AlertDialog adCtrl;
      /*???????*/
      final EditText etFreq = new EditText(this);
      etFreq.setHint(
        String.format(
          getString(R.string.actKeyBoard_tv_long_pass_freq_hint),
            BTN_REPEAT_MIN_FREQ)); //??????
      etFreq.setInputType(InputType.TYPE_CLASS_NUMBER); //???????????
      etFreq.setText(String.valueOf(this.miRepateFreq));
      /*?????*/
      AlertDialog.Builder builder = new AlertDialog.Builder(this); //??????
      builder.setTitle(this.getString(R.string.dialog_title_keyboard_long_pass_frea));//????
      builder.setView(etFreq);
      builder.setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener(){
      @Override
      public void onClick(DialogInterface arg0, int arg1){
        int iFreq;
        //??????????
        if (etFreq.getText().toString().isEmpty())
          iFreq= 0;
        else
          iFreq = Integer.valueOf(etFreq.getText().toString());
        setBtnRepeatFreq(iFreq); //????????
        String sTmp = String.format(getString(R.string.actKeyBoard_msg_repeat_freq_set)+"\n", iFreq);
        mtvSendView.setText(sTmp);//??????????????
        
      }
      });
      builder.setCancelable(false);
      adCtrl = builder.create();
      adCtrl.show();
      etFreq.addTextChangedListener(new TextWatcher(){//??????????????????
      @Override
      public void afterTextChanged(Editable arg0){
        int iFreq;
        //??????????
        if (etFreq.getText().toString().isEmpty())
          iFreq= 0;
        else
          iFreq = Integer.valueOf(etFreq.getText().toString());
        
        if (iFreq >= BTN_REPEAT_MIN_FREQ){
          etFreq.setTextColor(android.graphics.Color.BLACK);
          adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
        }else{
          etFreq.setTextColor(android.graphics.Color.RED);
          adCtrl.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
        }
      }

      @Override
      public void beforeTextChanged(CharSequence arg0, int arg1,
          int arg2, int arg3) {
      }

      @Override
      public void onTextChanged(CharSequence arg0, int arg1, int arg2,
          int arg3) {
      }
        
      });
    }
    
    /**
     * ??????????
     * @return void
     * */
    private void autoScroll(){
      int iOffset = 0;
    //?????????
    iOffset = this.mtvRecView.getMeasuredHeight() - this.msvRecView.getHeight();     
        if (iOffset > 0)
          this.msvRecView.scrollTo(0, iOffset);
        
        if (this.mrlSendArea.getVisibility() == View.VISIBLE){  //??????????????????????
      iOffset = this.mtvSendView.getMeasuredHeight() - this.msvSendView.getHeight();     
          if (iOffset > 0)
            this.msvSendView.scrollTo(0, iOffset);
        }
    }
    
    //----------------
    /*???????(??????????????)*/
    private class receiveTask extends AsyncTask<String, String, Integer>{
      /**???:????*/
      private final static int CONNECT_LOST = 0x01;
      /**???:????????*/
      private final static int THREAD_END = 0x02;
    /**
     * ?????????????
     */
    @Override
    public void onPreExecute(){
      mtvRecView.append(getString(R.string.msg_receive_data_wating));
      mbThreadStop = false;
    }
    /**
      * ????????
      */
    @Override
    protected Integer doInBackground(String... arg0){
      mBSC.Receive(); //???????????????????
      while(!mbThreadStop){
        if (!mBSC.isConnect())
          return CONNECT_LOST; //??????????
        else
          SystemClock.sleep(10);//???????????????
        
        if (mBSC.getReceiveBufLen() > 0)
          this.publishProgress(mBSC.Receive());
      }
      return THREAD_END;
    }
    /**
     * ?????????
     */
    @Override
    public void onProgressUpdate(String... progress){
      mtvRecView.append(progress[0]); //??????????
      autoScroll(); //???????????
      refreshRxdCount(); //???????????
    }
    /**
      * ?????????????????
      */
    @Override
    public void onPostExecute(Integer result){
      if (CONNECT_LOST == result) //??????
        mtvRecView.append(getString(R.string.msg_msg_bt_connect_lost));
      else
        mtvRecView.append(getString(R.string.msg_receive_data_stop));//????????
      refreshHoldTime(); //??????????????-?????
    }
    }
}




Java Source Code List

mobi.dzs.android.BLE_SPP_PRO.BaseActivity.java
mobi.dzs.android.BLE_SPP_PRO.BaseCommActivity.java
mobi.dzs.android.BLE_SPP_PRO.actAbout.java
mobi.dzs.android.BLE_SPP_PRO.actByteStream.java
mobi.dzs.android.BLE_SPP_PRO.actCmdLine.java
mobi.dzs.android.BLE_SPP_PRO.actDiscovery.java
mobi.dzs.android.BLE_SPP_PRO.actKeyBoard.java
mobi.dzs.android.BLE_SPP_PRO.actMain.java
mobi.dzs.android.BLE_SPP_PRO.globalPool.java
mobi.dzs.android.bluetooth.BTSerialComm.java
mobi.dzs.android.bluetooth.BluetoothCtrl.java
mobi.dzs.android.bluetooth.BluetoothSppClient.java
mobi.dzs.android.bluetooth.CResourcePV.java
mobi.dzs.android.control.button.ButtonPassListener.java
mobi.dzs.android.control.button.RepeatingButton.java
mobi.dzs.android.storage.CJsonStorage.java
mobi.dzs.android.storage.CKVStorage.java
mobi.dzs.android.storage.CSharedPreferences.java
mobi.dzs.android.util.CHexConver.java
mobi.dzs.android.util.LocalIOTools.java