gpt4 book ai didi

java - Android/Java WiFi 直接对端列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:41 26 4
gpt4 key购买 nike

哟,

我正在尝试让 Android 设备(它是 Nexus 7,尽管我无法想象这有多大关系)通过 WiFi Direct 的奇迹与 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 Intent 永远不会广播,所以当我寻找同伴时,它开始搜索,说一切都很好,但没有给出给我一份结果 list ……我猜应该是这样

我一天中的大部分时间都在与这个问题搏斗,我得了花粉症,让生活变得更加甜蜜

代码:

Activity :

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 知识非常少。

提前致谢。

最佳答案

我可以看到你在构造你的WiFiDirectBroadcastReceiver时做了一个初步的发现

然后在您的 Activity.onResume() 中,您首先构建 WiFiDirectBroadcastReceiver,然后构建 registerReceiver()

@Override
public void onResume() {
super.onResume();
_broadcastReceiver = new WiFiDirectBroadcastReceiver(_manager, _channel, this, _peerListListener);
registerReceiver(_broadcastReceiver, _intentFilter);
}

您没有看到响应返回的问题可能是因为序列。尝试将 discoverPeers() 移动到 Activity.onResume() 应该可以解决问题.

关于java - Android/Java WiFi 直接对端列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17530885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com