8

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
  • 1
    Can 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? Commented 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 hardware Commented 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 this Commented Sep 18, 2014 at 7:16

1 Answer 1

7
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();
Sign up to request clarification or add additional context in comments.

6 Comments

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
Please elaborate somewhat i really don't have idea about data communication through wi fi through android device and hardware
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
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.