4

I'm working on an Android Wi-Fi P2p project (also called WiFi direct).

This is how I try to create the group:

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {

  private  WifiP2pManager mManager;
  private  Channel        mChannel;
  private  MainActivity   mActivity;

    public WiFiDirectBroadcastReceiver(WifiP2pManager p2pManager,
                                       Channel channel, MainActivity mainActivity) {
        super();
        this.mManager = p2pManager;
        this.mChannel = channel;
        this.mActivity = mainActivity;
    }

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

        if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
            String state = MainActivity.wifiP2PStateMap.get(intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1));
            if (state.equalsIgnoreCase("ENABLED")) {
                Log.d(MainActivity.TAG, "P2P state changed to ENABLED ");

                mManager.createGroup(mChannel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        Log.d(MainActivity.TAG, "Group created ");
                    }

                    @Override
                    public void onFailure(int reason) {
                        Log.d(MainActivity.TAG, "Failed to create Group " + MainActivity.failureMap.get(reason));
                    }
                });
            }
        }else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)){
            //STUB
        }else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)){
            //STUB
        }else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
            //STUB
        }else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
            //STUB
        }
    }
}

When I scan the available wifi groups using wifi-analyzer I see that my group was created on channel 1, and it is always on channel 1.

Since this channel might be over crowded with other Access points I want to use a different channel.

The problem is that Android's API does not have a visible method to change the channel.

Here I found these questions: Q1, Q2, but they suggest rooting the device which is not an option for me.

When I looked into Android's sources I found that there is a hidden method:

/** @hide */
public void setWifiP2pChannels(Channel c, int lc, int oc, ActionListener listener) {
    checkChannel(c);
    Bundle p2pChannels = new Bundle();
    p2pChannels.putInt("lc", lc);
    p2pChannels.putInt("oc", oc);
    c.mAsyncChannel.sendMessage(SET_CHANNEL, 0, c.putListener(listener), p2pChannels);
}

Using reflection I tried the following:

Method setWifiP2pChannels = p2pManager.getClass().getMethod("setWifiP2pChannels", WifiP2pManager.Channel.class, int.class, int.class, WifiP2pManager.ActionListener.class);

setWifiP2pChannels.invoke(p2pManager, channel, 0, channel_to_set, new WifiP2pManager.ActionListener() {
    @Override
    public void onSuccess() {

        Log.d(MainActivity.TAG, "Changed channel (" + channel_to_set + ") succeeded");
    }

    @Override
    public void onFailure(int reason) {
        Log.d(MainActivity.TAG, "Changed channel (" + channel_to_set + ")  failed");
    }


});

When channel_to_set was 0 or 1 the actual channel did not change. When channel_to_set to something higher, creating a group returned an error.

Any idea how to solve this?

Thanks

Community
  • 1
  • 1
noti
  • 887
  • 7
  • 25
  • It's actually a very deliberate design of the Android P2P system to use the same channel. The WiFi chipsets in mobile phones aren't really designed to be like Wifi routers - and multiple in/out is pretty limited (and designed for small size of phones and battery conservation - not range or capability). The design is such that it favors the main AP it is associated with - not sure what logic it uses when not associated - but the source code is out there. Expect changes as each new release progresses, as this code in Android is often problematic in real-world experience – RoundSparrow hilltx Feb 26 '16 at 04:26

0 Answers0