android - 在不发现对等点的情况下连接到 WiFi P2P 设备
android - Connecting to a WiFi P2P device without discovering peers
我想与另一台通过 NFC 对等的设备建立 WiFi 直接连接。我的步骤如下:
首先,Device A获取自己的WiFiP2P地址,通过NFC传输给Device B。
然后,设备 B 尝试使用提供的地址与设备 A 建立连接。
如您所见,我没有在这个过程中涉及发现同行。但是当Device B尝试连接时,结果总是失败(原因0,这应该是ERROR)。
我认为这可能与设备可见性有关,但我不知道也找不到任何使设备可见的代码。
这是我的代码:
//NOTE: These code should be executed on Device B
//Starting WiFi Direct Transmission
//First we should establish a connection
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = remoteWifiP2pDevice;
//remoteWifiP2pDevice is the address of device A obtained from NFC
config.wps.setup = WpsInfo.PBC;
mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//success logic
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
if (!FILE_RECV)
{
new SendFilesTask().execute("");
}
}
@Override
public void onFailure(int reason) {
//failure logic
Toast.makeText(MainActivity.this, "failed" + reason, Toast.LENGTH_SHORT).show();
}
});
在 OnCreate() 中我有
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);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
WifiDirectBroadcastReceiver 的代码只与获取设备 A 的地址有关,可以认为是空的。
这些有什么问题,我该如何解决?
提前致谢。
P.S。如果我手动连接设备 A 和 B,并再次 运行 我的代码,它 returns 成功。
我发现延迟几秒就能连接成功。我不知道原因,但这可以用作解决方法。
那么到底有没有更好的解决办法呢?为什么延迟有效?
- WIfi 直接假设:
All the devices should be in discoverable (scanning mode)
simultaneously.
Devices scan for a 30 sec and after that
it stops by default.So we need to initiate scan programatically
using discoverpeers method.
Most important is displaying
nearby devices is device specific.ie sometimes devices wont show
nearby ones eventhough they are available.Thats why wifi direct
is not reliable and because of these, there wont be much wifi
direct apps in play store.
我想与另一台通过 NFC 对等的设备建立 WiFi 直接连接。我的步骤如下:
首先,Device A获取自己的WiFiP2P地址,通过NFC传输给Device B。
然后,设备 B 尝试使用提供的地址与设备 A 建立连接。
如您所见,我没有在这个过程中涉及发现同行。但是当Device B尝试连接时,结果总是失败(原因0,这应该是ERROR)。
我认为这可能与设备可见性有关,但我不知道也找不到任何使设备可见的代码。
这是我的代码:
//NOTE: These code should be executed on Device B
//Starting WiFi Direct Transmission
//First we should establish a connection
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = remoteWifiP2pDevice;
//remoteWifiP2pDevice is the address of device A obtained from NFC
config.wps.setup = WpsInfo.PBC;
mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
//success logic
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
if (!FILE_RECV)
{
new SendFilesTask().execute("");
}
}
@Override
public void onFailure(int reason) {
//failure logic
Toast.makeText(MainActivity.this, "failed" + reason, Toast.LENGTH_SHORT).show();
}
});
在 OnCreate() 中我有
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);
mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);
WifiDirectBroadcastReceiver 的代码只与获取设备 A 的地址有关,可以认为是空的。
这些有什么问题,我该如何解决? 提前致谢。
P.S。如果我手动连接设备 A 和 B,并再次 运行 我的代码,它 returns 成功。
我发现延迟几秒就能连接成功。我不知道原因,但这可以用作解决方法。
那么到底有没有更好的解决办法呢?为什么延迟有效?
- WIfi 直接假设:
All the devices should be in discoverable (scanning mode) simultaneously.
Devices scan for a 30 sec and after that it stops by default.So we need to initiate scan programatically using discoverpeers method.
Most important is displaying nearby devices is device specific.ie sometimes devices wont show nearby ones eventhough they are available.Thats why wifi direct is not reliable and because of these, there wont be much wifi direct apps in play store.