Android 对等设备上的 WiFi 直接图像共享
Posted
技术标签:
【中文标题】Android 对等设备上的 WiFi 直接图像共享【英文标题】:WiFi Direct Image Sharing on peer device in Android 【发布时间】:2016-12-01 04:11:41 【问题描述】:我已经成功实现了 wifi direct 示例演示,两种方式的图像共享工作正常,但我的问题是当一个设备 A 发送连接到设备 B 的邀请而设备 B 取消该邀请时,我无法处理该事件的取消回调,因为它是 android 生成的。
任何可以帮助我解决问题的人。
以下是我的广播接收器代码和连接代码:
/**
* 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));
对于连接,我使用了以下代码:
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = device.deviceAddress;
config.wps.setup = WpsInfo.PBC;
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
device.wps.setup = WpsInfo.PBC;
device.groupOwnerIntent = 0; // I want this device to become the owner
manager.connect(channel, device, new ActionListener()
@Override
public void onSuccess()
// WiFiDirectBroadcastReceiver will notify us. Ignore for now.
@Override
public void onFailure(int reason)
Toast.makeText(WiFiDirectActivity.this, "Connect failed. Retry.",
Toast.LENGTH_SHORT).show();
);
【问题讨论】:
【参考方案1】:该类需要实现ConnectionInfoListener。在 onConnectionInfoAvailable(final WifiP2pInfo info) 函数中,您可以检查是否建立了成功的连接。 WifiP2pInfo 类型的 info 参数包含有关连接的信息。它包含一个名为 groupFormed 的布尔值,指示 p2p 组是否已成功形成。如果设备是groupOwner和groupOwner的IP等,也可以从中检索。
@覆盖 公共无效 onConnectionInfoAvailable(最终 WifiP2pInfo 信息)
// 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
希望这会有所帮助。如果您有任何问题,请告诉我。
来自:
Check whether android wifip2p connection was successful?
【讨论】:
以上是关于Android 对等设备上的 WiFi 直接图像共享的主要内容,如果未能解决你的问题,请参考以下文章
Android WiFi 直接在 android pie 上不显示对等点
使用wifi直接在android中的两个wifi直接连接用户之间传递字符串?