Actually I want to send data from a hardware piece to an android device. The hardware device is connected to local wireless router which is connected to modem. Android device will also connected to same router through WI-FI. Can you please suggest some links or tutorial from where i can get idea how to establish communication between hardware device an the android device to send and receive data through WI-FI .Please Help any sample code or links
3
-
1Can you elaborate on the hardware device? What is that hardware device. What are the network interfaces in it? What are the interfaces it is offering?Aman Gautam– Aman Gautam2013-12-03 07:36:48 +00:00Commented Dec 3, 2013 at 7:36
-
Here actually I'm having a custom hardware which will work as a wireless access point which will allow wireless devices (android phone)to connect to a wired network using Wi-Fi.and wi-fi is inbuilt into the hardwareNeelam Singh– Neelam Singh2013-12-03 09:20:21 +00:00Commented Dec 3, 2013 at 9:20
-
I came through an API in android called NSD ( Network Service Discovery) whether this will be useful for identifying the h/w connected through Wi-Fi and send commands to that... Suggest on thisVenkatraman– Venkatraman2014-09-18 07:16:32 +00:00Commented Sep 18, 2014 at 7:16
Add a comment
|
1 Answer
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SimpleClientActivity extends Activity {
private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
File file = new File("/mnt/sdcard/input.jpg"); //create file instance, file to transfer or any data
try {
client = new Socket("10.0.2.2", 4444);// ip address and port number of ur hardware device
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText("File Sent");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
// to send message u can also use below code
public static String ipAddress;// ur ip
public static int portNumber;// portnumber
private Socket client;
private OutputStreamWriter printwriter;
private String message;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
client = new Socket(ipAddress, portNumber);
printwriter = new OutputStreamWriter(client
.getOutputStream(), "ISO-8859-1");
printwriter.write("any message");
printwriter.flush();
printwriter.close();
client.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
6 Comments
Neelam Singh
Thanks Anil for your quick response !!!! for receiving data at android i need to write client code means at hardware side there should be server code which will communicate with client at android side and second question where is the use of wi fi
Neelam Singh
Please elaborate somewhat i really don't have idea about data communication through wi fi through android device and hardware
Neelam Singh
Here actually I'm having a custom hardware which will work as a wireless access point which will allow wireless devices (android phone)to connect to a wired network using Wi-Fi
Neelam Singh
thanks anil,what i got it i need to configure/set hardware device which is acting as a access point on android device,and before the client code start i need to check also whether wi fi is on or not on android device then i need to pass hardware device ip address and ip,correct me if i'm right
Neelam Singh
hi Anil !! got some doubt actually to make data transfer possible between hardware device and android mobile 2 ways are possible 1.Hardware device will act as access point.or 2.Android device will act as Access point.My question is if i'm making hardware device as access point,then after setting that access point in android mobile,How my android application will get details of set access point
|