android


TCP Comunication between a WiFi Module and Android App


I need to implement a TCP comunication between the SPWF01SA Wifi-Module (from ST Microelectronics) and an Android App. For the Wifi-Module firmware i'm using the X-Cube-WIFI Middleware library as a Server Socket, while in Android i have an AsyncTask as a Client Socket. The wifi module is controlled by the stm32f103 micro(using an I2C interface) and they are both connected to an access point. Here is the Android Client Socket code for the initialization/socket-read and socket-write:
#Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
boolean result = false;
try {
Log.i("AsyncTask", "doInBackground: Creating socket");
//Get a List of IPs that have a the desired port opened
SocketAddress sockaddr = new InetSocketAddress(MY_IP, PORT);
nsocket = new Socket();
nsocket.connect(sockaddr, 5000); //10 second connection timeout
if (nsocket.isConnected()) {
nsocket.setSendBufferSize(250);
nis = nsocket.getInputStream();
nos = nsocket.getOutputStream();
Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
Log.i("AsyncTask", "doInBackground: Waiting for inital data...");
byte[] buffer = new byte[4096];
int read = nis.read(buffer, 0, 4096); //This is blocking
publishType = PUBLISH_READ;
while(read != -1){
byte[] tempdata = new byte[read];
System.arraycopy(buffer, 0, tempdata, 0, read);
publishProgress(tempdata);
Log.i("AsyncTask", "doInBackground: Got some data");
read = nis.read(buffer, 0, 4096); //This is blocking
}
}
} catch (IOException e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: IOException");
result = true;
} catch (Exception e) {
e.printStackTrace();
Log.i("AsyncTask", "doInBackground: Exception");
result = true;
} finally {
try {
nis.close();
nos.close();
nsocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("AsyncTask", "doInBackground: Finished");
}
return result;
}
public boolean sendDataToNetwork(final String cmd)
{
if (nsocket.isConnected())
{
Log.i("AsyncTask", "SendDataToNetwork: Writing received message to socket");
new Thread(new Runnable()
{
public void run()
{
try
{
nos.write(cmd.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
Log.i("AsyncTask", "SendDataToNetwork: Message send failed. Caught an exception");
}
}
}).start();
return true;
}
Log.i("AsyncTask", "SendDataToNetwork: Cannot send message. Socket is closed");
return false;
}
For small size socket write(From Android to the module) everything works, the problem rises when i need to send more than 512bytes (witch is the wifi module rx buffer). If i send more than that to the wifi module, its RX buffer goes on overflow. I thought, that the line nsocket.setSendBufferSize(250) would make the trick, but doesn't make any difference. How can i stream to the wifi module large amount of data, if the latter has a small receiver buffer?
I'm new to TCP implementations, thank you for your time.
Other info:
The WiFi Module is controlled through the I2C interface by the STM32F103RB microcontroller, and the Middlware library is provided directly by ST(I have posted the link at the beginging).
After debbuging the software on the micro that comunicates with the wifi module, i saw that there are two malfunctions that occur ramdomply:
either the I2C Rx buffer overflows
or while it reads the rx buffer it can miss some I2C commands from the wifi modules, wich serves to read the current pending packet on the i2c rx buffer;
I2C speed between wifi-module and micro is set at 460800kbps.
Either of two, the problem seems to be that the TCP stream from android is too fast. Here are some specifications for the wifi module communciation(Provided by the STM team):
Wifi Module buffer size is 1Kb(same on the micro);
For TCP, the MSS (maximum segment size) is equal to 730B (fixed in the lwip stack options). So, both for TX and for RX, this is the max segment transmittable or receivable by the module.
ST Microelectronics affirm that by spliting the data in 100B packets, they reached 60kB/s.
How can i slow down the stream from the Android App, and fix an MSS less or equal to 730B?

Related Links

Dynamically added EditTexts focus goes from first to last when keyboard next button is clicked
Android : Click event is not triggered at time of animation
Galaxy S3 screen blackout layout not inflating but click actions are working
Android WebView Blocked a frame with origin
Move the layout as the user swipes the layout from right to left and dismiss it after few seconds with animation in android
How to get Data from Realm for my ListView
Multi-layer / libraries architecture with dagger2: designing scopes, components, modules
Can not send request to server on tab change
Fragment detached from activity without any obvious reason
Explicit Intent Errors while working with putExtra()
showing a specific output on alertdialog based on what is saved in sharedPreferences
Scroll a RecyclerView when scrolling second one
Firebase auth with an active session when the user id was deleted from the console
Upload image to wordpress via Android
Android. How to clone newest version of Google Calendar?
Align 2 textviews horizontally android

Categories

HOME
httparty
porting
design-patterns
c++11
android-studio-2.2
pda
youtube-data-api-v3
solver
r-googlesheets
rendering
spring-data-mongodb
web-hosting
hugo
jetty
x264
missing-data
flume
g++
vimdiff
reactive-programming
mautic
libusb
keytool
chargify
apache2.4
user-experience
gmock
google-api-client
siri
moses
maze
stackexchange
pylons
fragment-backstack
delimiter
tpm
paperjs
user-defined-functions
iis-express
paragraph
memory-address
activexobject
hunspell
slacktextviewcontroller
node-mssql
demo
metawidget
kill
doc
preloader
linqpad
strftime
optimizely
getchar
xbmc
openal
user-defined-fields
trojan
vibrate
flex4.6
xdoclet
project-organization
cda
kiosk
createprocess
invoke-sqlcmd
cycle2
design-principles
inf
django-urls
zen-cart
wso2greg
typeconverter
exceldatareader
uncaught-typeerror
appserver
ignite-ui
ora-00904
live555
jjaql
ray
artemis
scrypt
dtmf
asdf
kaleidoscope
video-codecs
http-status-code-410
qi4j
monotouch.dialog
cross-database
jquery-tools
htdocs
webdev.webserver
xui
cuteeditor
twitterizer
photolibrary
pagemethods
duplex
murmurhash
wcf-instancing
feasibility
wordprocessingml
dojo-dnd
wysiwym
movieplayer

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App