1

I have a bound service which I have setup in a Library module which I have created.

My issue would be simple if this was not a library module, as I need things to be dynamic for others.

The current app uses the library module to search and create connections via Wifi-Direct between two phones (no problems so far).

My issue is now my application (which I am testing to make sure the library module is not missing anything) does not know when the two phones have "connected".

I have tried a while loop to keep requesting contents and looping until it is not null, this will technically work? however i have had no luck with this.

I was thinking of implementing a wait function but this would be a last resort!

I am using the android guide to create my library Link


The Actual Problem

I am trying to get information on the connection to display to the user, obviously if the connection is yet to be established it will be null! thus causing me issues.

One solution I have found is to step through and manually wait until the other phone has accepted the connection and is successfully connected then carry on to request information etc..

Please let me know if any questions as this is very confusing I know!

Kurdish Droid
  • 279
  • 3
  • 15

1 Answers1

1

Updated: You will want to use sendBroadcast() in your library to send an intent to your app when your library detects a successful P2P connection. You will probably want to receive the intent in your app only if there is an Activity currently open. See new code below:

See that this was added to case where there is a P2P connection established, note that you should replace com.yourapp.example with your package name:

  Intent i = new Intent("com.yourapp.example.P2PCONNECTED");
  context.sendBroadcast(i);

Code to define the BroadcastReceiver in your library:

    WiFiDirectFilter = new IntentFilter(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    WiFiDirectFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    WiFiDirectFilterBroadcastReceiver = new BroadcastReceiver() {            
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

           Log.i("MyApp", action);

            if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
                int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);   

                    if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {   
                        Log.i("MyApp", "WifiDirect WIFI_P2P_STATE_CHANGED_ACTION Enabled: true");
                        //WiFi Direct Enabled
                        //Do something....
                    } 
                    else { 
                        Log.i("MyApp", "WifiDirect WIFI_P2P_STATE_CHANGED_ACTION Enabled: false");
                        //WiFi Direct not enabled...
                        //Do something.....
                    }

            }
            else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
                   NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);   

                    if(networkInfo != null) {
                        boolean isWiFiDirectConnected = networkInfo.isConnected();
                        Log.i("MyApp", "WifiDirect WIFI_P2P_CONNECTION_CHANGED_ACTION Connected: " + );
                        if (isWiFiDirectConnected){
                           //WiFi Direct connected!
                           //Send Broadcast to your app
                           Intent i = new Intent("com.yourapp.example.P2PCONNECTED");
                           context.sendBroadcast(i);
                        }
                        else{
                           //WiFi Direct not connected
                           //Do something

                        }
                    }

            }
        }
    };

Then in any activity or fragment in your app, you would want to register in onResume() and unregister in onPause(), see code below:

 @Override
public void onResume() {
    super.onResume();
    IntentFilter iFilter= new IntentFilter("com.yourapp.example.P2PCONNECTED");
    //iFilter.addAction("someOtherAction"); //if you want to add other actions to filter
    this.registerReceiver(br, iFilter);
}

@Override
public void onPause() {
    this.unregisterReceiver(br);
    super.onPause();
}

private BroadcastReceiver br = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals("com.yourapp.example.P2PCONNECTED")){
             this.runOnUiThread(mUpdateP2PStatus);
        }
    }

};

private final Runnable mUpdateP2PStatus= new Runnable() {

    @Override
    public void run() {
      //TODO: Update your UI here                        
    }
};
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Yes this is correct and I have already implemented this in my library, the issue is how do I call back dynamically? to let the user know connection is sucessful – Kurdish Droid Mar 03 '15 at 22:12
  • Ahh, ok. You would probably want to send your own Broadcasts from your library, and use a BroadcastReceiver in your app to receive them. See this post: http://stackoverflow.com/questions/15634062/how-to-callback-into-various-activities-from-a-android-library/15638686#15638686 – Daniel Nugent Mar 03 '15 at 22:30
  • Hi again sorry for this just had time to look at this now, that is the answer I am looking for however I dont know what exactly to implement... It sounds to me to keep my main broadcast receiver and re-register it on each activity I use? surely not ? or do I create another broadcast receiver for each activity to pickup these calls? Please also note I use a bound service and fragments! – Kurdish Droid Mar 05 '15 at 11:15
  • Hi there, I updated the answer with how I think it would work. I haven't tested this with a library/app scenario, so you may need to modify, but hopefully it will get you on the right track. – Daniel Nugent Mar 05 '15 at 17:49
  • yes this will do atm however i have ran into a problem, because im extending fragments, I cannot extend BroadcastReceiver... Any suggestions? How can I implement a interface as this is the only solution i found. – Kurdish Droid Mar 07 '15 at 12:01
  • Forgot to mention I am having issues with registering and unregistering broadcast receiver. – Kurdish Droid Mar 07 '15 at 12:44
  • If you're doing it in a Fragment, you can use `getActivity().registerReceiver()` take a look at this post: http://stackoverflow.com/questions/16616654/register-and-unregister-broadcast-receiver-in-a-fragment – Daniel Nugent Mar 07 '15 at 16:11