我想在执行请求对等时获取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”的那个。有什么想法吗?
答案 0 :(得分:2)
如果您想要其他设备名称:
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) {}
答案 1 :(得分:2)
您拥有WifiP2pDeviceList
(对等方)
在对等方上调用方法getDeviceList()
,返回P2p设备的集合(列表)Collection<WifiP2pDevice>
然后遍历一个WifiP2pDevice
的集合元素,它将包含你需要的deviceName
属性。
参考Android开发人员的this培训
希望你能得到它