gpt4 book ai didi

java - 如何解决通过 Wi-Fi (android API) 发现其他设备?

转载 作者:行者123 更新时间:2023-11-30 09:06:37 25 4
gpt4 key购买 nike

最近我按照 Developer.Android.Com 给出的步骤进行操作

不过看来我的运气还不错。我试图发现附近可用的同行,将它们放入 arraylist 但似乎运气不好。我什么都没得到。

我的实际 2 设备实际上是 android 4.1 和 4.2由于 Wifi Direct 基于 API 级别 14,即 android 4.0+,所以我认为我的真实设备不是问题。

我使用的概念是使用 1 个 Activity 和 1 个 BroadCast 接收器。

请看一下我的代码,是我写错了还是忘记了什么?

Chat1Activity.java

package com.example.androtut;

import java.util.ArrayList;

import others.MyBroadcastReceiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;

import android.content.IntentFilter;
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.Bundle;
import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class Chat1Activity extends Activity {

private final IntentFilter mintentFilter = new IntentFilter();
private Channel mChannel;
private WifiP2pManager mManager;
private BroadcastReceiver mReceiver;
private ArrayList peers = new ArrayList();
private PeerListListener myPeerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {

// Out with the old, in with the new.
peers.clear();
peers.addAll(peerList.getDeviceList());

// If an AdapterView is backed by this data, notify it
// of the change. For instance, if you have a ListView of available
// peers, trigger an update.

if (peers.size() == 0) {
Toast.makeText(getApplicationContext(),"Nothing found!", Toast.LENGTH_LONG).show();
return;
} else {
Toast.makeText(getApplicationContext(),"We've found " + peers.size() + " users", Toast.LENGTH_LONG).show();
}


}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat1);

//Toast.makeText(getApplicationContext(), "Wifi is okay", Toast.LENGTH_LONG).show();

prepareIntentFilter();

Button btn_startchat = (Button) findViewById(R.id.btn_startchat);

// Listening to News Feed button click
btn_startchat.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
// Launching News Feed Screen
detectPeers();
}
});

mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(this, getMainLooper(), null);
}


private void detectPeers(){

mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

@Override
public void onSuccess() {
// Code for when the discovery initiation is successful goes here.
// No services have actually been discovered yet, so this method
// can often be left blank. Code for peer discovery goes in the
// onReceive method, detailed below.
}

@Override
public void onFailure(int reasonCode) {
// Code for when the discovery initiation fails goes here.
// Alert the user that something went wrong.
}
});




}


@Override
protected void onResume() {
super.onResume();
mReceiver = new MyBroadcastReceiver(mManager, mChannel, this, myPeerListListener);
registerReceiver(mReceiver, mintentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}

private void prepareIntentFilter() {

mintentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
// Indicates a change in the list of available peers.
mintentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
// Indicates the state of Wi-Fi P2P connectivity has changed.
mintentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
// Indicates this device's details have changed.
mintentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

}


}

还有 MyBroadcastReceiver.java

package others;

import java.util.ArrayList;

import com.example.androtut.Chat1Activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
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.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

private WifiP2pManager mManager;
private Channel mChannel;
private Chat1Activity mActivity;
private PeerListListener myPeerListListener;



public MyBroadcastReceiver(WifiP2pManager manager, Channel channel,
Chat1Activity activity, PeerListListener obPeerList) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
this.myPeerListListener = obPeerList;
}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// Check to see if Wi-Fi is enabled and notify appropriate activity
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// Call WifiP2pManager.requestPeers() to get a list of current peers

if (mManager != null) {
mManager.requestPeers(mChannel, myPeerListListener);
}



} 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



}

if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi P2P is enabled
Toast.makeText(mActivity.getApplicationContext(),"Wifi is okay", Toast.LENGTH_LONG).show();

} else {
// Wi-Fi P2P is not enabled
Toast.makeText(mActivity.getApplicationContext(),
"Wifi is not okay", Toast.LENGTH_LONG).show();
}
}

}

}

最佳答案

我猜您没有将 PeerListListener 添加到您的 Activity 中。所以它不会回复。

你可以尝试在你的activity中实现PeerListListener

public class Chat1Activity extends Activity implements WifiP2pManager.PeerListListener

并在您的 Activity 中覆盖 onPeerAvailable 方法

 @Override
public void onPeersAvailable(WifiP2pDeviceList peers) {

//put your code here

}

并在您的 Receiver 中更改 requestPeers 方法

 if (mManager != null) {
mManager.requestPeers(mChannel, mActivity);
}

希望对您有所帮助!!

关于java - 如何解决通过 Wi-Fi (android API) 发现其他设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24357892/

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