简体   繁体   中英

HM-10 read, write, notify, Can i do it all at once?

I have written the following code below using the SoftwareSerial.h library

void loop(){
//BT LED READ
    if(BTSerial.available())
    {
        int c = BTSerial.read();
        Serial.println(c);
        Serial.print("\n");

        if (c == 1)
            digitalWrite(ledpin, HIGH);

        else if (c == 0)
          digitalWrite(ledpin, LOW);

  //BUTTON PRESS CODE
        buttonState = digitalRead(inputPin);
        if(buttonState == HIGH)
        {
            digitalWrite(outputLed, HIGH);
            BTSerial.print(22);
        }
        else
        {
            digitalWrite(outputLed, LOW);
        } 
    }

//Serial.println(digitalRead(state));

}

To accompany my code, I have written a mobile application in Xamarin studio. What my code does, is that it accepts a call from the application (either 0 or 1 byte) which makes it turn on an LED.

Secondly, what I'm trying to accomplish, and yet struggle with, is that I want the Arduino to notify my application (or write... I'm really not sure what this library allows me to do since I have a hard time finding some elaborate documentation) when a button on my breadboard is pressed - it turns on an LED too when pressed.

I realized that if I encapsulate the code for the button press inside BTSerial.available() it doesn't work, meaning that it doesn't send any data to my application, nor does the LED turn on when I press the button. - It does though still accept parameters being sent to it.

Vise versa, if I don't encapsulate my button code inside the BTSerial.available() I can write data to my phone, but my phone cannot send data to the BT device.

What is going on here, and why can't I access the two functionalities simultaneous, and what is a potential fix?

I think you are not clear about the properties of BLE characteristic and how HM-10 function. The way it does is essentially an UART bridge, that everything goes to RX from microcontrollers will be sent as the notification of the characteristic, and everything coming from characteristic write will pass on to the TX and then to microcontrollers. So in your case, you have to get the part of button press out of the BTSerial.available() part, so that the two functions you have are independent. And you have to turn on the notification of your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM