哟,
我正试图通过WiFi Direct的奇迹来获取一个Android设备(它是一个Nexus 7,虽然我无法想象这太重要了)与Raspberry Pi进行通信。据说这是可能的,并且它似乎是六个小时前比一个更好的解决方案而不是沿着服务器 - 客户端路线,但我遇到了问题
Android开发者网站很不错,因为它有两个指南:
http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html http://developer.android.com/guide/topics/connectivity/wifip2p.html
这很可爱,因为我没有大量的Java经验。但实际上,在该网站上存在的问题很多,他们认为代码中的元素(即放置东西)通常会丢失。
我已经非常仔细地遵循了这两个指南,但我遇到了问题 - WIFI_P2P_PEERS_CHANGED_ACTION意图从不广播,所以当我正在寻找同伴时,它开始搜索,说一切都很好,但是没有给出我的结果列表......我猜它应该
我大部分时间一直在努力解决这个问题,为了让生活变得更加甜美,可以让生活更美好
CODE:
的活动:
package com.example.bingotest;
import java.util.ArrayList;
import java.util.List;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.Menu;
public class BingoActivity extends Activity {
private BingoView _view;
private IntentFilter _intentFilter = new IntentFilter();
private BroadcastReceiver _broadcastReceiver = null;
private WifiP2pManager _manager;
private Channel _channel;
private List _peers = new ArrayList();
private PeerListListener _peerListListener;
//-------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
_view = new BingoView(this);
setContentView(_view);
_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);
_manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
_channel = _manager.initialize(this, getMainLooper(), null);
//_broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this);
//registerReceiver(_broadcastReceiver, _intentFilter);
_peerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
Log.d("wifi", "here");
// Out with the old, in with the new.
_peers.clear();
_peers.addAll(peerList.getDeviceList());
if (_peers.size() == 0) {
Log.d("wifi", "No devices found");
return;
}
}
};
}
//-------------------------------------------------------------------------------
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bingo, menu);
return true;
}
//-------------------------------------------------------------------------------
@Override
public void onResume() {
super.onResume();
_broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this, _peerListListener);
registerReceiver(_broadcastReceiver, _intentFilter);
}
//-------------------------------------------------------------------------------
@Override
public void onPause() {
super.onPause();
unregisterReceiver(_broadcastReceiver);
}
}
WiFiDirectBroadcastReceiver类:
package com.example.bingotest;
import java.util.ArrayList;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.PeerListListener;
import android.util.Log;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager _manager;
private Channel _channel;
private BingoActivity _activityRef;
private List _peers = new ArrayList();
PeerListListener _peerListListener;
//-------------------------------------------------------------------------------
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel, BingoActivity activity, PeerListListener peerListListener) {
super();
_manager = manager;
_channel = channel;
_activityRef = activity;
_peerListListener = peerListListener;
_manager.discoverPeers(_channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d("wifi", "onsuccess");
}
@Override
public void onFailure(int reasonCode) {
Log.d("wifi", "onfailure");
}
});
}
//-------------------------------------------------------------------------------
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("wifi", "receive: " + intent.getAction());
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
Log.d("wifi", "WIFI DIRECT ON");
} else {
Log.d("wifi", "WIFI DIRECT OFF");
}
}
else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
Log.d("wifi", "Peers changed");
if (_manager != null) {
_manager.requestPeers(_channel, _peerListListener);
}
}
else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
}
else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}
}
//-------------------------------------------------------------------------------
}
(对潜在的混乱和浪费代码道歉 - 我一直在尝试尝试获得结果)
帮助很大。我的猜测是peerListListener在错误的地方。我的Android知识很少。
提前致谢。
答案 0 :(得分:3)
我可以看到你在构建 WiFiDirectBroadcastReceiver
时做了初步发现然后在 Activity.onResume()中首先构建 WiFiDirectBroadcastReceiver ,然后 registerReceiver()。
@Override
public void onResume() {
super.onResume();
_broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this, _peerListListener);
registerReceiver(_broadcastReceiver, _intentFilter);
}
您没有看到响应的问题可能是因为序列。尝试将 discoverPeers()移动到 Activity.onResume()应解决问题。
答案 1 :(得分:0)
您错过了对DiscoverPeers的电话:
要开始使用Wi-Fi Direct搜索附近的设备,请致电 discoverPeers()。
您希望初始发现发生时的特定应用程序逻辑将决定代码中的位置。在SDK 17的API示例中,当从菜单中单击选项时会发生这种情况。这可以很容易地移动到onResume方法(这将在每次恢复活动时启动,或者在屏幕上的按钮的单击监听器中启动。
为了将来的故障排除,我建议将WiFiDirectBroadcastReceiver类保留在Activity的类中(除非您需要重用代码)以便于回调使用 - 您可以直接从内部类访问外部类的方法并使其成为可能更容易排除故障。
答案 2 :(得分:0)
尝试更改此内容:
_manager.requestPeers(_channel, _peerListListener);
到此:
_manager.requestPeers(_channel, _activityRef._peerListListener);
答案 3 :(得分:0)
自2011年10月起,Google正在提供Wifi Direct示例:https://android.googlesource.com/platform/development/+/master/samples/WiFiDirectDemo
我添加了这个链接,因为我花了一段时间才找到它(原始链接是Wifi P2P Demo,而不是WiFiDirectDemo)。