I'm developing a distributed application that communicates via Wi-Fi Direct, but I'm new to this technology and I'm at the moment messing around with it. But I face this problem. So first I'll define the environment:
There is one single android application being developed using Xamarin (so all code will be in C#). It has just one activity MainActivity
.
I'm deploying it to two different devices:
Motorola MotoG3: Android 6.0.1 (right picture)
Xiaomi Mi A1: Android 8.1.0 (left picture)
Important: On both of them I can search for peers using WiFi Direct directly from the built-in network options, so it shouldn't be a hardware issue.
And what is the problem? Well, with the MotoG3 device the application works fine, it finds the other device (Mi A1) and some neighbor's device. But when it's run on Mi A1 device it won't find any peers. When debugging, the OnPeersAvailable
callback has for parameter an empty WifiP2pDeviceList
.
I know the situation really looks like compatibility issues between android sdks, but I have tried everything I've read: adding <uses-feature..>, updating to last target framework, trying both Release and Debug configurations,... so any other idea will be welcome.
In the following images I show the result of the discovering peers action for both mentioned devices. On the left: Mi A1 (not working), on the right: MotoG3 (working fine).
Code snippets:
In MainActivity.cs:
btnDiscover.Click += delegate
{
mManager.DiscoverPeers(mChannel, new MyActionListener(this));
};
peerListListener = new MyPeersListListener(this);
MyActionListener class
private class MyActionListener : Java.Lang.Object, WifiP2pManager.IActionListener
{
private MainActivity mActivity;
public MyActionListener(MainActivity mainActivity)
{
this.mActivity = mainActivity;
}
public void OnFailure([GeneratedEnum] WifiP2pFailureReason reason)
{
mActivity.tvConnStatus.SetText("Discovery starting failed", TextView.BufferType.Normal);
}
public void OnSuccess()
{
mActivity.tvConnStatus.SetText("Discovery started", TextView.BufferType.Normal);
}
}
MyPeersListListener class
private class MyPeersListListener : Java.Lang.Object, WifiP2pManager.IPeerListListener
{
private readonly MainActivity mainActivity;
public MyPeersListListener(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
public void OnPeersAvailable(WifiP2pDeviceList peersList)
{
if (peersList.DeviceList.Count == 0)
{
Toast.MakeText(mainActivity.ApplicationContext, "No devices found", ToastLength.Short).Show();
return;
}
if (!peersList.Equals(mainActivity.mPeers))
{
mainActivity.mPeers.Clear();
mainActivity.mPeers.AddRange(peersList.DeviceList);
mainActivity.devicesNames = new string[peersList.DeviceList.Count];
mainActivity.devicesArray = new WifiP2pDevice[peersList.DeviceList.Count];
int i = 0;
foreach (WifiP2pDevice device in peersList.DeviceList)
{
mainActivity.devicesNames[i] = device.DeviceName;
mainActivity.devicesArray[i] = device;
i++;
}
ArrayAdapter<string> adapter = new ArrayAdapter<string>(mainActivity.ApplicationContext, Android.Resource.Layout.SimpleListItem1, mainActivity.devicesNames);
mainActivity.lvPeersList.Adapter = adapter;
}
}
}
The currently granted permissions are the following:
- android.permission.CHANGE_WIFI_STATE
- android.permission.ACCESS_WIFI_STATE
- android.permission.ACCESS_NETWORK_STATE
- android.permission.INTERNET
- android.permission.CHANGE_NETWORK_STATE
- android.permission.UPDATE_DEVICE_STATS
I think I haven't left anything, if there's some more info to share, just ask.
Any help will be more than welcome.
Thanks.