Android Bluetooth поток данных в реальном времени медленный - PullRequest
1 голос
/ 19 января 2020

У меня есть модуль ЭКГ, подключенный к Arduino Uno, и модуль Bluetooth H C -06, который измеряет сигнал ЭКГ с частотой 100 Гц и отправляет данные в приложение Android каждые 10 мс с 6 байтами данных. Но я не могу получить 100 образцов в секунду.

Вот мой код Arduino:

#include <SoftwareSerial.h>
SoftwareSerial myBT(2, 3);
void setup() {
  myBT.begin(115200);
  Serial.begin(115200);
  pinMode(A0, OUTPUT);
}
char cmd;
int ECG;
void loop() 
{
  if(myBT.available()>0)
  {
    cmd = myBT.read();

    while(cmd = 'r')
    {
    ECG = ((ECG+1)%1023);
    float Volt = (float)ECG*5.0/1023.0;
    myBT.print("s"+String(Volt,2));
    delay(10);
    }
  }
}

Вот мой код Android Java:

package com.example.mscale;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.icu.text.UnicodeSetSpanner;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.series.Series;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;


public class MonitorActivity extends AppCompatActivity {
    private final String TAG = "MonitorActivity";
    //Stuffs
    GraphView graphx;
    Button recordButton;
    TextView statusBtTxt, KilogramTxt;
    //Bluetooth Stuffs
    private static final int REQUEST_ENABLE_BT = 1;
    BluetoothAdapter bluetoothAdapter;
    private ArrayList<String> mDeviceList;
    private String pairedAddres;
    ArrayAdapter<String> adapter;
    private BluetoothSocket mBluetoothSocket;
    public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    protected static final int SUCESS_CONNECT = 0;
    protected static final int MESSAGE_READ = 1;
    protected static final int MESSAGE_WRITE = 2;
    private boolean recording = false;

