@Override
        public boolean onLongClick(View v) {
          LedButton btn = (LedButton) v;
          int index = ledButtons.indexOf(btn);

          currColorIndex = (currColorIndex + 1) % 3 + 1;
          // Log.v(TAG, "currColorIndex = " + currColorIndex);
          ledColors.set(index, currColorIndex);
          btn.setColorIndex(currColorIndex);
          if (drawMode) {
            sendLedColor(btn);
          }
          return true;
        }
        @Override
        public void onClick(View v) {
          LedButton btn = (LedButton) v;
          int index = ledButtons.indexOf(btn);

          if (ledColors.get(index) == 0) {
            ledColors.set(index, currColorIndex);
            btn.setColorIndex(currColorIndex);
          } else {
            ledColors.set(index, 0);
            btn.setColorIndex(0);
          }

          if (drawMode) {
            sendLedColor(btn);
          }
        }
 public void sendLedColor(LedButton btn) {
   int index = ledButtons.indexOf(btn);
   sendMessage(
       "L:" + (index / MATRIX_N) + "," + (index % MATRIX_N) + "," + btn.getColorIndex() + "\n");
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_upload_matrix);

    super.onCreate(savedInstanceState);

    activity = this;

    mtxDAO = new LedMatrixDAO(this);

    final Intent intent = getIntent();
    currMatrix = intent.getParcelableExtra(EXTRAS_LED_MATRIX);
    if (currMatrix == null) {
      currMatrix = mtxDAO.getDummyLedMatrix();
    }

    GridLayout grid = (GridLayout) findViewById(R.id.gridLayout);
    String ledColorsStr = currMatrix.getMatrix();
    if (ledColorsStr == null) {
      ledColorsStr = LedMatrixDAO.MTX_BLANK;
    }

    assert (ledColorsStr.length() == MATRIX_NN);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int btnWidth = (displaymetrics.widthPixels - 50) / MATRIX_N - 4;
    Log.v(TAG, "btnWidth = " + btnWidth);

    char _c;
    int _ci;
    LedButton btn;
    for (int i = 0; i < MATRIX_NN; i++) {
      _c = ledColorsStr.charAt(i);
      _ci = Integer.valueOf("" + _c);

      ledColors.add(_ci);

      btn = new LedButton(this);
      btn.setColorIndex(_ci);
      ledButtons.add(btn);

      btn.setMinimumWidth(btnWidth);
      btn.setMinimumHeight(btnWidth);
      btn.setMaxWidth(btnWidth);
      btn.setMaxHeight(btnWidth);

      btn.setOnClickListener(onLedBtnClickListener);
      btn.setOnLongClickListener(onLedBtnLongClickListener);

      grid.addView(btn);
    }

    // Sets up UI references.
    mConnectionState = (TextView) findViewById(R.id.connection_state);
    // is serial present?
    isSerial = (TextView) findViewById(R.id.isSerial);

    switchDraw = (Switch) findViewById(R.id.switchDraw);
    switchDraw.setOnCheckedChangeListener(
        new CompoundButton.OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            updateMatrixInfo(currMatrix);
            if (isChecked) {
              drawMode = true;
              sendMessage("B:" + currMatrix.getMatrix() + "\n");
            } else {
              drawMode = false;
            }
          }
        });

    returnText = (TextView) findViewById(R.id.textReturn);

    buttonSave = (Button) findViewById(R.id.saveButton);
    buttonDelete = (Button) findViewById(R.id.deleteButton);
    buttonSend = (Button) findViewById(R.id.sendButton);

    buttonSave.setOnClickListener(onClickListener);
    buttonDelete.setOnClickListener(onClickListener);
    buttonSend.setOnClickListener(onClickListener);

    infoButton = (ImageView) findViewById(R.id.infoImage);
    infoButton.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            iascDialog();
          }
        });

    getActionBar().setTitle(getString(R.string.title_image) + ":" + currMatrix.getName());
    getActionBar().setDisplayHomeAsUpEnabled(true);

    Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
    bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

    toastMessage(getString(R.string.opt_info));
  }