Estoy enviando varias cadenas por bluetooth desde arduino a Android, al llegar a la aplicación hago subcadenas para poder obtener los datos que necesito. Sin embargo, las cadenas están llegando mal, se cortan y por lo tanto no puedo obtener las substrings que necesito. Por ejemplo: Desde Arduino mando cadenas de este estilo: #ecg{21304(2.41~
Al enviar varias cadenas en el monitor serial de arduino se ven asi:
#ecg{21304(2.41~#ecg{21324(3.37~#ecg{21324(1.98~#ecg{21345(3.14~#ecg{21345(3.05~#ecg{21365(1.98~#...
Pero cuando llegan al celular llegar cortadas, algo como esto:
g{ 21304(2.41 ~#e cg{21324(3.37~#ec g{21324(1.98~#ecg{21345(3. 14~#ecg {21345(3.05~#ecg{21365(1.98~#...
[Observese que desde un inicio no están llegando completas en la mayoría de los casos] Y por tanto no puedo haces las substrings de los datos que necesito. ¿Saben por qué se cortan y no llegan completas a como las envío desde el arduino? ¿Hay manera de solucionarlo?
La recepción de cadenas incompletas podría deberse a la velocidad de transmisión usada en el código Arduino (115200)? Hice pruebas cargando un programa en otro arduino que envía datos via bluetooth a la misma app y estos llegan bien, la diferencia es que esté envía los datos a 9600
Este es el código de las cadenas que envio desde arduino:
#include <eHealth.h>
float datosECG[200], ImprimeDatosECG[100];
float PosPar;
int pos,i,cont=0;
unsigned long previousMillis = 0;
const long intervaloRequerido = 200;
// The setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
}
// The loop routine runs over and over again forever:
void loop() {
unsigned currentMillis = millis();
if(eHealth.getECG()>0.5)
{
for(i=0;i<100;i++)
{
pos=i;
datosECG[i] = eHealth.getECG();
// Serial.println(datosECG[i]);
PosPar = pos%50;//Con 50 conecta el bluetooth pero no envia datos por bluetooth y la grafica pierde mucha informacion
if(PosPar==0)
{
Serial.print('#');
Serial.print("ecg");
Serial.print('{');
Serial.print(currentMillis);
Serial.print('(');
Serial.print(datosECG[i]);
Serial.print('~');
}
}
delay(1); // wait for a millisecond
}
}
Esta es la parte donde se hace el procesamiento para hacer las subcadenas:
switch (msg.what) {
case CONST.MSG_LEER:
String readMessage = (String) msg.obj;
recDataString.append(readMessage);
int charInicioCadena = recDataString.indexOf("#");
int charInicioTiempo = recDataString.indexOf("{");
//int charFinTiempo = recDataString.indexOf("}");
int charInicioValor = recDataString.indexOf("(");
//int charFinValor = recDataString.indexOf(")");
int charFinCadena = recDataString.indexOf("~");
System.out.println("Pos de Caracter de Inicio de cadena: "+charInicioCadena);
System.out.println("Pos de Caracter de Inicio de tiempo: "+charInicioTiempo);
//System.out.println("Pos de Caracter de fin de tiempo: "+charFinTiempo);
System.out.println("Pos de Caracter de Inicio de valor: "+charInicioValor);
//System.out.println("Pos de Caracter de fin de valor: "+charFinValor);
System.out.println("Pos de Caracter de fin de cadena: "+charFinCadena);
if (charFinCadena > 0) {
nombreDispositivo = recDataString.substring(charInicioCadena + 1, charInicioTiempo);
switch (nombreDispositivo) {
case "ecg":
segundo = recDataString.substring(charInicioTiempo + 1, charInicioValor);
valorECG = recDataString.substring(charInicioValor + 1, charFinCadena);
System.out.println("Dispositivo: " + nombreDispositivo);
System.out.println("segundo: " + segundo);
System.out.println("Valor ECG: " + valorECG);
Globals.lstSendSegundoECG.add(segundo+";"+valorECG);
System.out.println("Valor de la lista: " + Globals.lstSendSegundoECG);
break;
default:
break;
}
}
break;
Esta es la parte donde envío los datos que llegan del bluetooth a la actividad principal:
while (mState == STATE_CONNECTED) {
try {
String outputText="";
byte[] buffer = new byte[256];
bytes = mmInStream.read(buffer);
mDataParse.Add(buffer, bytes);
String readMessage = new String(buffer, 0, bytes);
System.out.println("Datos del Arduino: "+readMessage);
mHandler.obtainMessage(CONST.MSG_LEER,bytes,-1,readMessage).sendToTarget();//lindonde se envian los datos a la actividad principal
Esta es una salida del debug de la consola de android:
12/29 17:41:03: Launching app
$ adb push C:\Users\Javier\AndroidStudioProjects\BotonMedicoAmunet\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.botonmedico.amunet
$ adb shell pm install -r "/data/local/tmp/com.botonmedico.amunet"
pkg: /data/local/tmp/com.botonmedico.amunet
Success
$ adb shell am start -n "com.botonmedico.amunet/com.botonmedico.amunet.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Connecting to com.botonmedico.amunet
W/ActivityThread: Application com.botonmedico.amunet is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/art: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8668', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1381)
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
D/Atlas: Validating map...
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016_msm8226_LA.BF.1.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.00
Build Date: 02/11/15 Wed
Local Branch:
Remote Branch: quic/LA.BF.1.1.1_rb1.10
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.1_RB1.05.00.02.042.016 + 62ca4eb + acd831d + 9f8b442 + e027a02 + cba30ba + 53c303a + a649d79 + 23e16f8 + 5e97da7 + cbd2a44 + 33d072a + 7aacf06 + 72b33e7 + 28f6f60 + b4c13d8 + NOTHING
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
I/System.out: Entrando al onSuccess
I/BluetoothUtils: -----startDiscovery----
I/BluetoothUtils: Get BT device name:BotonMedicoBT mac:00:07:80:B3:96:4C
D/BluetoothChatService: connect to: 00:07:80:B3:96:4C
D/BluetoothChatService: setState() 0 -> 2
I/BluetoothChatService: BEGIN mConnectThread SocketType:Secure
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
I/BluetoothUtils: -----finishDiscovery----
D/BluetoothChatService: connected, Socket Type:Secure
D/BluetoothChatService: create ConnectedThread: Secure
I/System.out: Bluet: { when=-4h10m40s40ms what=1 target=com.botonmedico.amunet.MainActivity$1 }
D/BluetoothChatService: setState() 2 -> 3
I/BluetoothChatService: BEGIN mConnectedThread
I/System.out: Datos del Arduino: #ecg{2
I/System.out: Pos de Caracter de Inicio de cadena: 0
I/System.out: Pos de Caracter de Inicio de tiempo: 4
I/System.out: Pos de Caracter de Inicio de valor: -1
I/System.out: Pos de Caracter de fin de cadena: -1
I/System.out: Datos del Arduino: 0144(2.26~#ecg{20164(2.62~#ecg{20164(3.25~#ecg{20185(1.76~#ecg{20185(3.38~
I/System.out: Pos de Caracter de Inicio de cadena: 10
I/System.out: Pos de Caracter de Inicio de tiempo: 14
I/System.out: Pos de Caracter de Inicio de valor: 4
I/System.out: Pos de Caracter de fin de cadena: 9
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.botonmedico.amunet, PID: 17532
java.lang.StringIndexOutOfBoundsException: length=74; regionStart=15; regionLength=-11
at java.lang.AbstractStringBuilder.startEndAndLength(AbstractStringBuilder.java:211)
at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:616)
at java.lang.StringBuilder.substring(StringBuilder.java:44)
at com.botonmedico.amunet.MainActivity$1.handleMessage(MainActivity.java:274)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
I/System.out: Datos del Arduino: #ecg{20205(2.81~#ecg{20205(
I/System.out: Datos del Arduino: 2.50~#ecg{20226(3.36~#ecg{20226(1.79~#ecg{20246(3.16~#ecg{20246(2.89~#ecg{20267(2.02~#ecg{202
I/System.out: Datos del Arduino: 67(3.38~#ecg{20286(2.17~#ecg{20286(3.05~#ecg{20306(3.19~#
I/System.out: Datos del Arduino: ecg{20306(1.93~#ecg{20327(3.37~#ecg{20327(2.30~#ecg{20347(2.58~#ecg{
I/System.out: Datos del Arduino: 20347(3.27~#ecg{20368(1.76~#ecg{20368(3.40~#ecg{20388(2.83~#ecg{20388(2.47~#ecg{20409(3.37~
I/System.out: Datos del Arduino: #ecg{20409(1.81~#ecg{20429(3.16~#ecg{20429(2.94~#ecg{20449(2.01~#ecg{
I/System.out: Datos del Arduino: 20449(3.37~#ecg{20469(2.30~#ecg{20469(3.04~#ecg{20490(3.25~#ecg{20490
I/System.out: Datos del Arduino: (1.94~#ecg{20510(3.40~
I/System.out: Datos del Arduino: #ecg{20510(2.33~#ecg{20531(2.57~#ecg{20531(3.26~#ecg{20551(1.77~#ecg{20551(3.37~#ecg{20572(2.84~#ecg{20572(2.39~#ecg{20592(3.
I/System.out: Datos del Arduino: 33~#ecg{20592(1.85~#ecg{20612(3.09~#ecg{20612(2.96~#ecg{20632(1.96~#e
I/System.out: Datos del Arduino: cg{20632(3.44~#ecg{20653(2.24~#ecg{20653(2.96~
I/System.out: Datos del Arduino: #ecg{20673(3.23~#ecg{20673(1.89~#ecg{20694(3.37~#ecg{20694(2.41~#ecg{20714(2.47~#
I/System.out: Datos del Arduino: ecg{20714(3.28~#ecg{20734(1.79~#ecg{20734(3.37~
I/System.out: Datos del Arduino: #ecg{20754(2.90~#ecg{20754(2.47~#ecg{20774(3.36~#ecg{20774
I/System.out: Datos del Arduino: (1.85~#ecg{20795(3.11~#ecg{20795(3.01~#ecg{20815(1.96~#
I/System.out: Datos del Arduino: ecg{20815(3.37~#ecg{20836(2.29~#ecg{20836(2.96~#ecg{20856(3.25~#ecg{20856(1.86~#ecg{20877(3.39~#
I/System.out: Datos del Arduino: ecg{20877(2.42~#ecg{20897(2.49~#ecg{20897(3.29~#ecg{20917(1.81~#ecg{209
I/System.out: Datos del Arduino: 17(3.40~#ecg{20937(2.92~#ecg{20937(2.36~#ecg{20958(3.39~#ecg{20958(1.86~#ecg
I/System.out: Datos del Arduino: {20978(3.03~#ecg{20978(2.49~#ecg{20999(1.49~#ecg{20999(3.25~
I/System.out: Datos del Arduino: #ecg{21019(2.31~#ecg{21019(3.11~#ecg{21040(3.27~#ecg{21040(1.97~#ecg{21060(3
I/System.out: Datos del Arduino: .49~#ecg{21060(2.48~#ecg{21080(2.59~#
I/System.out: Datos del Arduino: ecg{21080(3.28~#ecg{21100(1.87~#ecg{21100(3.48~#ecg{21121(2.94~#
I/System.out: Datos del Arduino: ecg{21121(2.47~#ecg{21141(3.38~#ecg{21141(1.95~#ecg{21161(3.20~#ecg{21161(3.01~#ecg
I/System.out: Datos del Arduino: {21182(2.02~#ecg{21182(3.48~#ecg{21202(2.37~#ecg{21202(3.09~#ecg{21223(3.26~#
I/System.out: Datos del Arduino: ecg{21223(1.92~#ecg{21242(3.48~#ecg{21242(2.52~#
I/System.out: Datos del Arduino: ecg{21263(2.59~#ecg{21263(3.31~#ecg{21283(1.92~#ecg{21283(3.48~#ecg{21304(2.99~
I/System.out: Datos del Arduino: #ecg{21304(2.41~#ecg{21324(3.37~#ecg{21324(1.98~#ecg{21345(3.14~#ecg{21345(3.05~#ecg{21365(1.98~#
I/System.out: Datos del Arduino: ecg{21365(3.40~#ecg{21386(2.42~#ecg{21386(3.00~#ecg{21405(3.29~#
I/System.out: Datos del Arduino: ecg{21405(1.89~#ecg{21426(3.47~#ecg{21426(2.55~#ecg{21446(2.49~#ecg{21446(3.33~#
I/System.out: Datos del Arduino: ecg{21467(1.92~#ecg{21467(3.48~#ecg{21487(3.00~#ecg{21487(2.43~#ecg{21508(3.39~#ecg{21508(1.9
I/System.out: Datos del Arduino: 8~#ecg{21528(3.11~#ecg{21528(3.12~#ecg{21549(1.96~#ecg
I/System.out: Datos del Arduino: {21549(3.42~#ecg{21569(2.49~#ecg{21569(2.97~
I/System.out: Datos del Arduino: #ecg{21588(3.32~#ecg{21588(1.87~#ecg{21609(3.47~#ecg{21609(2.61~#ecg{21629(2.47~#ecg{21629(3.34~#ecg{21650(1.96~#ecg{2
I/System.out: Datos del Arduino: 1650(3.46~#ecg{21670(3.03~#ecg{21670(2.36~#ecg{21691(3.40~#e
I/System.out: Datos del Arduino: cg{21691(2.04~#ecg{21711(3.04~
I/System.out: Datos del Arduino: #ecg{21711(3.10~#ecg{21731(1.90~#ecg{21731(3.37~#ecg{21751(2.50~#ecg{21751(2.91~#ecg{21772
I/System.out: Datos del Arduino: (3.29~#ecg{21772(1.85~#ecg{21792(3.45~#ecg{21792(2.68~#ecg{21813(2.40~#ecg
I/System.out: Datos del Arduino: {21813(3.45~#ecg{21833(2.02~#ecg{21833(3.25~#ecg{21854(2.98~#ecg{
I/System.out: Datos del Arduino: 21854(2.28~#ecg{21874(3.39~#ecg{218
I/System.out: Datos del Arduino: 74(2.06~#ecg{21894(2.99~#ecg{21894(3.17~#ecg{21914(1.89~#ecg{21914(3.41~#ecg{21935(2.57~#ecg{219
I/System.out: Datos del Arduino: 35(2.90~#ecg{21955(3.32~#ecg{21955(1.87~#ecg{21976(3.34~#
I/System.out: Datos del Arduino: ecg{21976(2.72~#ecg{21996(1.72~#ecg{21996(3.10~#ecg{22017(1.95~
I/System.out: Datos del Arduino: #ecg{22017(3.09~#ecg{22037(2.28~#ecg{22037(1.12~
I/System.out: Datos del Arduino: #ecg{22056(3.24~#ecg{22056(2.07~#ecg{22077(2.83~#ecg{22077(3.19~#ecg{22097(1.77~#
I/System.out: Datos del Arduino: ecg{22097(3.39~#ecg{22118(2.52~#ecg{22118(2.83~#ecg{22138(3.32~#ecg{22
I/System.out: Datos del Arduino: 138(1.88~#ecg{22159(3.43~#ecg{22159(2.71~#ecg{22179(
I/System.out: Datos del Arduino: 2.34~#ecg{22179(3.32~#ecg{22200(2.02~#ecg{22200(3.36~#ecg{22219(3.10~#ecg{22219(2.25~
I/System.out: Datos del Arduino: #ecg{22240(3.40~#ecg{22240(2.16~#ecg{22260(2.91~#ecg{22260(3.17~#ec
I/System.out: Datos del Arduino: g{22281(1.85~#ecg{22281(3.44~#ecg{22301(2.63~
I/Process: Sending signal. PID: 17532 SIG: 9
Disconnected from the target VM, address: 'localhost:8668', transport: 'socket'
Espero en verdad que me puedan ayudar con mi problema, de antemano gracias.
<br>
, ¿qué es realmente lo que recibes?