无法发现对等点 - Wifi Direct-

Posted

技术标签:

【中文标题】无法发现对等点 - Wifi Direct-【英文标题】:Cannot discover peers - Wifi Direct- 【发布时间】:2021-04-23 20:24:17 【问题描述】:

我想在同一本地网络上的两台安卓设备之间建立连接,并通过我的安卓应用在它们之间交换数据。 为了达到这个目的,我使用了带有广播接收器方法的 Wifi Direct Api,但由于某种原因,discoverPeers 方法没有被触发。

这是我的 MainActivity 的代码:

public class MainActivity extends AppCompatActivity 

private static MainActivity instance;

Button btnOnOff, btnDiscover, btnSend;
ListView listView;
TextView read_msg_box, connection_status;
EditText writeMessage;

WifiManager wifiManager;
WifiP2pManager mManager;
WifiP2pManager.Channel mChannel;

BroadcastReceiver mReceiver;
IntentFilter mIntentFilter;

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

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()];
            deviceArray = new WifiP2pDevice[peerList.getDeviceList().size()];

            int index = 0;
            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(getApplicationContext(), "No device Found", Toast.LENGTH_SHORT).show();
            return;
        
    
;

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

public static MainActivity getInstance() 
    return instance;



private void exqListener() 
    btnOnOff.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            if (wifiManager.isWifiEnabled() == true) 
                wifiManager.setWifiEnabled(false);
                btnOnOff.setText("Set To OFF ");
             else 
                wifiManager.setWifiEnabled(true);
                btnOnOff.setText("Set To ON");
            
        
    );

    btnDiscover.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            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;
            
            mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() 
                @Override
                public void onSuccess() 
                    connection_status.setText("Discovery Started");
                

                @Override
                public void onFailure(int reason) 
                    connection_status.setText("Discovery Starting Failed");
                
            );
        
    );


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

    wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    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);


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

这是 WifiDirectBroadcatReceiver 之一:

public class WifiDirectBroadcastReceiver extends BroadcastReceiver 
private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private AppCompatActivity mActivity;

public WifiDirectBroadcastReceiver(WifiP2pManager mManager, WifiP2pManager.Channel mChannel, AppCompatActivity mActivity) 
    this.mManager = mManager;
    this.mChannel = mChannel;
    this.mActivity = mActivity;


@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)) 
        Toast.makeText(context, "Dkhal hnaya bro", Toast.LENGTH_SHORT).show();
        if (mManager != 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.
                return;
            

            mManager.requestPeers(mChannel, MainActivity.getInstance().peerListListener);

            
     else if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)) 
        // Do something
     else if (WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION.equals(action)) 
        // Do something
    

这是 AndroidManifest.xml 中的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

根据代码,一旦我按下按钮 btnDiscover,就会有一个 textView 更改其文本,但是当我这样做时,什么也没有发生。

知道我在运行应用程序之前打开了我的位置服务,会出现什么错误?

欢迎所有其他解决这种情况的方法。

【问题讨论】:

【参考方案1】:

从 Android 11 开始,某些权限必须为 requested during runtime,无论清单中定义了什么。正如代码中的 cmets 所指出的,您应该在 MainActivity 中使用 requestPermissions 来执行此操作,并(可选)使用 onRequestPermissionsResult 对结果做出反应。

【讨论】:

以上是关于无法发现对等点 - Wifi Direct-的主要内容,如果未能解决你的问题,请参考以下文章

wifi-direct端连接到Android上的对等点?

在wifi-direct p2p连接中获取对等设备的IP地址[重复]

Android 将设备名称重命名为 wifi-direct

WiFi 直连:多久调用一次对等发现?

Wifi Direct P2P 无法在 Android 10+ 设备上运行,无法发现设备

Android Wi-Fi Direct P2P 连接多个设备