   /* @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (Bluetooth.connectedThread != null) {
            Bluetooth.connectedThread.write("Q");}//Stop streaming
        super.onBackPressed();
    }*/

    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUCESS_CONNECT:
                    statusBtTxt.setText("Connected");
                    recording = true;
                    break;
                case MESSAGE_READ:
                    byte[] readBuf = (byte[]) msg.obj;
                    String strIncom = new String(readBuf, 0, 5);                 // create string from bytes array
                    statusBtTxt.setText(strIncom);
                    Log.d(TAG, strIncom);
                    /*if (strIncom.indexOf("s") == 3) {
                        Log.d(TAG, strIncom);
                        strIncom = strIncom.replace("s", "");
                        if(isFloatNumber(strIncom)){
                        }
                    }else Log.d(TAG, "The s is not where it should be!");*/
                    break;
                case MESSAGE_WRITE:
                    ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
                    connectedThread.write("r");
                    break;
            }
        }
    };

    public boolean isFloatNumber(String num) {
        //Log.d("checkfloatNum", num);
        try {
            Double.parseDouble(num);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideStatusbar();
        setContentView(R.layout.activity_monitor);
        findIDStuff();
        plotting();
        System.gc();
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recording == false) {
                    bluetoothManagement();
                } else {
                    ConnectedThread connectedThread = new ConnectedThread(mBluetoothSocket);
                    connectedThread.write("r");
                    connectedThread.start();
                }
            }
        });
    }

    //Find ID of Stuffs
    private void findIDStuff() {
        recordButton = findViewById(R.id.recordBtn);
        //graphx = (GraphView) findViewById(R.id.graph);
        statusBtTxt = findViewById(R.id.btStatusTxt);
        KilogramTxt = findViewById(R.id.kiloTxt);
    }

    //Bluetooth management
    private void bluetoothManagement() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        } else {
            //Do sth
            pairedBtDevices();
        }
    }

    //Graph plotting
    private void plotting() {


    }

    //Hide status bar
    private void hideStatusbar() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    //Paired Bluetooth device
    private void pairedBtDevices() {
        // Register for broadcasts when a device is discovered.
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(receiver, filter);
        Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                String module = "ECG_Module";
                if (device.getName().equals(module)) {
                    pairedAddres = device.getAddress();
                }
            }
            bluetoothAdapter.startDiscovery();
            //Log.d(TAG, String.valueOf(mPairedDeviceList));
        } else {
            statusBtTxt.setText("No paired");
        }
    }

    // Create a BroadcastReceiver for ACTION_FOUND.
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
            mDeviceList = new ArrayList<String>();
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Discovery has found a device. Get the BluetoothDevice
                // object and its info from the Intent.
                String module = "ECG_Module";
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                if (device.getName() != null && device.getName().equalsIgnoreCase(module) && device.getAddress() != null && device.getAddress().equalsIgnoreCase(pairedAddres)) {
                    mDeviceList.add(device.getName() + "\n" + device.getAddress());
                    bluetoothAdapter.cancelDiscovery();
                    open_dialog();
                }
            } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
                statusBtTxt.setText("Start Discovery...");
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                statusBtTxt.setText("End Discovery....");
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }

    //Open dialog
    public void open_dialog() {

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        final View row = getLayoutInflater().inflate(R.layout.rowitem, null);
        ListView mlistView = (ListView) row.findViewById(R.id.listviewBt);
        final Button mBtnConnect = (Button) row.findViewById(R.id.conBtn);
        Button mBtnCancel = (Button) row.findViewById(R.id.canBtn);

        mlistView.setClickable(true);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mDeviceList);
        mlistView.setAdapter(adapter);
        alertDialog.setView(row);
        final AlertDialog dialog = alertDialog.create();
        dialog.show();

        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String mNames = (String) parent.getItemAtPosition(position);
                final String[] mMacAddress = mNames.split("\\r?\\n");

                mBtnConnect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(mMacAddress[1]);
                            ConnectThread connectThread = new ConnectThread(bluetoothDevice);
                            connectThread.start();
                            Context context = getApplicationContext();
                            Toast toast = Toast.makeText(context, "Connnecting to " + mMacAddress[1], Toast.LENGTH_SHORT);
                            toast.show();
                            dialog.hide();
                        } catch (Exception e) {
                            Context context = getApplicationContext();
                            Toast toast = Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT);
                            toast.show();
                            return;
                        }
                    }
                });
            }
        });

        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.hide();
            }
        });
    }

    //Connection Bluetooth
    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            // Use a temporary object that is later assigned to mmSocket
            // because mmSocket is final.
            BluetoothSocket tmp = null;
            mmDevice = device;

            try {
                // Get a BluetoothSocket to connect with the given BluetoothDevice.
                // MY_UUID is the app's UUID string, also used in the server code.
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "Socket's create() method failed", e);
            }
            mBluetoothSocket = tmp;
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it otherwise slows down the connection.
            bluetoothAdapter.cancelDiscovery();
            try {
                // Connect to the remote device through the socket. This call blocks
                // until it succeeds or throws an exception.
                mmSocket.connect();
            } catch (IOException connectException) {
                // Unable to connect; close the socket and return.
                try {
                    mmSocket.close();
                    Log.d(TAG, "Failed connection");
                } catch (IOException closeException) {
                    Log.e(TAG, "Could not close the client socket", closeException);
                }
                return;
            }

            // The connection attempt succeeded. Perform work associated with
            // the connection in a separate thread.
            mHandler.obtainMessage(SUCESS_CONNECT, mmSocket).sendToTarget();
        }

        // Closes the client socket and causes the thread to finish.
        public void cancel() {
            try {
                statusBtTxt.setText("Failed Connection");
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "Could not close the client socket", e);
            }
        }
    }


    class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the input and output streams, using temp objects because
            // member streams are final
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        StringBuffer sbb = new StringBuffer();

        public void run() {
            // Keep listening to the InputStream until an exception occurs
            byte[] buffer;
            int bytes = 0;
            try {
                while(mmInStream.read() != (byte) 's')
                {}
                //Since we already read out the "s", we need to read 4 more bytes, to regain alignment.
                mmInStream.read();
                mmInStream.read();
                mmInStream.read();
                mmInStream.read();
                //Now, the next byte read should be an "s".
            } catch (IOException e) {
                return;
            }
            while (true) {
                try {
                    buffer = new byte[1024];
                    // Read from the InputStream
                    int totalRead = 0;
                    while(totalRead < 5)
                    {
                        bytes = mmInStream.read(buffer,totalRead,5-totalRead);
                        if(bytes==-1)
                            throw new IOException("EOS reached");
                        totalRead += bytes;
                    }
                    //Log.d("Obtain: ", String.valueOf(totalRead));
                    mHandler.obtainMessage(MESSAGE_READ, totalRead, -1, buffer).sendToTarget();
                } catch (IOException e) {
                    break;
                }
            }
        }
            /* Call this from the main activity to send data to the remote device */
            public void write (String income){

                try {
                    mmOutStream.write(income.getBytes());
                } catch (IOException e) {
                }
            }

            /* Call this from the main activity to shutdown the connection */
            public void cancel () {
                try {
                    mmSocket.close();
                } catch (IOException e) {
                }
            }
        }
    }

