Sending and recieving data over wifi connection in android programatically

3k views Asked by At

I am trying to make a app that communicates with a hotspot in my pc. I am being able to connect to the hotspot but i am not being able to figure out how to send and recieve data in android. This is my code as of now in my app.

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// setup a wifi configuration
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"pigo\"";
wc.preSharedKey = "\"12345678\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
int netId = wifiManager.addNetwork(wc);
wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
stepinfo.setText("Initiating Connection . . .");

I have looked at other similar questions but nothing seems to be working with my app.

1

There are 1 answers

0
unrealsoul007 On

By doing what you are doing above you can only connect to the hotspot of the computer (Windows PC - given you are using netsh wlan). This will allow you to connect to your PC and at max share its internet. But to enable communication between your computer and your phone, you need to build an app over this layer.

What this app will do is. It will listen on specific port(s) on your PC and your mobile. So when the connection through hotspot is established you will need to have a Socket and do Socket.connect(new InetAddress(IP, PORT)). Lets say your computer listens on PORT_0, then your mobile needs to do Socket.connect(new InetAddress(IP_COMPUTER, PORT_0)).

This way you now have a socket which can be used for communication between your PC and your mobile.