我已经苦苦挣扎了几天,一直在使用2部运行Xamarin测试应用程序的Samsung S8手机,并使用最少的WiFiP2PManager代码来发现对等实体。我已经浏览了社区论坛和文档-可以这么辛苦,肯定有我缺少的东西。
我已经检查了常见错误:
ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, CHANGE_NETWORK_STATE, INTERNET
)仍然,使用设备列表的空集合调用OnPeersAvailable。
在调试窗口中的调用顺序原来是:
WifiP2pManager.Initialize
MainActivity.RegisterReceiver
WifiP2pManager.DiscoverPeers
WifiP2pManager.RequestPeers
OnPeersAvailable
(DeviceList.Count == 0
)这是我的代码,也许您可以看到一些内容?
using System; using Android.App; using Android.OS; using Android.Support.V7.App; using Android.Runtime; using Android.Net.Wifi.P2p; using Android.Content; using static Android.Net.Wifi.P2p.WifiP2pManager; namespace WiFiDirectTest { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)] public class MainActivity : AppCompatActivity, IChannelListener, IPeerListListener { private WifiP2pManager manager; Channel channel; MyBroadcastReceiver receiver; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); SetContentView(Resource.Layout.activity_main); System.Diagnostics.Debug.WriteLine($"Wifi - initialize"); manager = (WifiP2pManager)GetSystemService(WifiP2pService); channel = manager.Initialize(this, MainLooper, null); } protected override void OnResume() { base.OnResume(); System.Diagnostics.Debug.WriteLine($"Wifi - register receiver"); receiver = new MyBroadcastReceiver(manager, channel, this); receiver.Register(this); System.Diagnostics.Debug.WriteLine($"Wifi - discover peers"); manager.DiscoverPeers(channel, new MyActionListner()); } protected override void OnPause() { base.OnPause(); System.Diagnostics.Debug.WriteLine($"Wifi - unregister receiver"); receiver.Unregister(this); receiver = null; } public void OnPeersAvailable(WifiP2pDeviceList peers) { var count = peers.DeviceList.Count; System.Diagnostics.Debug.WriteLine($"Wifi - peers available: {count}"); } public void OnChannelDisconnected() { System.Diagnostics.Debug.WriteLine("OnChannelDisconnected"); } private class MyActionListner : Java.Lang.Object, WifiP2pManager.IActionListener { public MyActionListner() { } public void OnFailure(WifiP2pFailureReason reason) { // breakpoint placed here, never reached. } public void OnSuccess() { } } } public class MyBroadcastReceiver : BroadcastReceiver { WifiP2pManager manager; WifiP2pManager.Channel channel; MainActivity activity; public MyBroadcastReceiver(WifiP2pManager manager, Channel channel, MainActivity activity) { this.manager = manager; this.channel = channel; this.activity = activity; } public void Register(MainActivity context) { var intentFilter = new IntentFilter(); intentFilter.AddAction(WifiP2pManager.WifiP2pStateChangedAction); intentFilter.AddAction(WifiP2pManager.WifiP2pPeersChangedAction); intentFilter.AddAction(WifiP2pManager.WifiP2pConnectionChangedAction); intentFilter.AddAction(WifiP2pManager.WifiP2pThisDeviceChangedAction); context.RegisterReceiver(this, intentFilter); } public void Unregister(MainActivity context) { context.UnregisterReceiver(this); } public override void OnReceive(Context context, Intent intent) { var action = intent.Action; if (WifiP2pManager.WifiP2pStateChangedAction.Equals(action)) { System.Diagnostics.Debug.WriteLine("WifiP2pStateChangedAction"); } else if (WifiP2pManager.WifiP2pPeersChangedAction.Equals(action)) { System.Diagnostics.Debug.WriteLine($"Wifi - request peers"); manager.RequestPeers(channel, activity); } else if (WifiP2pManager.WifiP2pConnectionChangedAction.Equals(action)) { System.Diagnostics.Debug.WriteLine("WifiP2pConnectionChangedAction"); } else if (WifiP2pManager.WifiP2pThisDeviceChangedAction.Equals(action)) { System.Diagnostics.Debug.WriteLine("WifiP2pThisDeviceChangedAction"); } } } }
有什么想法吗?
-约翰
我已经苦苦挣扎了几天,一直在使用2部运行Xamarin测试应用程序的Samsung S8手机,并使用最少的WiFiP2PManager代码来发现对等实体。我浏览了社区论坛,然后...
您可以将设备列表复制到列表中并在其上应用size()(请参见下文,其中peers_list是列表)