Bluetooth update UI thread

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
HereturbiesWalt
Level 1
Level 1
5 sign-ins 5 questions asked First like given

Hi

I am writing an embedded application for Infineon XMC4500 microprocessor. I read data from different sensors and I send this data by means of bluetooth module in order to visualize them on the screen of Android smarphone. I use GraphView library.

I implemented BluetoothClass according to Android tutorial. If I get data from single sensor then everything is ok. But when I get data from multiple sensors unfortunately my application does not work smoothly.

Of course I am doing Bluetooth connection in seperate thread and I try to update UI in Handler. Tell me please what am I doing wrong. Of course I send data by means of JSON exchange format:

I send something like this from the microcontroller side:

  sprintf(json_data, "{"
                            "\"m\":"
                            "["
                                    "{"
                                        "\"id\":a,"
                                        "\"x\":%.2f,"
                                        "\"y\":%.2f,"
                                        "\"z\":%.2f"
                                    "},"
                                    "{"
                                        "\"id\":g,"
                                        "\"x\":%.2f,"
                                        "\"y\":%.2f,"
                                        "\"z\":%.2f"
                                    "},"
                                    "{"
                                        "\"id\":m,"
                                        "\"x\":%.2f,"
                                        "\"y\":%.2f,"
                                        "\"z\":%.2f"
                                    "},"
                                    "{"
                                        "\"id\":t,"
                                        "\"x\":%.2f"
                                    "},"
                                    "{"
                                        "\"id\":h,"
                                        "\"x\":%.2f"
                                    "}"
                            "]"
                        "}", getAccelXf(), getAccelYf(), getAccelZf(), getGyroXf(), getGyroYf(), getGyroZf(), getMagnetXf(), getMagnetYf(), getMagnetZf(), readTemperature(), readHumidity());

As an attachment to my request I add pieces of source code:

    public class ConnectedThread extends Thread
{
    BluetoothSocket connectedSocket;
    InputStream inStream;
    OutputStream outStream;

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



        try
        {

            if (connectedSocket != null)
            {
                tmpIn = connectedSocket.getInputStream();
                tmpOut = connectedSocket.getOutputStream();
                mHandler.obtainMessage(Constants.MESSAGE_DEVICE_CONNECTED_SUCCESSFULLY).sendToTarget();
               // Toast.makeText(activityContext, "connectedSocket != null", Toast.LENGTH_LONG);
            }
        }
        catch (IOException e) {
            mHandler.obtainMessage(Constants.MESSAGE_INPUT_OUTPUT_STREAM_UNAVAILABLE).sendToTarget();
            e.printStackTrace();
        }

        inStream = tmpIn;
        outStream = tmpOut;
    }

    public void run()
    {

        if(bluetoothAdapter.isDiscovering()){
            bluetoothAdapter.cancelDiscovery();
        }

        byte[] buffer = new byte[4096];
        int bytes;

        String dupa;

        while (true)
        {
            try {
                if (inStream != null)
                {
                    bytes = inStream.available();

                    if(bytes > 0 && bytes <= 200)
                    {
                        byte[] pocketBytes = new byte[bytes];
                        inStream.read(pocketBytes);
                        //Log.d(TAG, "setState() " + mState + " -> " + state);
                        System.out.println(pocketBytes.toString());
                        dupa = pocketBytes.toString();
                        System.out.println(dupa);

                        mHandler.obtainMessage(MESSAGE_READ, bytes, -1, pocketBytes).sendToTarget();
                    }
                }
            } catch (IOException e) {
                mHandler.obtainMessage(Constants.MESSAGE_REMOTE_DEV_DISCONNECTED).sendToTarget();
                break;
            }

            /*try
            {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            outStream.write(bytes);
        } catch (IOException e) { }
    }

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

}

And after that I try to update UI in the Handler.

It does not work smoothly, morever sometimes it does not respond.

Could you please tell me what am I doing wrong azar echatrandom ?

Thx in advance.

0 Likes
2 Replies
Vasanth
Moderator
Moderator
Moderator
250 sign-ins 500 solutions authored First question asked

Hi,
Is this a BLE UART module ? Could you confirm whether the data in the UART interface is proper ?

Best Regards,
Vasanth R S

0 Likes
ncbs
Moderator
Moderator
Moderator
500 replies posted 50 likes received 250 sign-ins

Hi @HereturbiesWalt ,

The thread was locked due to inactivity for a long time. You can continue the discussion on the topic by opening a new thread with reference to the locked one. The continuous discussion in an inactive thread may mostly be unattended by community users.

Thanks and Regards,
Nikhil

0 Likes