0

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");
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
senrulz
  • 121
  • 1
  • 2
  • 12
  • have u tried SharedPreferences? – Itzik Samara Jul 20 '14 at 10:34
  • hey guys, thanks for your answers! I actually tried the things i'm familiar with since i'm a bit new to android! @Itzik what is SharedPreferences? – senrulz Jul 21 '14 at 13:45
  • @Mike i can understand the retrieval part now, sorry, but what do you mean by put info into the "Intent you're using to start the Activity"? – senrulz Jul 21 '14 at 13:45
  • oh i think i get the point, but it would be nice if you can post the answer i really appreciate it :) – senrulz Jul 21 '14 at 14:50
  • 1
    @Mike oh thank you, i get it now! it was right there in that demo coding (setIsWifiP2pEnabled is done the same way) and i didn't see it. i feel stupid for asking the question now lol sorry for your time and thank you very much for pointing that! add it as an answer if you want, then i'll mark it as the answer :) – senrulz Jul 22 '14 at 17:31
  • 1
    @Mike ohh that's fine, you answered somehow and thank you for that! I have added the answer. thanks again. – senrulz Jul 24 '14 at 05:05
  • yeah yeah! it was the "wifi-direct demo" – senrulz Jul 24 '14 at 15:57

1 Answers1

0

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");

senrulz
  • 121
  • 1
  • 2
  • 12