Create hotspot programatically using WiFi direct Android

Viewed 217

I have been trying to create a WiFi hotspot using WiFi Direct in Android. After searching on google and Android Developer Forum, I found out that this can be done using WifiP2pManager. I have been able to create a Group and generate it's credentials via the following code:

    WifiManager wifi =(WifiManager) getApplicationContext().getSystemService("wifi");
    IntentFilter intentFilter = new IntentFilter();
    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);

    WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    static Channel channel = manager.initialize(this, getMainLooper(), this);

    registerReceiver(p2pRecv, intentFilter);

    manager.createGroup(channel, null);

    manager.requestGroupInfo(channel, new GroupInfoListener() {
        @Override
        public void onGroupInfoAvailable(WifiP2pGroup group) {
            if (group != null && group.isGroupOwner()) {
                String groupName = group.getNetworkName();
                String groupPassword = group.getPassphrase();
                txtHotspotName.setText(groupName + "|||" + groupPassword);

            }
        }
    });

Once credentials are generated any device nearby is able to connect using these credentials, but after connection the internet doesn't work on connected device even when WiFi is connected on Group Owner device. Is there any way to get the internet to work on connected devices?

0 Answers
Related