javaandroideclipsewifi-directwifip2p

Pass a value from broadcastreceiver class to main activity in wifi direct


I want to send the value in string variable result, which is in the WiFiDirectBroadcastReceiver, to WiFiDirectActivity and I tried doing it as shown in this.

but when I ran the application it kept searching for peers. when I comment those added lines (last 3 line of WiFiDirectBroadcastReceiver) it works fine.

codes for onReceive function in WiFiDirectBroadcastReceiver and upto onResume() in WiFiDirectActivity are added.

how I can pass that value to WiFiDirectActivity? what am I missing here?

WiFiDirectBroadcastReceiver.java

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

      //if (MyIntentService.ACTION_MyIntentService.equals(action))
     // {
        //result = intent.getStringExtra(MyIntentService.EXTRA_KEY_OUT);
          result = "hello";
      //}

    if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

        // UI update to indicate wifi p2p status.
        int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
        if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
            // Wifi Direct mode is enabled
            activity.setIsWifiP2pEnabled(true);
        } else {
            activity.setIsWifiP2pEnabled(false);
            activity.resetData();

        }
        Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
    } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

        // request available peers from the wifi p2p manager. This is an
        // asynchronous call and the calling activity is notified with a
        // callback on PeerListListener.onPeersAvailable()
        if (manager != null) {
            manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
                    .findFragmentById(R.id.frag_list));
        }
        Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
    } else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {

        if (manager == null) {
            return;
        }

        NetworkInfo networkInfo = (NetworkInfo) intent
                .getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

        if (networkInfo.isConnected()) {

            // we are connected with the other device, request connection
            // info to find group owner IP

            DeviceDetailFragment fragment = (DeviceDetailFragment) activity
                    .getFragmentManager().findFragmentById(R.id.frag_detail);
            manager.requestConnectionInfo(channel, fragment);
            //fragment.peerCountInfo=result;
            //fragment.peerCt = peerCountFromDlist;
            //fragment.peerNm = peerNameFromDlist;
        } else {
            // It's a disconnect
            activity.resetData();
        }
    } else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
        DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
                .findFragmentById(R.id.frag_list);
        fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
                WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
        //peerCountFromDlist = fragment.peerTot;
        //peerNameFromDlist = fragment.deviceid;

    }

    intent.putExtra("message",result);
    intent.setClass(context, WiFiDirectActivity.class);
    context.startActivity(intent);
}

WiFiDirectActivity.java

public class WiFiDirectActivity extends Activity implements ChannelListener, DeviceActionListener {

public static final String TAG = "wifidirectdemo"; private WifiP2pManager manager; private boolean isWifiP2pEnabled = false; private boolean retryChannel = false; private final IntentFilter intentFilter = new IntentFilter(); private Channel channel; private BroadcastReceiver receiver = null; private String resultString; private int peerCount; /** * @param isWifiP2pEnabled the isWifiP2pEnabled to set */ public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) { this.isWifiP2pEnabled = isWifiP2pEnabled; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // add necessary intent values to be matched. intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION); intentFilter.addAction(MyIntentService.ACTION_MyIntentService); manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); channel = manager.initialize(this, getMainLooper(), null); } /** register the BroadcastReceiver with the intent values to be matched */ @Override public void onResume() { super.onResume(); receiver = new WiFiDirectBroadcastReceiver(manager, channel, this); registerReceiver(receiver, intentFilter); Bundle extras = getIntent().getExtras(); if (extras != null) { resultString = extras.getString("message"); } }

Solution

  • I found this easy way to send some data to Main Activity from BroadcastReceiver thanks to Mike M. (same way setIsWifiP2pEnabled(boolean isWifiP2pEnabled) method works)

    sending "message" and retrieving it to str variable ;

    create a public method in the MainActivity :

       public void setResult(String result){
              str = result;
    }
    

    Then call it in the BroadcastReceiver :

    activity.setResult("message");