编程语言
首页 > 编程语言> > java-如何从WifiP2pDeviceList获取wifi直接设备名称

java-如何从WifiP2pDeviceList获取wifi直接设备名称

作者:互联网

执行请求对等方时,我想获得wi-fi直接名称,这是我的代码:

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

    Log.d(tag, "success discover the peers ");

    if (mManager != null) {
        mManager.requestPeers(mChannel, new WifiP2pManager.PeerListListener() {

            @Override
            public void onPeersAvailable(WifiP2pDeviceList peers) {
                // TODO Auto-generated method stub

                if (peers != null) {
                    if (device.deviceName.equals("ABC")) {
                        Log.d(tag, "found device!!! ");

                        Toast.makeText(getApplicationContext(), "FOUND!!", Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
    } else {
        Log.d(tag, "mMaganger == null");
    }
}

我想从对等方列表中获取deviceName,以便可以找到一个名为“ ABC”的设备.任何想法?

解决方法:

如果您想要其他设备名称:

wifiP2pManager.requestPeers(wifiChannel, new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList wifiP2pDeviceList) {

            for (WifiP2pDevice device : wifiP2pDeviceList.getDeviceList())
            {
                if (device.deviceName.equals("ABC")) Log.d(tag, "found device!!! ");
                // device.deviceName
            }
        }
    });

如果您想要设备名称,请在接收器中获取它:

if (action.equals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION))
{
    WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
    // device.deviceName
}

如果要更改设备名称:

try {
    Method method = wifiP2pManager.getClass().getMethod("setDeviceName",
        WifiP2pManager.Channel.class, String.class, WifiP2pManager.ActionListener.class);

    method.invoke(wifiP2pManager, wifiChannel, "New Device Name", new WifiP2pManager.ActionListener() {
        public void onSuccess() {}

        public void onFailure(int reason) {}
    });
} catch (Exception e)   {}

标签:wifi-direct,java,android
来源: https://codeday.me/bug/20191028/1951598.html