我已经成功实现了wifi直接示例演示并且双向图像共享工作正常但我的问题是当一个设备A发送邀请连接到设备B而设备B取消该邀请时,我无法处理这取消了该事件的回调,因为它是Android生成的。
任何可以帮助我解决问题的人。
以下是我的广播接收代码和连接代码:
>CF
对于连接,我使用了以下代码:
/**
* A BroadcastReceiver that notifies of important wifi p2p events.
*/
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private Channel channel;
private WiFiDirectActivity activity;
/**
* @param manager WifiP2pManager system service
* @param channel Wifi p2p channel
* @param activity activity associated with the receiver
*/
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
WiFiDirectActivity activity) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
/*
* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
activity.setIsWifiP2pEnabled(true);
} else {
activity.setIsWifiP2pEnabled(false);
activity.resetData();
}
Log.d(WiFiDirectActivity.TAG, "P2P state changed - " + state);
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (manager != null) {
manager.requestPeers(channel, (PeerListListener) activity.getFragmentManager()
.findFragmentById(R.id.frag_list));
}
Log.d(WiFiDirectActivity.TAG, "P2P peers changed");
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
if (manager == null) {
return;
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// we are connected with the other device, request connection
// info to find group owner IP
DeviceDetailFragment fragment = (DeviceDetailFragment) activity
.getFragmentManager().findFragmentById(R.id.frag_detail);
manager.requestConnectionInfo(channel, fragment);
} else {
// It's a disconnect
activity.resetData();
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
DeviceListFragment fragment = (DeviceListFragment) activity.getFragmentManager()
.findFragmentById(R.id.frag_list);
fragment.updateThisDevice((WifiP2pDevice) intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));
}
}
}
答案 0 :(得分:0)
该类需要实现ConnectionInfoListener。在函数onConnectionInfoAvailable(最终WifiP2pInfo信息)中,您可以检查是否已建立成功连接。 WifiP2pInfo类型的info参数包含有关连接的信息。它包含一个名为groupFormed的布尔值,指示是否已成功形成p2p组。如果设备是groupOwner并且groupOwner的IP等,您也可以从中检索。
@覆盖 public void onConnectionInfoAvailable(final WifiP2pInfo info){
// After the group negotiation, we check if this device
// is acting as group owner
if (info.groupFormed && !info.isGroupOwner) {
// Do whatever you want the group owner to do
} else if (info.groupFormed) {
// The device now act as the slave device,
// and the other connected device is group owner
}
Google提供了一个非常好的演示应用,它使用WiFi Direct在2台设备之间发送图像。检查它的实现并尝试在它之上构建。链接:http://developer.android.com/guide/topics/connectivity/wifip2p.html
希望这会有所帮助。如果您有任何问题,请告诉我。
来自: