PSoC to Android Studio: Turning off and on an LED with two buttons

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AnCo_2736831
Level 4
Level 4
25 likes received 10 likes received 10 likes given

Hi All,

I have made a BLE app that connects the PSoC device to the Android device, using PSoC Creator 4 and Android Studio. I used the "How to Create a PSoC Android BLE App tutorial series, http://www.cypress.com/video-library/PSoC-Software/how-create-psoc-android-ble-app-lesson-1-getting-...​, and modified it so that it only turns off and on an LED, instead of the additional capsense feature.

However, in the tutorials, instead of using two buttons, one to turn on, and one to turn off the LED, it used one switch. The switch would work as one component with two functions, one to turn on, and one to turn off the LED. Although that is good, I would like to have two buttons to turn on/off the LED.

How do you convert the program in Android Studio from one switch to two buttons, so that the end result will be the same in that I will be able to control the LED and turn it on/off?

Thanks,

Andrew Collins

0 Likes
1 Solution
Anonymous
Not applicable

Ah; I think I see the problem

The writeLedCharacteristic() is using a single characteristic, and writing a 0 or 1 to it to tell the demo project to turn the LED on or off.

Since you split the functionality of the switch into two buttons, and want two separate characteristics, one for each button, to signal when the LED turns on or turns off, then the characteristic for only one of them will get written correctly

If you have your code tied to calling the writeLedCharacteristic(ON/OFF) function, then you could merely change the inside to be:

public void writeLedCharacteristic(boolean value) {

   byte[] byteVal = new byte[1];

   if (value) {

  byteVal[0] = (byte) (1);

   mONCharacterisitc.setValue((byte)(1)); //sets the value into the characteristic structure for sending it to the remote device

   mBluetoothGatt.writeCharacteristic(mONCharacterisitc); //sends the structure to the remote device

  } else {

  byteVal[0] = (byte) (0);

   mOFFCharacterisitc.setValue((byte)(1)); //sets the value into the characteristic structure for sending it to the remote device

   mBluetoothGatt.writeCharacteristic(mOFFCharacterisitc); //sends the structure to the remote device

  }

  Log.i(TAG, "LED " + value);

   mLedSwitchState = value; //Not sure what this is for, probably a local variable to track the switch state for the gui

}

mOFFCharacteristic and mONCharacteristic would be the UUID handles for the two characteristics for turning it on/off

(If you don't need to split the two buttons to have their own characteristics, then this seems like overcomplicating it)

Basically, the writeLEDCharacteristic function worked as a toggle, for toggling the value of the characteristic between 1 and 0, and this is probably shown back to the gui using mLedSwitchState.

You will want to change the buttons to fire two different writeLEDCharacteristic() functions, or have them call the same function with different values, and then change which characteristic handle is used to write to remote device to change which of the two BLE Characteristics you are writing values to.

PS: Most likely, there is a location where the UUID of the mLEDCharacteristic is being set. You will need to change that location of code to write the correct two UUIDs for the two "mOnCharacteristic" and "mOFFCharacteristic" structures in the sample code above.

View solution in original post

0 Likes
9 Replies
Anonymous
Not applicable

Change the android code to have two buttons (duplicate the switch code into two buttons by modifying names etc. to make them unique)

Then change the code that runs when the "switch" is presssed to be code that runs for each button when pressed

0 Likes

Hi,

I have tried to convert the Android Program, and have successfully completed most of it. However, there is one section of code that I am having trouble to convert from switch to button:

/* This will be called when the LED On/Off switch is touched */
led_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

   // Turn the LED on or OFF based on the state of the switch
   mPSoCCapSenseLedService.writeLedCharacteristic(isChecked);

  }

});

Do you know how to convert this into two buttons?

Thanks,

Andrew

0 Likes
Anonymous
Not applicable

It looks like a handler for a switch event state changed; So you would need to change it from one to two event handlers, with one for each button. Here is a thread talking about the more technical details of the android event handling for buttons: Button Click Listeners in Android - Stack Overflow

And another thread that might help: java - Android: how to handle button click - Stack Overflow

I would try something like (copy-pasted from second thread link):

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton
.setOnClickListener( new OnClickListener() {

  
@Override
  
public void onClick(View v) {
  
// TODO Auto-generated method stub
  
***Do what you want with the click here***
  
}
  
});

Then, create two of these; One for button 1 to turn the LED on, and one for button 2 to turn the LED off.

0 Likes

Hi Pratt,

Thanks for the useful information! However, when I was changing the code, I ran into a few problems.

Below is my code that I converted from switch to button:

  1. Button clickButton = (Button) findViewById(R.id.btn_ON);
  2. clickButton.setOnClickListener( new MainActivity() {
  3.    public void onClick(View v) {
  4.    mPSoCCapSenseLedService.writeLedCharacteristic(true);
  5.   }
  6. });

