Skip to content Skip to sidebar Skip to footer

Receive Character Using Android Phone From Arduino

I worked this code to receive a single letter of the arduino I can not see any response on the phone text viewer when I want arduino sends the letter 'A' shows me the word 'ON'a

Solution 1:

you are updating textview from a thread, it must be throwing some exception but as you have not printed anything in your catch block you are not getting any output or error or anything, always remember, you cannot update views from any thread other than UI thread.

try {
            int bytesAvailable = btSocket.getInputStream().available();

            byte []packetBytes= newbyte[bytesAvailable];
            if (bytesAvailable > 0) {
                tb.setText(bytesAvailable+ "ok");
                btSocket.getInputStream().read(packetBytes);

                         for(int i=0; i<bytesAvailable;i++)
                         {
                            if (packetBytes[i]==65)
                                 tb.setText("ON");
                             elseif (packetBytes[i] ==90)
                                 tb.setText("off");
                         }
                   }

        } catch (Exception e) {
         // ADD THIS TO SEE ANY ERROR 
         e.printStackTrace();            
        }

if you are running this thread inside activity class then you can run this

runOnUiThread(newRunnable() {
            @Overridepublicvoidrun() {
                tb.setText("ON")
            }
        });

else you have to implement some mechanism using broadcast receiver or interface for passing the data to your activity/fragment for updating the textview inside runOnUiThread.

Post a Comment for "Receive Character Using Android Phone From Arduino"