在我的两台设备上,当我尝试使用 WifiP2pManager
的 createGroup
或 removeGroup
时,我得到了 onFailure
以 2 (BUSY) 作为 reason
的回调。
我尝试使用此处给出的建议 WifiP2pManager return BUSY state on CreateGroup (在创建新组之前删除组),但这没有帮助,因为我总是得到 onFailure
removeGroup
中的回调。
我测试的设备:LG Optimus G (CM 12.1 - Android 5.1)、Gigabyte GSmart Guru G1 (stock Android 4.2)。
更新
代码是:
manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Debug.d();
manager.createGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Debug.d();
}
@Override
public void onFailure(int reason) {
Debug.d("" + reason);
}
});
}
@Override
public void onFailure(int reason) {
Debug.d("" + reason);
}
});
最佳答案
我收到此错误是因为我在组不存在时尝试使用 removeGroup
。解决方案是在删除当前组或创建新组之前检查当前组是否存在。
这是工作代码(我使用 Retrolambda):
manager.requestGroupInfo(channel, group -> {
if (group != null) {
Debug.d("group != null");
manager.removeGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Debug.d();
manager.createGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Debug.d();
}
@Override
public void onFailure(int reason) {
Debug.d("" + reason);
}
});
}
@Override
public void onFailure(int reason) {
Debug.d("" + reason);
}
});
} else {
manager.createGroup(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Debug.d();
}
@Override
public void onFailure(int reason) {
Debug.d("" + reason);
}
});
}
});
关于android - WifiP2pManager createGroup 和 removeGroup 失败并显示 BUSY 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30417586/