Unable to write data to BLE device

Hello.

I am just getting started with AppInventor and have created a simple Android app which just needs to send a character to my Arduino based upon the click of a button. I have a BLE device on my Arduino (SH-HC-08), so I have, within the last day, downloaded and installed the BluetoothLE extension. I can connect successfully from the app using the MAC address. I can also connect by scanning and finding the device and connecting to it that way. So connection is all good.

The manufacturer DSD Tech provides a simplistic app that can send and receive text with my application. That connects and works fine. My intent with MIT AppInventor is to create a more tailored app for my application rather than manually typing in and seeing text strings to/from my Arduino.

Where I'm having trouble is sending any kind of data. For the BLE device, it looks like my options are things like WriteByte, WriteString, WriteShort, etc. So I am trying to use WriteString:

foo

I double checked my UUIDs (which are the same) and they are correct. I've tried various ways of doing this. Here, I directly provide a constant string for the value. I also tried providing a list of one element, that element being the one string I wish to send. On every attempt, a call to WriteString just crashes the app instantly. It exits suddenly with no message or other indication.

I see others are successfully using the BLE extension and these particular Write methods, so I assume this is something I'm doing wrong, or maybe something's gotten corrupted? I did a lot of searching and I don't see anyone else reporting this experience. I'm looking for a little advice on how to resolve it. Do I need to start fresh and reconstruct the app?

Thank you!

UPDATE: Here's the whole collection of app blocks:

Here's the associates sketch:

float temperatureCtoF(float t) {
  return (t * 9.0)/5.0 + 32.0;
}

float temperatureFtoC(float t) {
  return (t - 32)*5.0/9.0;
}

// R from V, given that the series resistor (Rs) goes to ground
//
float computeRfromV(float V) {
  const float Rs = 10010;   // Series resistor
  return (Rs * (1023.0 - V)) / V;
}

// Simpler formula, using B coefficient
// Compared to an independent sensor, this formula appears more accurate
//
float computeTemperature(int Vo) {
  const float B = 3950;         // Middle value taken from the data sheet
                                // Computed as ln(Rt1/Rt2)/(1/t1 - 1/t2)

  const float Rt = 10950;       // Resistance of thermistor at nominal temp
  const float T0F = 75;         // Nominal temp (F)

  float R = computeRfromV(Vo);
  float T0 = temperatureFtoC(T0F) + 273.15;   // nominal room temperature, degK
  float Tc = (B * T0) / (B + T0*log(R/Rt)) - 273.15;

  return Tc;
}

float average(float value) {
  const byte nvalues = 8;
  static byte current = 0;      // Inde for current value
  static float sum = value * nvalues;
  static bool first = true;
  static float values[nvalues];

  if (first) {
    for (int i = 0; i < nvalues; i++)
      values[i] = value;
  }
  else {
    sum = sum - values[current] + value;

    if (++current >= nvalues)
      current = 0;

    values[current] = value;
  }

  return sum/nvalues;
}

volatile char scale = 'F';

void displayTemperature(float temp, char scale) {
  if ( scale == 'F' )
    temp = temperatureCtoF(temp);

  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println(scale);
}

void getTemperature() {
  static char last_scale = scale;
  static int last_temp = 9999;

  int V0 = analogRead(A1);
  float Tc = computeTemperature(V0);
  int t = Tc;

  if ( t != last_temp || scale != last_scale ) {
    displayTemperature(t, scale);
    last_scale = scale;
    last_temp = t;
  }
}

void setup() {
  // Serial port for tracing/debugging
  Serial.begin(115200);

  // Configure bluetooth serial
  Serial1.begin(9600);
}

void loop() {
  const uint32_t tempReadTime = 1000;  // Read temperature once per second
  static uint32_t lastTime = millis();

  // Check to see if we have a bluetooth request to change the temperature scale
  if (Serial1.available()) {
    char s = Serial1.read();

    if (s == 'C' || s == 'F') {
      scale = s;
      Serial1.print(scale);
    }
  }

  uint32_t currentTime = millis();

  if (currentTime - lastTime > tempReadTime) {
    getTemperature();
    lastTime = currentTime;
  }
}

Hello Mark

  1. Make sure your phone has location switched on.
  2. Make sure your phone's Bluetooth is also BLE (The HC-08 is v4.0)

If those are correct, it is most likely something to do with your Arduino Sketch.

Hi Chris

Thank you for the quick reply. Some additional information:

  • My phone location is switched on. But what is location used for in this case?
  • The phone does support BLE. In fact, I'm able to connect and send/receive text with my Arduino application using the manufacturer's simplistic application that sends text and receives text (mfg is DSD Tech). I wanted to use MIT AppInventor to create a more tailored app.
  • In my example that fails, I have not received anything from the Arduino app. I'm only sending so far, or attempting to send, and that attempt is crashing.
  • I am only using AppInventor for the mobile app side. The Arduino side I've written directly.

So, I suspect there's a problem on the application side somewhere.

Security of the connection. This is a Google Android security measure.

That is a different scenario - always tell us the way it is from the get-go, we are a small team of volunteers with 400,000 Active Users to help :koala:

In the Blocks Work Area, select "download blocks as image" from the right-click pop-up menu.

Change the Sketch extension to .txt to post here.

That is a different scenario

I'm sorry for the misunderstanding. I understand the sensitivity to time and needed information, and am trying to be sensitive to that. I appreciate very much any help that can be offered. I only posted a WriteString, so I thought it was clear that I am not receiving from Arduino but writing to it.

I updated my original post with the whole app as blocks shown.

It would be helpful to know which version of the BLE extension you are using and where you found it.

ABG, I'll check on the version number. As I mentioned, I just downloaded it from the official site and installed it within the last day, so it should be the latest.

What phone are you using?

Hy Patryk_F - I am using an LG G7 ThinQ, Android 9, Kernel 4.9.112. It supports BLE. I can use the manufacturer's app and talk via BLE just fine.

So as I mentioned before, we need to see your Sketch :upside_down_face:

I've attached the sketch to my original post as well. As I mentioned, all it does on the Bluetooth currently is look to receive a C or an F to change the temperature scale. It does not send anything back to the app. Also, as I mentioned, if I use the DSD Tech Bluetooth app, which just connects and will send or receive text strings over the connection, that works fine. I can send "C" or "F" and it works as expected. When I use the AppInventor app, as soon as I touch the F or C button, it crashes instantly.

Export your .aia file and upload it here.

Service: "0000ffe0-0000-1000-8000-00805f9b34fb”

Characteristic: “0000ffe1-0000-1000-8000-00805f9b34fb”

Patryk, you nailed it! I didn't see that one digit difference in the two UUIDs. I thought I had read in some example somewhere that they could be identical, which led me not to examine them closely enough. Thank you!

Where did you find the correct values?

In Google :wink:

They are the same as for hm10.

In my case, I used the "LightBlue" app which fetches all the characteristics of your Bluetooth device and I read them off of that. I obviously missed one digit, though. :-/

(UUIDs added to FAQ)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.