*btn_ON stands for the button that turns the LED on

The problems that occured were:

  1. "Cannot resolve symbol 'setOnClickListener' - line 2
  2. "Variable 'onClick' is never used" - line 4
  3. "Cannot resolve symbol 'v' - line 4

Do you know how to fix these problems?

0 Likes
Anonymous
Not applicable

Sorry, not really I don't know much about android development.

But from the looks of it, all three of those issues are related to the setOnClickListener function not being recognized.

Check if you need to import or include a library for buttons/components, or be more explicit with the library declaration/reference for the onclick listener function.

0 Likes

Pratt,

No worries. Through a bit more research I found that if I moved the entire chunk of program (for the onClickListener of the Button) inside the brackets of the onCreate void, then the program would run with no errors!

I have made the LED ON button with no problem, so I decided to duplicate it for the LED OFF. However, when I do this, it does nothing. I switched everything so it is supposed to make the LED turn off instead of on, and I made two different UUIDs (characteristics) from the PSoC, one turning it on and the other turning it off. I know that these should work because I tried out my app in the CySmart app to see if it works. The first characteristic turns it on, and the second turns it off.

Here is my PSoC code for turning it ON:

red_Write(!wrReqParam->handleValPair.value.val[0]);

...And for turning it OFF:

red_Write(!wrReqParam->handleValPair.value.val[1]);

However, if there is no problem in those two functions, I may have narrowed down the problem to this set of program in Android Studio that was used earlier for the switch:

public void writeLedCharacteristic(boolean value) {

   byte[] byteVal = new byte[1];

   if (value) {

  byteVal[0] = (byte) (1);

  } else {

  byteVal[0] = (byte) (0);

  }

  Log.i(TAG, "LED " + value);

   mLedSwitchState = value;

   mLedCharacterisitc.setValue(byteVal);

   mBluetoothGatt.writeCharacteristic(mLedCharacterisitc);

}

When I duplicated this for the LED OFF button, I had it so the (byte) (1) and (byte) (0) would be switched, but this does not do anything.

Do you understand what this section of programming is doing? If so, can you make it so that it turns off the LED instead of turning it ON?

0 Likes
Anonymous
Not applicable

Ah; I think I see the problem

The writeLedCharacteristic() is using a single characteristic, and writing a 0 or 1 to it to tell the demo project to turn the LED on or off.

Since you split the functionality of the switch into two buttons, and want two separate characteristics, one for each button, to signal when the LED turns on or turns off, then the characteristic for only one of them will get written correctly

If you have your code tied to calling the writeLedCharacteristic(ON/OFF) function, then you could merely change the inside to be:

public void writeLedCharacteristic(boolean value) {

   byte[] byteVal = new byte[1];

   if (value) {

  byteVal[0] = (byte) (1);

   mONCharacterisitc.setValue((byte)(1)); //sets the value into the characteristic structure for sending it to the remote device

   mBluetoothGatt.writeCharacteristic(mONCharacterisitc); //sends the structure to the remote device

  } else {

  byteVal[0] = (byte) (0);

   mOFFCharacterisitc.setValue((byte)(1)); //sets the value into the characteristic structure for sending it to the remote device

   mBluetoothGatt.writeCharacteristic(mOFFCharacterisitc); //sends the structure to the remote device

  }

  Log.i(TAG, "LED " + value);

   mLedSwitchState = value; //Not sure what this is for, probably a local variable to track the switch state for the gui

}

mOFFCharacteristic and mONCharacteristic would be the UUID handles for the two characteristics for turning it on/off

(If you don't need to split the two buttons to have their own characteristics, then this seems like overcomplicating it)

Basically, the writeLEDCharacteristic function worked as a toggle, for toggling the value of the characteristic between 1 and 0, and this is probably shown back to the gui using mLedSwitchState.

You will want to change the buttons to fire two different writeLEDCharacteristic() functions, or have them call the same function with different values, and then change which characteristic handle is used to write to remote device to change which of the two BLE Characteristics you are writing values to.

PS: Most likely, there is a location where the UUID of the mLEDCharacteristic is being set. You will need to change that location of code to write the correct two UUIDs for the two "mOnCharacteristic" and "mOFFCharacteristic" structures in the sample code above.

0 Likes

Thanks so much Pratt! The two buttons finally work, and now I can successfully turn the LED Off/On!

I am truly grateful for how much you helped me to solve this problem.

Thanks again,

Andrew Collins

Anonymous
Not applicable

I'm glad it is working

And you're welcome for the little help I gave!

0 Likes