我正在努力寻找附近的同龄人进行连接,经理。DiscoveryPeers显示出成功,但未能倾听PeerListener
活动代码
public class MainActivity extends AppCompatActivity {
Button btn;
WifiP2pManager manager;
WifiP2pManager.Channel channel;
BroadcastReceiver receiver;
IntentFilter intentFilter;
public ArrayList<WifiP2pDevice> peerlist;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.wificonnections);
peerlist=new ArrayList<>();
checkpermission();
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
channel = manager.initialize(this, getMainLooper(), null);
receiver = new WifiBroadCastReceiver(manager, channel,MainActivity.this);
intentFilter = new IntentFilter();
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);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(MainActivity.this," aditya rocks",Toast.LENGTH_SHORT).show();
//Log.d("permission","GRANTED");
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
}
});
manager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
Log.d("discoveries started","working");
}
@Override
public void onFailure(int reason) {
Log.d("Discoveries Failed","Not working");
Log.d("reason for failure",String.valueOf(reason));
}
});
@Override
protected void onResume() {
super.onResume();
registerReceiver(receiver,intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
WifiP2pManager.PeerListListener peerListListener=new WifiP2pManager.PeerListListener() {
@Override
public void onPeersAvailable(WifiP2pDeviceList peers) {
if(!peers.getDeviceList().equals(peerlist)){
peerlist.clear();
peerlist.addAll(peers.getDeviceList());
String []devicename=new String[peerlist.size()];
int index=0;
for(WifiP2pDevice device:peerlist)
{
devicename[index]=device.deviceName;
}
for (String s : devicename) {
Log.d("device name : ", s);
}
}
if(peerlist.size()==0)
{
Log.d("No device available"," check device");
}
}
};
@RequiresApi(api = Build.VERSION_CODES.M)
public void checkpermission()
{
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
{
Log.d("permission","GRANTED");
}
else{
Log.d("permission","GRANTED this was called");
ActivityCompat.requestPermissions(MainActivity.this,
new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_WIFI_STATE
,Manifest.permission.CHANGE_WIFI_STATE,Manifest.permission.ACCESS_COARSE_LOCATION
,Manifest.permission.WRITE_EXTERNAL_STORAGE
,Manifest.permission.INTERNET},2);
}
}
}
广播接收机类别代码
public class WifiBroadCastReceiver extends BroadcastReceiver {
WifiP2pManager manager;
WifiP2pManager.Channel channel;
MainActivity activity;
public WifiBroadCastReceiver(WifiP2pManager manager, WifiP2pManager.Channel channel, MainActivity activity) {
super();
this.manager = manager;
this.channel = channel;
this.activity = activity;
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
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) {
Log.d("wifi", "ENABLED");
} else {
Log.d("WIFI", "DISABLED");
}
} else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
//here you have to call wifip2pmanager.requestpeer() to get current peer list
if (manager != null) {
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
manager.requestPeers(channel, activity.peerListListener);
}
else {
manager.requestPeers(channel, activity.peerListListener);
}
}
else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action))
{
// you will be notified about connection or disconnection
}
else if(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action))
{
// respond to this device statechange....
}
}
}
我试图找到PeerListener的用法,但它说从未使用过。因此,初始化存在一些问题。我认为是这样。同样,在wifi状态之后,开始在启用和禁用状态之间切换。
Log output
D/permission: GRANTED
D/discoveries started: working
D/wifi: ENABLED
D/wifi: ENABLED
D/WIFI: DISABLED
D/wifi: ENABLED
D/WIFI: DISABLED
D/wifi: ENABLED
D/WIFI: DISABLED
D/wifi: ENABLED
D/WIFI: DISABLED
D/wifi: ENABLED
暂无答案!
目前还没有任何答案,快来回答吧!