Передаваемое переменное значение и Logcat в Android полученные данные (без myBT.print ("\ n") и входа датчика):

2020-01-22 13:12:22.369 28250-28250/com.example.mscale D/MonitorActivity: s0.01
2020-01-22 13:12:22.401 28250-28250/com.example.mscale D/MonitorActivity: s0.01
2020-01-22 13:12:22.421 28250-28250/com.example.mscale D/MonitorActivity: s0.02
2020-01-22 13:12:22.421 28250-28250/com.example.mscale D/MonitorActivity: s0.02
2020-01-22 13:12:22.422 28250-28250/com.example.mscale D/MonitorActivity: s0.03
2020-01-22 13:12:22.444 28250-28250/com.example.mscale D/MonitorActivity: s0.03
2020-01-22 13:12:22.454 28250-28250/com.example.mscale D/MonitorActivity: s0.04
2020-01-22 13:12:22.454 28250-28250/com.example.mscale D/MonitorActivity: s0.04
2020-01-22 13:12:22.454 28250-28250/com.example.mscale D/MonitorActivity: s0.05
2020-01-22 13:12:22.478 28250-28250/com.example.mscale D/MonitorActivity: s0.05
2020-01-22 13:12:22.488 28250-28250/com.example.mscale D/MonitorActivity: s0.06
2020-01-22 13:12:22.488 28250-28250/com.example.mscale D/MonitorActivity: s0.06
2020-01-22 13:12:22.514 28250-28250/com.example.mscale D/MonitorActivity: s0.07
2020-01-22 13:12:22.522 28250-28250/com.example.mscale D/MonitorActivity: s0.07
2020-01-22 13:12:22.522 28250-28250/com.example.mscale D/MonitorActivity: s0.08
2020-01-22 13:12:22.548 28250-28250/com.example.mscale D/MonitorActivity: s0.08
2020-01-22 13:12:22.555 28250-28250/com.example.mscale D/MonitorActivity: s0.09
2020-01-22 13:12:22.555 28250-28250/com.example.mscale D/MonitorActivity: s0.09
2020-01-22 13:12:22.572 28250-28250/com.example.mscale D/MonitorActivity: s0.10
2020-01-22 13:12:22.589 28250-28250/com.example.mscale D/MonitorActivity: s0.10
2020-01-22 13:12:22.589 28250-28250/com.example.mscale D/MonitorActivity: s0.11
2020-01-22 13:12:22.606 28250-28250/com.example.mscale D/MonitorActivity: s0.11
2020-01-22 13:12:22.623 28250-28250/com.example.mscale D/MonitorActivity: s0.12
2020-01-22 13:12:22.640 28250-28250/com.example.mscale D/MonitorActivity: s0.12
2020-01-22 13:12:22.641 28250-28250/com.example.mscale D/MonitorActivity: s0.13
2020-01-22 13:12:22.659 28250-28250/com.example.mscale D/MonitorActivity: s0.13
2020-01-22 13:12:22.673 28250-28250/com.example.mscale D/MonitorActivity: s0.14
2020-01-22 13:12:22.673 28250-28250/com.example.mscale D/MonitorActivity: s0.14
2020-01-22 13:12:22.674 28250-28250/com.example.mscale D/MonitorActivity: s0.15
2020-01-22 13:12:22.700 28250-28250/com.example.mscale D/MonitorActivity: s0.15
2020-01-22 13:12:22.707 28250-28250/com.example.mscale D/MonitorActivity: s0.16
2020-01-22 13:12:22.707 28250-28250/com.example.mscale D/MonitorActivity: s0.16
2020-01-22 13:12:22.724 28250-28250/com.example.mscale D/MonitorActivity: s0.17
2020-01-22 13:12:22.724 28250-28250/com.example.mscale D/MonitorActivity: s0.17
2020-01-22 13:12:22.743 28250-28250/com.example.mscale D/MonitorActivity: s0.18
2020-01-22 13:12:22.758 28250-28250/com.example.mscale D/MonitorActivity: s0.18
2020-01-22 13:12:22.758 28250-28250/com.example.mscale D/MonitorActivity: s0.19
2020-01-22 13:12:22.778 28250-28250/com.example.mscale D/MonitorActivity: s0.19
2020-01-22 13:12:22.793 28250-28250/com.example.mscale D/MonitorActivity: s0.20
2020-01-22 13:12:22.794 28250-28250/com.example.mscale D/MonitorActivity: s0.20
2020-01-22 13:12:22.815 28250-28250/com.example.mscale D/MonitorActivity: s0.21
2020-01-22 13:12:22.828 28250-28250/com.example.mscale D/MonitorActivity: s0.21
2020-01-22 13:12:22.828 28250-28250/com.example.mscale D/MonitorActivity: s0.22
2020-01-22 13:12:22.829 28250-28250/com.example.mscale D/MonitorActivity: s0.22
2020-01-22 13:12:22.858 28250-28250/com.example.mscale D/MonitorActivity: s0.22
2020-01-22 13:12:22.877 28250-28250/com.example.mscale D/MonitorActivity: s0.23
2020-01-22 13:12:22.878 28250-28250/com.example.mscale D/MonitorActivity: s0.23
2020-01-22 13:12:22.878 28250-28250/com.example.mscale D/MonitorActivity: s0.24
2020-01-22 13:12:22.897 28250-28250/com.example.mscale D/MonitorActivity: s0.24
2020-01-22 13:12:22.911 28250-28250/com.example.mscale D/MonitorActivity: s0.25
2020-01-22 13:12:22.912 28250-28250/com.example.mscale D/MonitorActivity: s0.25
2020-01-22 13:12:22.929 28250-28250/com.example.mscale D/MonitorActivity: s0.26
2020-01-22 13:12:22.929 28250-28250/com.example.mscale D/MonitorActivity: s0.26
2020-01-22 13:12:22.945 28250-28250/com.example.mscale D/MonitorActivity: s0.27
2020-01-22 13:12:22.964 28250-28250/com.example.mscale D/MonitorActivity: s0.27
2020-01-22 13:12:22.965 28250-28250/com.example.mscale D/MonitorActivity: s0.28
2020-01-22 13:12:22.980 28250-28250/com.example.mscale D/MonitorActivity: s0.28
2020-01-22 13:12:22.998 28250-28250/com.example.mscale D/MonitorActivity: s0.29
2020-01-22 13:12:23.013 28250-28250/com.example.mscale D/MonitorActivity: s0.29
2020-01-22 13:12:23.013 28250-28250/com.example.mscale D/MonitorActivity: s0.30
2020-01-22 13:12:23.033 28250-28250/com.example.mscale D/MonitorActivity: s0.30
2020-01-22 13:12:23.046 28250-28250/com.example.mscale D/MonitorActivity: s0.31
2020-01-22 13:12:23.046 28250-28250/com.example.mscale D/MonitorActivity: s0.31
2020-01-22 13:12:23.047 28250-28250/com.example.mscale D/MonitorActivity: s0.32
2020-01-22 13:12:23.069 28250-28250/com.example.mscale D/MonitorActivity: s0.32
2020-01-22 13:12:23.079 28250-28250/com.example.mscale D/MonitorActivity: s0.33
2020-01-22 13:12:23.080 28250-28250/com.example.mscale D/MonitorActivity: s0.33
2020-01-22 13:12:23.104 28250-28250/com.example.mscale D/MonitorActivity: s0.34
2020-01-22 13:12:23.115 28250-28250/com.example.mscale D/MonitorActivity: s0.34
2020-01-22 13:12:23.115 28250-28250/com.example.mscale D/MonitorActivity: s0.35
2020-01-22 13:12:23.116 28250-28250/com.example.mscale D/MonitorActivity: s0.35
2020-01-22 13:12:23.139 28250-28250/com.example.mscale D/MonitorActivity: s0.36
2020-01-22 13:12:23.149 28250-28250/com.example.mscale D/MonitorActivity: s0.36
2020-01-22 13:12:23.149 28250-28250/com.example.mscale D/MonitorActivity: s0.37
2020-01-22 13:12:23.178 28250-28250/com.example.mscale D/MonitorActivity: s0.37
2020-01-22 13:12:23.198 28250-28250/com.example.mscale D/MonitorActivity: s0.38
2020-01-22 13:12:23.198 28250-28250/com.example.mscale D/MonitorActivity: s0.38
2020-01-22 13:12:23.215 28250-28250/com.example.mscale D/MonitorActivity: s0.39
2020-01-22 13:12:23.216 28250-28250/com.example.mscale D/MonitorActivity: s0.39
2020-01-22 13:12:23.216 28250-28250/com.example.mscale D/MonitorActivity: s0.40
2020-01-22 13:12:23.235 28250-28250/com.example.mscale D/MonitorActivity: s0.40
2020-01-22 13:12:23.248 28250-28250/com.example.mscale D/MonitorActivity: s0.41
2020-01-22 13:12:23.249 28250-28250/com.example.mscale D/MonitorActivity: s0.41
2020-01-22 13:12:23.266 28250-28250/com.example.mscale D/MonitorActivity: s0.42
2020-01-22 13:12:23.284 28250-28250/com.example.mscale D/MonitorActivity: s0.42
2020-01-22 13:12:23.299 28250-28250/com.example.mscale D/MonitorActivity: s0.43
2020-01-22 13:12:23.299 28250-28250/com.example.mscale D/MonitorActivity: s0.43
2020-01-22 13:12:23.319 28250-28250/com.example.mscale D/MonitorActivity: s0.43
2020-01-22 13:12:23.336 28250-28250/com.example.mscale D/MonitorActivity: s0.44
2020-01-22 13:12:23.336 28250-28250/com.example.mscale D/MonitorActivity: s0.44
2020-01-22 13:12:23.355 28250-28250/com.example.mscale D/MonitorActivity: s0.45
2020-01-22 13:12:23.367 28250-28250/com.example.mscale D/MonitorActivity: s0.45
2020-01-22 13:12:23.386 28250-28250/com.example.mscale D/MonitorActivity: s0.46
2020-01-22 13:12:23.386 28250-28250/com.example.mscale D/MonitorActivity: s0.46
2020-01-22 13:12:23.387 28250-28250/com.example.mscale D/MonitorActivity: s0.47
2020-01-22 13:12:23.387 28250-28250/com.example.mscale D/MonitorActivity: s0.47
2020-01-22 13:12:23.417 28250-28250/com.example.mscale D/MonitorActivity: s0.48
2020-01-22 13:12:23.435 28250-28250/com.example.mscale D/MonitorActivity: s0.48
2020-01-22 13:12:23.436 28250-28250/com.example.mscale D/MonitorActivity: s0.49
2020-01-22 13:12:23.436 28250-28250/com.example.mscale D/MonitorActivity: s0.49
2020-01-22 13:12:23.457 28250-28250/com.example.mscale D/MonitorActivity: s0.50
2020-01-22 13:12:23.474 28250-28250/com.example.mscale D/MonitorActivity: s0.50
2020-01-22 13:12:23.474 28250-28250/com.example.mscale D/MonitorActivity: s0.51
2020-01-22 13:12:23.474 28250-28250/com.example.mscale D/MonitorActivity: s0.51
2020-01-22 13:12:23.501 28250-28250/com.example.mscale D/MonitorActivity: s0.52
2020-01-22 13:12:23.501 28250-28250/com.example.mscale D/MonitorActivity: s0.52
2020-01-22 13:12:23.519 28250-28250/com.example.mscale D/MonitorActivity: s0.53
2020-01-22 13:12:23.540 28250-28250/com.example.mscale D/MonitorActivity: s0.53
2020-01-22 13:12:23.541 28250-28250/com.example.mscale D/MonitorActivity: s0.54
2020-01-22 13:12:23.541 28250-28250/com.example.mscale D/MonitorActivity: s0.54
2020-01-22 13:12:23.553 28250-28250/com.example.mscale D/MonitorActivity: s0.55
2020-01-22 13:12:23.573 28250-28250/com.example.mscale D/MonitorActivity: s0.55
2020-01-22 13:12:23.587 28250-28250/com.example.mscale D/MonitorActivity: s0.56
2020-01-22 13:12:23.587 28250-28250/com.example.mscale D/MonitorActivity: s0.56

1 Ответ

0 голосов
/ 20 января 2020

Ваш модуль Bluetooth, вероятно, в порядке. Проблемы с производительностью, которые вы видите, более вероятны из-за реализации вашего протокола: А именно, символы s находятся не там, где вы ожидаете их видеть. Для этого есть несколько причин, поэтому давайте go пройдем через них один за другим:

Значение ЭКГ

Обратите внимание, что возвращаемое значение analogRead() в диапазоне от 0 до 1023 ( документы ), что соответствует диапазону от 0 В до Vref. Если предположить, что Vref равно 5 В, то выходной сигнал 3,3 В на ЭКГ приведет к тому, что analogRead() вернет 675. Таким образом, на практике ECG, вероятно, колеблется от 0 до 675. Вот почему у вас есть эта логика масштабирования c:

float Volt = (float)ECG*5.0/1023.0;

, которая преобразует показания AD C обратно в напряжение.

Точность логики масштабирования c

Если ECG равно 5, то Volt будет 0.024437929. Это противоречит вашему предположению, что Volt всегда является четырехзначным числом с плавающей запятой, и это приводит к смещению ваших s символов.

Проблемы Logcat

  • Пропущенные сообщения: это не из-за сбоя в вашем BT-соединении. Это потому, что вы получаете больше сообщений в один буфер, чем планировали. mmInStream.read(buffer) попытается прочитать до 1024 байтов; это означает, что он может захватить несколько сообщений. Но если вы получаете несколько сообщений в своем буфере, вы обрабатываете только первое.

  • Поврежденные сообщения: например, s0.����. Это связано с тем, что mmInStream.read(buffer) читает только 3 байта, но вы предполагали, что он прочитал 5. Видите ли, read() возвращает значение, как только у него есть хотя бы 1 новый доступный байт. Это не значит, что он прочитал все , что вы хотели. В этом случае последние 2 байта были унифицированы (0), но вы все равно пытались преобразовать их в String.

  • Смещенные сообщения: по той же причине root как испорченные сообщения. Обратите внимание, что следующая строка в logcat - 15s0.. Это потому, что 15-е сообщение (s0.15) разбилось прямо посередине. Затем часть 16-го сообщения (s0.) была добавлена ​​в конец, потому что теперь ваше выравнивание отключено. Перестройка была восстановлена ​​только (на следующей строке) из-за причуд в синхронизации сокетов.

