如何为非root用户修改Wi-Fi p2p频率?

时间:2015-09-17 09:13:38

标签: android android-wifi wifi-direct wifip2p

我正在开发Android Wi-Fi P2p项目(也称为WiFi直接)。

这是我尝试创建群组的方式:

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
        }
    }
}

当我使用wifi-analyzer扫描可用的wifi组时,我看到我的组是在通道1上创建的,它始终位于通道1上。

由于此频道可能过度拥挤其他接入点,我想使用其他频道。

问题是Android的API没有可见的方法来更改频道。

我在这里发现了以下问题:Q1Q2,但他们建议根据设备生根,这对我来说不是一个选项。

当我查看Android的来源时,我发现有一种隐藏的方法:

/** @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);
}

使用反射我尝试了以下内容:

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


});

channel_to_set为0或1时,实际频道没有变化。当channel_to_set更高时,创建组会返回错误。

知道如何解决这个问题吗?

由于

0 个答案:

没有答案