Android Question Arduino > Android USB connection

Declan

Well-Known Member
Licensed User
Longtime User
Hi,
Please be gentle - I am a complete newbie :(
I have purchased the Full Version of B4A and am looking for an example of an app that will read messages sent from an Arduino UNO R3 connected to the USB port of my Android Tablet and display the message in a window.
The message is sent at regular intervals from the Arduino UNO.
What I am trying to mimic is that the Android Tablet will act as a Serial Monitor (similar to the Serial Monitor in the Arduino IDE).
All the examples that I have found are way more complex than expect and after reading and searching for hours - I finally succumb.
 

Declan

Well-Known Member
Licensed User
Longtime User
Yes, I did.
However, that refers to the Arduino Mega 2560 or Mega ADK.
If I select either of those Arduino boards in the Arduino IDE, the code compiles with no problem.
But the code will not compile if I select the Arduino UNO board in the Arduino IDE.

The errors on compile are:
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp: In static member function 'static void MAX3421E::setRST(uint8_t)':
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:58: error: 'PORTJ' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:58: error: 'PJ2' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:60: error: 'PORTJ' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:60: error: 'PJ2' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp: In static member function 'static uint8_t MAX3421E::readINT()':
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:65: error: 'PINE' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:65: error: 'PE6' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp: In static member function 'static void MAX3421E::pinInit()':
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:77: error: 'DDRE' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:77: error: 'PE6' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:78: error: 'DDRJ' was not declared in this scope
C:\Program Files\Arduino\libraries\USB_Host_Shield\Max3421e.cpp:78: error: 'PJ2' was not declared in this scope
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Thanks Licht2002,
That worked for me and I can see the data coming in to a edittext control that I placed in the Android app.
This needs a bit of work on so that I can filter-out the data I need from the received message.
I can also send a message to the Arduino (to change certain parameters in the Arduino) which seems to be working OK.

I am busy searching for some code examples of reading the message received with Astreams_NewData.
I need to wait - or fill the Buffer - until a ETX character arrives ("#"), then I have a complete string.
The message that is received is "$2,19,0,1,155#. The commas "," are delimiters.
Where:
$ = STX
2 = Device number (from an I2C bus on the Arduino)
19 = Sensor Data
0 = Sensor Fault condition (if any)
1 = Sensor Output State
155 = Sensor Setpoint.
# = ETX (End of Text)

So, I need to wait for the "#" to confirm complete string received.
If complete string is received, I must extract (parse) the data between the delimiters (",").
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Tom,
Do not forget to add the line in the manifest.....

Checking the manifest, the code below was already inserted.
AddActivityText(main, <intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />)

Is that what you mean?
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If you don't need access to the raw bytes, you're better off using the AsyncStreamsText class which will give you a simple to use _NewText(Text as String) event. This class takes care of waiting for the buffer to fill, end of message characters, etc... In your example, you would receive "2,19,0,1,155" in the _NewText() event and you need only use Regex.Split(",", Text) to split it up into an array.
 
Upvote 0
Top