Решение всех 3 из этих проблем состоит в том, чтобы убедиться, что read() блокируется до тех пор, пока читает ровно 5 байтов (см. «Fix # 2»).

Fix # 1

Принудительно все преобразования строк приводят к получению ровно 4 символов ( docs ):

myBT.print("s"+String(Volt,2));   //2 decimal places works out to 4 characters total, for your input range.

Fix # 2

Убедитесь, что все чтения BT-сокета имеют длину ровно 5 байтов.

buffer = new byte[1024];
// Read from the InputStream
int totalRead = 0;
while(totalRead < 5)
{
    bytes = mmInStream.read(buffer,totalRead,5-totalRead);
    if(bytes==-1)
        throw new IOException("EOS reached");
    totalRead += bytes;
}

В качестве альтернативы, Вы можете использовать функцию, разработанную для этой цели, например DataInputStream.readFully () .

Fix # 3

По причинам, которые не являются Совершенно ясно, что отправленные сообщения имеют фиксированное смещение относительно полученных сообщений. Это может быть из-за проблемы с начальными условиями; Ваш Arduino может отправлять данные в модуль BT, не зная, подключен он или нет. Таким образом, в тот момент, когда соединение BT фактически происходит, оно начинает посылать в середине сообщения, а не в начале. Чтобы устранить эту проблему и восстановить выравнивание, я предлагаю использовать некоторый код, подобный следующему:

// Keep listening to the InputStream until an exception occurs
byte[] buffer;
int bytes = 0;
try {
    while(mmInStream.read() != (byte) 's')    //TODO: Handle "-1" case
    {}
    //Since we already read out the "s", we need to read 4 more bytes, to regain alignment.
    mmInStream.read();
    mmInStream.read();
    mmInStream.read();
    mmInStream.read();
    //Now, the next byte read should be an "s".
} catch (IOException e) {
    return;
}
while (true) {
(...)

После того, как эти исправления будут реализованы, вы можете избавиться от sleep() в вашем приеме l oop, потому что read() logi c блоков до получения необходимых данных . Кроме того, вместо обработки сообщений, только если первый байт равен s, вы должны закрыть соединение , если это условие не выполняется (поскольку это указывает на то, что ваш партнер работает неправильно).

Примечание: я не проверял этот код, но он должен работать. Чтобы избежать подобных проблем в будущем, добавьте logi c, чтобы проверить больше ваших предположений . Например, на Arduino вы можете включить светодиод if(strlen(String(Volt))!=4). И если s не там, где вы ожидаете, не игнорируйте проблему; выяснить почему .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...