找不到附近的 WiFi-Direct 设备,显示“未找到设备!"; [英] Can't find nearby WiFi- Direct devices showing "No Device Found!"

查看:35
本文介绍了找不到附近的 WiFi-Direct 设备,显示“未找到设备!";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WifiDirect 相互发现两台设备.设备分别运行 Android 8.0 和 9.0 版.但没能成功.

I am trying to Discover two devices each other using WifiDirect. Devices are running Android version 8.0 and 9.0 respectively. But not able to succeed.

我第一次点击两台设备上的发现"按钮时,都会等待 2-3 秒并显示没有找到 Toast 设备.我第二次尝试在一秒钟内发现两者都显示 No device found Toast.

First time I click on Discover button on both devices both waits for 2-3 seconds and shows No device found Toast. Second time I try to discover on both within a second both show up the No device found Toast.

MainActivity.java

package com.example.prototypewfp2p;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    Button btnOnOff, btnDiscover, btnSend;
    ListView listView;
    TextView read_msg_box, ConnectoinStatus;
    EditText writeMsg;
    WifiManager wifiManager;
    WifiP2pManager mMangaer;
    WifiP2pManager.Channel mChannel;
    BroadcastReceiver mReceiver;
    IntentFilter mIntentFilter;

    List<WifiP2pDevice> peers = new ArrayList<WifiP2pDevice>();
    String[] deviceNameArray;
    WifiP2pDevice[] deviceArray;

    //  Permissions Related
    public String[] PermissionsList = {Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE
            , Manifest.permission.CHANGE_NETWORK_STATE, Manifest.permission.ACCESS_FINE_LOCATION};
    public int permsRequestCode = 200;


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

//      Permissions

        for (int i = 0; i < PermissionsList.length; i++) {
            if (ContextCompat.checkSelfPermission(MainActivity.this, PermissionsList[i]) == PackageManager.PERMISSION_DENIED) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                    requestPermissions(PermissionsList, permsRequestCode);

                }
            } else {
                Toast.makeText(MainActivity.this, "Permissions Granted Already!", Toast.LENGTH_SHORT).show();
            }
        }


        //Calling Functions Declared below
        initialWork();
        exqListener();
    }

    //Permission (Override stuff)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (permsRequestCode) {
            case 200:
                if(grantResults.length > 0 &&  grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this,"Permission[1] granted!",Toast.LENGTH_LONG);
                }
                else{Toast.makeText(this,"Permission[1] Denied!",Toast.LENGTH_LONG);}
                if(grantResults.length > 0 &&  grantResults[2] == PackageManager.PERMISSION_GRANTED){Log.i("myTag","Permission 3 granted");}
                if(grantResults.length > 0 &&  grantResults[3] == PackageManager.PERMISSION_GRANTED){Log.i("myTag","Permission 4 granted");}
                if(grantResults.length > 0 &&  grantResults[4] == PackageManager.PERMISSION_GRANTED){Log.i("myTag","Permission 5 granted");}
                if(grantResults.length > 0 &&  grantResults[5] == PackageManager.PERMISSION_GRANTED){Log.i("myTag","Permission 6 granted");}
                if(grantResults.length > 0 &&  grantResults[1] == PackageManager.PERMISSION_GRANTED){Log.i("myTag","Permission 2 granted");}

