@Override
        public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
            mConnected = true;
            updateConnectionState(R.string.connected);
            invalidateOptionsMenu();
          } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
            mConnected = false;
            updateConnectionState(R.string.disconnected);
            invalidateOptionsMenu();
          } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
            BluetoothGattService gattService = mBluetoothLeService.getSoftSerialService();
            characteristicTX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
            // characteristicRX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
            characteristicRX = characteristicTX;

            if ((gattService != null) && (characteristicTX != null)) {
              mBluetoothLeService.setCharacteristicNotification(characteristicTX, true);

              isSerial.setText("Serial ready");
              updateReadyState(R.string.ready);
            } else {
              isSerial.setText("Serial can't be found");
            }

          } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            displayData(intent.getStringExtra(mBluetoothLeService.EXTRA_DATA));
          }
        }
 @Override
 public void onServiceConnected(ComponentName componentName, IBinder service) {
   mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
   if (!mBluetoothLeService.initialize()) {
     Log.e(TAG, "Unable to initialize Bluetooth");
     finish();
   }
   // Automatically connects to the device upon successful start-up initialization.
   mBluetoothLeService.connect(mDeviceAddress);
 }
  private void sendMessage(String msg) {
    Log.d(TAG, "Sending Result=" + msg);

    if (characteristicReady
        && (mBluetoothLeService != null)
        && (characteristicTX != null)
        && (characteristicRX != null)) {
      characteristicTX.setValue(msg);
      mBluetoothLeService.writeCharacteristic(characteristicTX);
      mBluetoothLeService.setCharacteristicNotification(characteristicRX, true);
    } else {
      Toast.makeText(DeviceControlActivity.this, "BLE Disconnected", Toast.LENGTH_SHORT).show();
    }
  }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_connect:
       mBluetoothLeService.connect(mDeviceAddress);
       return true;
     case R.id.menu_disconnect:
       mBluetoothLeService.disconnect();
       return true;
     case android.R.id.home:
       onBackPressed();
       return true;
   }
   return super.onOptionsItemSelected(item);
 }
  @Override
  protected void onResume() {
    super.onResume();

    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
    if (mBluetoothLeService != null) {
      final boolean result = mBluetoothLeService.connect(mDeviceAddress);
      Log.d(TAG, "Connect request result=" + result);
    }
  }