Java 如何从WifiP2pDeviceList获取wifi direct设备名称

Java 如何从WifiP2pDeviceList获取wifi direct设备名称,java,android,wifi-direct,Java,Android,Wifi Direct,我想在执行请求时获取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()

我想在执行请求时获取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
            }
        }
    });
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)   {}
如果您想在接收器中输入设备名称:

if (action.equals(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION))
{
    WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
    // device.deviceName
}
如果要更改设备名称:

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
            }
        }
    });
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)   {}

您拥有
WifiP2pDeviceList
的对象(对等)

在对等方上调用方法
getDeviceList()
,该方法返回P2p设备的集合(列表)
collection

然后遍历collection元素,它是一个
WifiP2pDevice
,它将包含
deviceName
属性,这正是您所需要的

参考android开发者的培训


希望您能够获得它

您所说的wifi直连名称是什么意思?你是说那个!!?因为设备名称是非常直接的获取…我编辑了我的qs,我想从对等列表中找到设备名称。但我不知道我的代码出了什么问题。我不能得到预期的结果。谢谢你的编辑。我想是的。你能不能再粘贴一些代码来准确地知道你的名字是不是搞错了?我指的是你试图获取设备名称的部分……我想我把获取自己的设备名称和其他设备名称弄糊涂了。“谢谢你指出这一点。”阿里解释得很清楚。我只是不想在他不尝试的情况下泄露代码,因为很容易找到:P:Pcheers@user2301281不客气。如果您满意,请标记答案。感谢you@user2301281我真的建议大家看看android SDK中的WiFi Direct演示。@Ali,是的。我之前已经看过并尝试过样本中的WifiDirectDemo。但是在广播示例中没有“onpeersavable()”部分。所以我不明白。很抱歉,我的编码能力很差非常感谢你澄清了这个想法=DI不能真正理解“遍历集合元素”。你能详细解释一下吗?对不起,我是新手。>@user2301281因为它包含设备的“列表”,所以需要遍历列表中的每个元素以获取其deviceName。请参阅@Ali回复库中的
循环。我想我现在更清楚了。我要试一试。谢谢你的建议。这真的救了我一天=DD