BLE BytesReceived is not a number

Hello,

Currently, I am facing some problems with MIT BLE function. I am able to connect to my BLE with App Inventor. Following on, I am supposed to send bytes over which I have also done it successfully.

However, I couldn’t do addition or any multiplication to the value that is being transmitted over. I have been looking up on the forum searching for keywords like “converting integers to numbers”. I have also used the “is number?” block as well and the value it returned to me was false. Have I done wrongly somewhere? Any help will be greatly appreciated as I have been stuck on this for days… This is the error I have gotten “the operation * cannot accept the arguments”

I read it on Reddit saying that app inventor auto converts numbers and text back and forth. Hence, I am pretty confused now too.

I could only attach one picture as I am a new user,

Your problem isn’t converting text to numbers.
It’s converting lists to numbers, which AI2 does not do.

Notice how the blocks names say Bytes, not Byte.
That means you will get a list of them.

When you try to treat a list like a text or number, AI2 wraps it with a () or []
like a sloppy chef resentful that no one removed his potatoes from the grocery bag,
so he cooks them in the bag.

Then the math blocks complain at having to pick () out of their teeth.

Use a for each byte in byteValues loop
and inside it set ConvertedValue.Text to byte + 10.

Hello JoonWei_Chong

What is your BLE device that is sending data to your App?

The value sent (in bytes) is assumed to be a number (integer?) but is it?

Do you have control of the BLE device - for example, if it’s a microcontroller, you would program the device with a script?

If you need to receive string, look this example "StringsReceived" with ().

(43)

Hi ABG,

Thanks for taking ur time to reply. Alright i will take a look into how to extract the numbers out from a list by using the blocks under the “List” section! I will be back to update the progress of it! Thanks a lot.

Hello ChrisWard,

I am using TinyShield BLE and TinyZero Accelerometer currently and i have modified from the example program given. Supposedly, i would be sending values of the accelerometer through the BLE. Hence, yes i do have control of the BLE device and i think it should be an integer. Probably i just need to do some trial and error with the app inventor BLE functions.

Below is the link where i get my example program if you are interested, https://learn.tinycircuits.com/Communication/Bluetooth-Low-Energy_TinyShield_Tutorial/

Hi Juan Antonio,

Thanks for the guide! I will go and look through it for insights.

I looked in the .zip file, and found:

//-------------------------------------------------------------------------------
// TinyCircuits ST BLE TinyShield UART Example Sketch
// Last Updated 2 March 2016
//
// This demo sets up the BlueNRG-MS chipset of the ST BLE module for compatiblity
// with Nordic's virtual UART connection, and can pass data between the Arduino
// serial monitor and Nordic nRF UART V2.0 app or another compatible BLE
// terminal. This example is written specifically to be fairly code compatible
// with the Nordic NRF8001 example, with a replacement UART.ino file with
// 'aci_loop' and 'BLEsetup' functions to allow easy replacement.
//
// Written by Ben Rose, TinyCircuits http://tinycircuits.com
//
//-------------------------------------------------------------------------------

#include <SPI.h>
#include <STBLE.h>

//Debug output adds extra flash and memory requirements!
#ifndef BLE_DEBUG
#define BLE_DEBUG true
#endif

#if defined (ARDUINO_ARCH_AVR)
#define SerialMonitorInterface Serial
#elif defined(ARDUINO_ARCH_SAMD)
#define SerialMonitorInterface SerialUSB
#endif

uint8_t ble_rx_buffer[21];
uint8_t ble_rx_buffer_len = 0;
uint8_t ble_connection_state = false;
#define PIPE_UART_OVER_BTLE_UART_TX_TX 0

void setup() {
SerialMonitorInterface.begin(9600);
while (!SerialMonitorInterface); //This line will block until a serial monitor is opened with TinyScreen+!
BLEsetup();
}

void loop() {
aci_loop();//Process any ACI commands or events from the NRF8001- main BLE handler, must run often. Keep main loop short.
if (ble_rx_buffer_len) {//Check if data is available
SerialMonitorInterface.print(ble_rx_buffer_len);
SerialMonitorInterface.print(" : ");
SerialMonitorInterface.println((char*)ble_rx_buffer);
ble_rx_buffer_len = 0;//clear afer reading
}
if (SerialMonitorInterface.available()) {//Check if serial input is available to send
delay(10);//should catch input
uint8_t sendBuffer[21];
uint8_t sendLength = 0;
while (SerialMonitorInterface.available() && sendLength < 19) {
sendBuffer[sendLength] = SerialMonitorInterface.read();
sendLength++;
}
if (SerialMonitorInterface.available()) {
SerialMonitorInterface.print(F("Input truncated, dropped: "));
if (SerialMonitorInterface.available()) {
SerialMonitorInterface.write(SerialMonitorInterface.read());
}
}
sendBuffer[sendLength] = '\0'; //Terminate string
sendLength++;
if (!lib_aci_send_data(PIPE_UART_OVER_BTLE_UART_TX_TX, (uint8_t*)sendBuffer, sendLength))
{
SerialMonitorInterface.println(F("TX dropped!"));
}
}
}

If you try to do math on the BLE output of this sketch, it will choke on the
":" and string length sent in the void loop part.

In AI2, you can use text split at : to break out the
length and string into separate list items.

Hi ABG,

I think I might have solved the current problem that I was facing previously due to the lack of understanding of the MIT BLE functions. Thanks to your advice above, I went to test the list functions and to my surprise I got it! Below are the new amendments I have made to App Inventor,


The first value was 86 which was in a list like what you said with the (). Hence after extracting the number in the list, I was able to add 10 into the value. The second line was just me testing the length of the list. I guess everything worked out pretty fine! I have also found out that the connection of the BLE will drop if u switch screens while reading up other posts.

I am thankful for all the help I have received in this forum! I will be continuing on my mini project and hopefully, there will not be any more hiccups.

Best Regards,
Chong Joon Wei

Glad it worked for you.

I don’t have hands on experience with BLE, but from what I have
read on this forum, you might not need to read Bytes in the Clock Timer
after you have registered For Bytes. They should arrive and trigger their event
automatically once Registered.

(I may be wrong.)

1 Like

This is correct. BLE provides a mechanism for a peripheral to notify a central device that there is new data. The RegisterFor... methods tie into this mechanism (compared with Read..., which attempts a read regardless of new data). Once you've registered, your app will be continually notified of new data until you call Unregister.... This eliminates the need for a Clock timer and is more power efficient since it's managed by the hardware rather than the software.

1 Like

Hi Evan,

Thanks for the information, much appreciated!

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