哟,
我正在尝试让一个 Android 设备(它是一个 Nexus 7,尽管我无法想象这很重要)通过 WiFi Direct 的奇迹与 Raspberry Pi 进行通信。据说这是可能的,而且六个小时前似乎比沿着服务器 - 客户端路由更好的解决方案,但我遇到了问题
Android 开发者网站很好,因为它有两个指南:
http://developer.android.com/training/connect-devices-wireless/wifi-direct.html http://developer.android.com/guide/topics/connectivity/wifip2p.html
这很可爱,因为我没有大量的 Java 经验。但问题是......实际上在那个网站上很多,是他们认为直接的代码中的元素(即在哪里放置东西)经常丢失。
我已经非常密切地遵循了这两个指南,但我遇到了一个问题 - WIFI_P2P_PEERS_CHANGED_ACTION 意图从不广播,所以当我寻找同伴时,它开始搜索,说一切都很好,但没有给我一个列表结果......我猜它应该
一天中的大部分时间我都在与这个问题搏斗,因为我患上了一个很好的花粉症病例,让生活变得更加甜蜜
代码:
活动:
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知识非常少。
提前致谢。