Alankrit Verma
Alankrit Verma

Reputation: 1

Unable to establish a simple socket connection between publisher and subscriber over Wifi-Aware

I am trying to create a socket connection between publisher and subscriber using Wifi Aware technology. I am following the below link from android developers website:

https://developer.android.com/develop/connectivity/wifi/wifi-aware#create_a_connection

As stated above, I am sending a message from subscriber which is successfully received on server side but when I request Network after creating a server socket on publisher, I get neither an error nor any network is received/created:

// Publisher callback method when publish called. When message send success it will then call server.accept() to listen

public void onMessageReceived(PeerHandle peer, byte[] message, Context mContext) {
    
   ......
   try{
      publisherSocket = new ServerSocket(0);
      publisherPort = publisherSocket.getLocalPort(0);
   }
    catch(...) {   .... }

   NetworkSpecifier networkSpecifier = new WifiAwareNetworkSpecifier.Builder(currentDiscoverySession,currentPublisherConnectedPeer).setPskPassphrase("some8Letter@").setPort(publisherPort).build;

   NetworkRequest publisherNetworkRequest = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI_AWARE).setNetworkSpecifier(networkSpecifier).build();

    ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback(){
       public void onAvailable(Network network) {
          // Problem is it was never called which means network not there
          currentDiscoverySession.sendMessage(currentPublisherConnectedPeer, 0, someDummyMessage);
       }
       
      .....};

   // Here ConnectivityManager is obatined using this.getSystemService(this.CONNECTIVITY_SERVICE) on onCreate of activity
   connectivityManager.requestNetwork(publisherNetworkRequest, callback);

// Subscribe callback method when subscribe called

public void onMessageReceived(PeerHandle peer, byte[] message, Context mContext) {
    
   NetworkSpecifier networkSpecifier = new WifiAwareNetworkSpecifier.Builder(currentDiscoverySession,peer).setPskPassphrase("some8Letter@").build();
   
   NetworkRequest subscriberNetworkRequest = new NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI_AWARE).setNetworkSpecifier(networkSpecifier).build();

   ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback(){
       public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities){
          .....
         WifiAwareNetworkInfo subscriberNetworkInfo = (WifiAwareNetworkInfo) networkCapabilities.getTransportInfo();
         
         subscriberSocket = network.getSocketFactory().createSocket(subscriberNetworkInfo.getPeerIpv6Addr(), subscriberNetworkInfo.getPort());
      // WRite some message using outputStream
      .....};


My manifests file :

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="34"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

minSdk : 34 targetSdk : 34

I do not know why it is not returning any network over Wifi-Aware when I call requestNetworkMethod.

I tried passing time in ms and saw that onUnavailable() method was called which basically meant that no such network is there/created.

I want to know if is it even possible to create such a socket connection over Wifi-Aware and if that is the case then is there something wrong with my code or is it because of any hardware limitation and if so how can I know that?

My ultimate goal is to create a video call app for the 2 apps to communicate over WIFI-Aware an my goal is to send and receive raw video bytes over this network without using any pre-existing protocols like WebRTC. If you find any problem with this approach then also please let me know.

Upvotes: 0

Views: 49

Answers (0)

Related Questions