//                boolean first = grantResults[0] == PackageManager.PERMISSION_GRANTED;
//                boolean second = grantResults[1] == PackageManager.PERMISSION_GRANTED;
//                boolean third = grantResults[2] == PackageManager.PERMISSION_GRANTED;
//                boolean fourth = grantResults[3] == PackageManager.PERMISSION_GRANTED;
//                boolean fifth = grantResults[4] == PackageManager.PERMISSION_GRANTED;
//                boolean sixth = grantResults[5] == PackageManager.PERMISSION_GRANTED;
                break;
        }
    }


    private void exqListener() {
        btnOnOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wifiManager.isWifiEnabled()) {
                    wifiManager.setWifiEnabled(false);
//                    Toast.makeText(MainActivity.this,"Wifi is turned OFF",Toast.LENGTH_LONG).show();
                } else {
                    wifiManager.setWifiEnabled(true);
//                    Toast.makeText(MainActivity.this,"Wifi is turned ON",Toast.LENGTH_LONG).show();
                }

            }
        });

        btnDiscover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
                    }
                    return;
                }
                mMangaer.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {
                        ConnectoinStatus.setText("Discovery Started!");
                    }

                    @Override
                    public void onFailure(int reason) {
                        ConnectoinStatus.setText("Could not start Discovery");
                    }
                });
            }
        });
    }


    private void initialWork() {
        btnOnOff = (Button) findViewById(R.id.onOff);
        btnDiscover = (Button) findViewById(R.id.discover);
        btnSend = (Button) findViewById(R.id.sendButton);
        listView = (ListView) findViewById(R.id.peerListView);
        read_msg_box = (TextView) findViewById(R.id.readMsg);
        ConnectoinStatus = (TextView) findViewById(R.id.connectionStatus);
        writeMsg = (EditText) findViewById(R.id.writeMsg);

        wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        mMangaer = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mMangaer.initialize(this, getMainLooper(),null);
        mReceiver = new WiFiDirectBroadcastReciever(mMangaer,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);
    }

    WifiP2pManager.PeerListListener peerListListener = new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList peerList) {
            if(!peerList.getDeviceList().equals(peers) ){
                peers.clear();
                peers.addAll(peerList.getDeviceList());

                deviceNameArray =  new String[peerList.getDeviceList().size()];     //Size of deviceNameArray initialized
                deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];   //Size of deviceArray initialized
                int index = 0;

                //assigning device name and device to 'deviceNameArray' and 'deviceArray'
                for (WifiP2pDevice device: peerList.getDeviceList()){
                    deviceNameArray[index] = device.deviceName;
                    deviceArray[index] = device;
                    index++;
                }

                ArrayAdapter<String> adapter= new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,deviceNameArray);
                listView.setAdapter(adapter);
            }
            if(peers.size()==0){
                Toast.makeText(MainActivity.this,"No Device Found!",Toast.LENGTH_LONG).show();
                return;
            }

        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mReceiver,mIntentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }
}

WiFiDirectBroadcastReciever.java

  package com.example.prototypewfp2p;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import static android.media.audiofx.Visualizer.STATE_ENABLED;

public class WiFiDirectBroadcastReciever extends BroadcastReceiver {

    private WifiP2pManager mMangaer;
    private WifiP2pManager.Channel mChannel;
    private MainActivity mActivity;

    public WiFiDirectBroadcastReciever(WifiP2pManager mMangaer, WifiP2pManager.Channel mChannel, MainActivity mActivity) {

        this.mMangaer = mMangaer;
        this.mActivity = mActivity;
        this.mChannel = mChannel;
    }

    @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) {
                Toast.makeText(context, "Wifi is On", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Wifi is Off", Toast.LENGTH_SHORT).show();
            }
        } else if (WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION.equals(action)) {
            if (mMangaer != null) {

                if (ActivityCompat.checkSelfPermission(context, 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.

                    Toast.makeText(context,"Acces Location (BroadcastRec) Not granted",Toast.LENGTH_SHORT).show();
                }
                mMangaer.requestPeers(mChannel, mActivity.peerListListener);
            }
        }
        else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){

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

        }


    }
}

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/onOff"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="24dp"
        android:layout_marginTop="47dp"
        android:text="Wifi On/Off"
        android:textSize="13dp" />

    <Button
        android:id="@+id/discover"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/onOff"
        android:layout_alignBottom="@+id/onOff"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="0dp"
        android:text="discover" />

    <ListView
        android:id="@+id/peerListView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/onOff"
        android:layout_marginTop="25dp"
        android:background="@android:color/holo_orange_light" />

    <TextView
        android:id="@+id/readMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/peerListView"
        android:layout_marginTop="31dp"
        android:text="Message"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="italic" />

    <EditText
        android:id="@+id/writeMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:ems="10"
        android:inputType="textPersonName"
        android:layout_toStartOf="@+id/sendButton" />

    <Button
        android:id="@+id/sendButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:text="Send" />

    <TextView
        android:id="@+id/connectionStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:text="Connection Status"
        android:textAlignment="center"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="18sp"
        android:textStyle="italic" />



</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.prototypewfp2p">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Prototypewfp2p">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

推荐答案

IDK 为什么 但是您必须在两台设备上都启用 gps(位置).我还使用 android 8 版设备和 android 10 设备 gps 进行了测试,两者都运行良好.

IDK why But you have to enable gps (Location) on both devices. I have also tested with android version 8 device and android 10 device gps enabled Both are working perfectly fine.

这篇关于找不到附近的 WiFi-Direct 设备,显示“未找到设备!";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