gpt4 book ai didi

android - 如何在 Android WifiP2pManager onPeersAvailable() 中检索对等列表?

转载 作者:行者123 更新时间:2023-11-29 17:25:47 27 4
gpt4 key购买 nike

我试图通过 WifiP2pManager 访问 Wifdirect 功能,我试图通过我的 Activity 类中的 onPeersAvailable() 检索对等列表,但无法实现。请建议我如何检索和显示可用的对等点以及在何处编码以进行连接。我的方法如下。

Log.d 没有在这个方法中打印它的值

@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
peers.clear();
peers.addAll(peerList.getDeviceList());
Log.e("Peer Size-* * * *", String.valueOf(peers.size())+peerList.toString());

//((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
if (peers.size() == 0) {
Log.d(MainActivity.TAG, "No devices found");
Log.e("Peer Size-","No Device Found * * * *");
return;
}

}

主要 Activity

public class MainActivity extends AppCompatActivity implements WifiP2pManager.ChannelListener,WifiP2pManager.PeerListListener {

WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;
BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;
public static final String TAG = "MainActivity";

private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
ProgressDialog progressDialog = null;
View mContentView = null;
private WifiP2pDevice device;

private WifiP2pManager manager;
private boolean isWifiP2pEnabled = false;
private boolean retryChannel = false;

private final IntentFilter intentFilter = new IntentFilter();


TextView value;

/*
public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled) {
this.isWifiP2pEnabled = isWifiP2pEnabled;
}
*/

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

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

mReceiver = new WiFiDirectBroadcastReceiver(mManager, mChannel, this);




mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);


}


@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();


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

@Override
public void onSuccess() {
Toast.makeText(MainActivity.this, "Discovery Initiated",
Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(int reasonCode) {
Toast.makeText(MainActivity.this, "Discovery Failed : " + reasonCode,
Toast.LENGTH_SHORT).show();
}
});
/*
Button clk = (Button) findViewById(R.id.showDetails);
clk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
TextView view2 = (TextView) findViewById(R.id.value);
view2.setText(device.deviceAddress);

}
});
*/

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}


return super.onOptionsItemSelected(item);
}




@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
peers.clear();
peers.addAll(peerList.getDeviceList());
Log.e("Peer Size-* * * *", String.valueOf(peers.size())+peerList.toString());

//((WiFiPeerListAdapter) getListAdapter()).notifyDataSetChanged();
if (peers.size() == 0) {
Log.d(MainActivity.TAG, "No devices found");
Log.e("Peer Size-","No Device Found * * * *");
return;
}

}

@Override
public void onChannelDisconnected() {

// we will try once more
if (manager != null && !retryChannel) {
Toast.makeText(this, "Channel lost. Trying again", Toast.LENGTH_LONG).show();
// resetData();
retryChannel = true;
manager.initialize(this, getMainLooper(), this);
} else {
Toast.makeText(this,
"Severe! Channel is probably lost premanently. Try Disable/Re-Enable P2P.",
Toast.LENGTH_LONG).show();
}

}
}

这是广播接收器

public class WiFiDirectBroadcastReceiver extends BroadcastReceiver  {

private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private MainActivity mActivity;
WifiP2pManager.PeerListListener myPeerListListener;

private List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
ProgressDialog progressDialog = null;
View mContentView = null;
private WifiP2pDevice device;


public WiFiDirectBroadcastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel,
MainActivity activity) {
super();
this.mManager = manager;
this.mChannel = channel;
this.mActivity = activity;
}

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


mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Toast.makeText(mActivity, "Channel Received", Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(int reasonCode) {
Toast.makeText(mActivity, "Channel Not Received", Toast.LENGTH_SHORT).show();
}
});

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) {
// Wifi Direct mode is enabled
//mActivity.setIsWifiP2pEnabled(true);
Log.e("WifiDIRECT-","Enabled");
} else {
//mActivity.setIsWifiP2pEnabled(false);
Log.e("WifiDIRECT-","Disabled");
// mActivity.resetData();

}
Log.d(MainActivity.TAG, "P2P state changed - " + state);

} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {

if (mManager != null) {
mManager.requestPeers(mChannel, myPeerListListener );
Log.e("Peer Changed:", "Detected Peer");
}

// Call WifiP2pManager.requestPeers() to get a list of current peers

} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
// Respond to new connection or disconnections
if (mManager == null) {
return;
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
// Respond to this device's wifi state changing
}

}



}

最佳答案

为什么在 WifiDirectBroadCastReceiveronReceive 方法中调用 discoverPeers?每次触发 Intent 时都不会继续调用 discoverPeers(您的 BroadcastReceiver 已注册)。

此外,这可能看起来很愚蠢,但是在您尝试查找它们时,您是否有任何其他设备正在运行 discoverPeers。在 android 中,唯一确认您的设备是否启用 Wifi-Direct 的方法是从它调用 discoverPeers(或转到 Settings->Wifi Settings->Wifi Direct->Scan)。

因此,只有当两个设备都调用各自的 discoverPeers 时,才会在 onPeersAvailable 中的彼此的 peerList 中找到它们。

关于android - 如何在 Android WifiP2pManager onPeersAvailable() 中检索对等列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34742120/

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