connect iPhone to Android's Wifi Direct soft AP

47,090

Depending on your phone you can just set up your Android phone as a portable hotspot and connect to that with the iPhone. From there it would be application specific to get data transferred.

However you can also use the Androids WiFi-Direct libraries. In that case you would use them to set up the Android phone to create a "Group owner", which basically is the same as it being a portable hotspot. Check out:

http://developer.android.com/guide/topics/connectivity/wifip2p.html

I'll give you a code example to help you get started.

public class WifiDirectAPtestActivity extends Activity 
{
private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;

private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;

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

    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);
    createGroup();
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
    manager.removeGroup(channel, new ActionListener() {

        @Override
        public void onFailure(int reasonCode) {
            Log.d("WifiDirectAptestActivity", "Disconnect failed. Reason :" + reasonCode);

        }

        @Override
        public void onSuccess() {
            Log.d("WifiDirectAptestActivity", "Should have been sucessfully removed");
        }

    });
}
public void createGroup()
{
    manager.createGroup(channel, new ActionListener() {

        @Override
        public void onSuccess() {
            // WiFiDirectBroadcastReceiver will notify us. Ignore for now.
            Log.d("WifiDirectAPtestActivity", "Group creating request successfully send");
        }

        @Override
        public void onFailure(int reason) {
            Toast.makeText(WifiDirectAPtestActivity.this, "Connect failed. Retry.",
                    Toast.LENGTH_SHORT).show();
        }
    });
}

In addition you'll need the broadcast receiver, look at the WiFi-Direct demo and it should be clear to you.

Note that line manager.createGroup(channel, new ActionListener() is the codeline of interest, it is this line that actually sets up the device as a portable hotspot.

Hope this clarifies things, I don't really know how detailed explanation you need. Comment if some things are not clear.

Share:
47,090
AlcubierreDrive
Author by

AlcubierreDrive

Updated on July 09, 2022

Comments

  • AlcubierreDrive
    AlcubierreDrive almost 2 years

    I know that Wifi Direct works by creating a Soft AP (software access point) in one of the devices. I also know that many Androids support Wifi Direct, but iPhones do not.

    My question is: is it possible to create a device-to-device wifi link that is Wifi Direct on the Android side, but regular wifi on the iPhone side? Where the Android's Wifi Direct would be presenting a soft AP, which the iPhone would see as indistinguishable from a regular AP and be able to associate to.

    Imagine that this is out in the wilderness where no router AP is available. Also, neither user has a tethering plan.

    This link would be used by a Bump-like app to transfer files.