Ich versuche, 2 Geräte mit Wi-Fi Direct zu verbinden, indem ich der Google API folge. Ich möchte die Liste der Geräte in der ListView sehen, und es schlägt fehl. Was ist falsch an meinem Code?Android Wi-Fi P2P: Anzeigen von Peers in ListView
MainActivity
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public TextView search_result;
private WifiP2pManager manager;
public static final String TAG = "wifidirectdemo";
private final IntentFilter intentFilter = new IntentFilter();
private Channel channel;
private BroadcastReceiver receiver = null;
public String test;
private ArrayAdapter<String> WifiP2parrayAdpter;
private Button seachBt;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
search_result = (TextView)findViewById(R.id.result_search);
mListView = (ListView) findViewById(R.id.listView);
WifiP2parrayAdpter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item);
mListView.setAdapter(WifiP2parrayAdpter);
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
channel = manager.initialize(this, getMainLooper(), null);
}
seachBt = (Button) findViewById(R.id.go_bt);
seachBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search(v);
}
});
}
public void search (View view) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
search_result.setText("מחפש.......");
}
@Override
public void onFailure(int reason) {
search_result.setText("ארעה שגיאה. נסה שוב");
}
});
}
}
/* register the broadcast receiver with the intent values to be matched */
@Override
protected void onResume() {
super.onResume();
registerReceiver(receiver, intentFilter);
}
/* unregister the broadcast receiver */
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
public void dysplayPeers (WifiP2pDeviceList peerList) {
WifiP2parrayAdpter.clear();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
for (WifiP2pDevice peer : peerList.getDeviceList()) {
WifiP2parrayAdpter.add(peer.deviceName + "\n" + peer.deviceAddress);
search_result.setText(peer.deviceName);
}
}
}
}
WiFiDirectBroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pConfig;
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.util.Log;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver {
private WifiP2pManager manager;
private Channel channel;
private MainActivity activity;
private List<WifiP2pDevice> mPeers;
private List<WifiP2pConfig> mConfigs;
/**
* @param manager WifiP2pManager system service
* @param channel Wifi p2p channel
* @param activity activity associated with the receiver
*/
public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
MainActivity activity) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
/*
* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {
// UI update to indicate wifi p2p status.
int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1);
if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) {
// Wifi Direct mode is enabled
} else {
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
// request available peers from the wifi p2p manager. This is an
// asynchronous call and the calling activity is notified with a
// callback on PeerListListener.onPeersAvailable()
if (manager != null) {
Toast.makeText(activity, "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
mPeers = new ArrayList<WifiP2pDevice>();
mConfigs = new ArrayList<WifiP2pConfig>();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
final PeerListListener peerListListener = new PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peerList) {
mPeers.clear();
activity.dysplayPeers(peerList);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mPeers.addAll(peerList.getDeviceList());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
for (int i = 0; i < peerList.getDeviceList().size(); i++) {
WifiP2pConfig config = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
config = new WifiP2pConfig();
}
config.deviceAddress = mPeers.get(i).deviceAddress;
mConfigs.add(config);
}
}
}
};
}
} else if (WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) {
}
NetworkInfo networkInfo = (NetworkInfo) intent
.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
if (networkInfo.isConnected()) {
// we are connected with the other device, request connection
// info to find group owner IP
} else {
// It's a disconnect
}
} else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) {
}
}
}
Aktivität main: