public void doConnect(View view) {
   if (tgDevice.getState() != TGDevice.STATE_CONNECTING
       && tgDevice.getState() != TGDevice.STATE_CONNECTED) {
     tgDevice.connect(rawEnabled);
   } else {
     tgDevice.close();
   }
 }
        @Override
        public void handleMessage(Message msg) {
          boolean att = false, med = false, bl = false, hr = false;
          switch (msg.what) {
            case TGDevice.MSG_STATE_CHANGE:
              switch (msg.arg1) {
                case TGDevice.STATE_IDLE:
                  break;
                case TGDevice.STATE_CONNECTING:
                  status.setText("connecting...");
                  connectButton.setEnabled(false);
                  break;
                case TGDevice.STATE_CONNECTED:
                  status.setText("connected");
                  tgDevice.start();
                  connectButton.setEnabled(false);
                  break;
                case TGDevice.STATE_NOT_FOUND:
                  status.setText("can't find");
                  connectButton.setEnabled(true);
                  break;
                case TGDevice.STATE_NOT_PAIRED:
                  status.setText("not paired");
                  connectButton.setEnabled(true);
                  break;
                case TGDevice.STATE_DISCONNECTED:
                  status.setText("disconnected");
                  connectButton.setEnabled(true);
                  blval = 0;
              }
              break;
            case TGDevice.MSG_POOR_SIGNAL:
              // signal = msg.arg1;
              status.setText("Signal: %" + (200 - msg.arg1) * 100 / 200);
              break;
            case TGDevice.MSG_RAW_DATA:
              // raw1 = msg.arg1;
              // tv.append("Got raw: " + msg.arg1 + "\n");
              break;
            case TGDevice.MSG_HEART_RATE:
              // heartrate.setText("HeartRate=" + msg.arg1);
              // hrval=msg.arg1;
              // hr = true;
              break;
            case TGDevice.MSG_ATTENTION:
              attention.setText("Attention: " + msg.arg1);
              attval = msg.arg1;
              att = true;
              break;
            case TGDevice.MSG_MEDITATION:
              meditation.setText("Meditation: " + msg.arg1);
              medval = msg.arg1;
              med = true;
              break;
            case TGDevice.MSG_BLINK:
              blink.setText("Blink: " + msg.arg1);
              blval = msg.arg1 + blval;
              bl = true;
              break;
            case TGDevice.MSG_RAW_COUNT:
              // tv.append("Raw Count: " + msg.arg1 + "\n");
              break;
            case TGDevice.MSG_LOW_BATTERY:
              Toast.makeText(getApplicationContext(), "Low battery!", Toast.LENGTH_SHORT).show();
              break;
            case TGDevice.MSG_RAW_MULTI:
              // TGRawMulti rawM = (TGRawMulti)msg.obj;
              // tv.append("Raw1: " + rawM.ch1 + "\nRaw2: " + rawM.ch2);
            default:
              break;
          }
          if (bl || med || att) {
            (new AsyncTask<Integer, String, Void>() {
                  String result = "stop";

                  @Override
                  protected Void doInBackground(Integer... params) {
                    result = send(params[0], params[1], params[2], params[3]);
                    //     onProgressUpdate(result);
                    return null;
                  }

                  @Override
                  protected void onProgressUpdate(String... result) {
                    if (result.equals("started")) {
                      updateState("State: START!", Color.GREEN);
                    } else if (result.equals("done")) {
                      updateState("State: DONE!", Color.BLUE);
                    } else {
                      updateState("State: " + result, Color.RED);
                    }
                  }
                })
                .execute(attval, medval, blval, hrval);
          }
        }
 private void close() {
   tgDevice.close();
   connectButton.setEnabled(true);
 }