gpt4 book ai didi

android - 如何让点对点 WiFi 服务发现工作?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:32 25 4
gpt4 key购买 nike

我正在尝试使用两台具有点对点 WiFi 连接的 Android 设备构建服务器-客户端架构。我在客户端和服务器之间有明确的区别,所以我试图删除不必要的代码。使用来自 http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html 的说明我有...

点对点服务

public class PeerToPeerService {
private static final Logger LOG = LoggerFactory.getLogger(PeerToPeerService.class);

private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private final Context mContext;
private final WifiP2pDnsSdServiceInfo mService;

public PeerToPeerService(Context context, String name) {
mContext = context;

Map<String, String> record = new HashMap<String, String>();
record.put(Constants.SERVICE_PROPERTY_NAME, name);
record.put(Constants.SERVICE_PROPERTY_PORT, "12345");

mService = WifiP2pDnsSdServiceInfo.newInstance(
"_test", "_presence._tcp", record);
}

public void start() {
mManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(mContext, mContext.getMainLooper(), null);

mManager.addLocalService(mChannel, mService, new ActionListener() {
@Override
public void onSuccess() {
LOG.info("Started Service");
}

@Override
public void onFailure(int error) {
LOG.warn("Failed to Start Service: {}", error);
}
});
}

public void stop() {
mManager.removeLocalService(mChannel, mService, null);
mManager = null;
LOG.info("Stopped Service");
}
}

点对点客户端

public class PeerToPeerClient {
private static final Logger LOG = LoggerFactory.getLogger(PeerToPeerClient.class);

private WifiP2pManager mManager;
private WifiP2pManager.Channel mChannel;
private final Context mContext;

public PeerToPeerClient(Context context) {
mContext = context;
}

public void findServices() {
mManager = (WifiP2pManager) mContext.getSystemService(Context.WIFI_P2P_SERVICE);
mChannel = mManager.initialize(mContext, mContext.getMainLooper(), null);

mManager.setDnsSdResponseListeners(mChannel,
new DnsSdServiceResponseListener() {
@Override
public void onDnsSdServiceAvailable(String instanceName,
String registrationType, WifiP2pDevice device) {
LOG.info("A");
LOG.info("Service Found: {}:{}", instanceName, registrationType);
}
},
new DnsSdTxtRecordListener() {
@Override
public void onDnsSdTxtRecordAvailable(String fullDomainName, Map<String, String> record,
WifiP2pDevice device) {
LOG.info("B");
LOG.info("{}:{} is {}", device.deviceName,
record.get(Constants.SERVICE_PROPERTY_PORT), record.get(Constants.SERVICE_PROPERTY_NAME));
}
});

WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
mManager.addServiceRequest(mChannel, serviceRequest,
new ActionListener() {
@Override
public void onSuccess() {
LOG.info("Added service discovery request");
}

@Override
public void onFailure(int error) {
LOG.info("Failed adding service discovery request: {}", error);
}
});

mManager.discoverServices(mChannel, new ActionListener() {
@Override
public void onSuccess() {
LOG.info("Service discovery initiated");
}

@Override
public void onFailure(int arg0) {
LOG.info("Service discovery failed");
}
});
}
}

每个单独的 ActionListener 都调用了 onSuccess,任何地方都没有失败。但是我从来没有得到任何 setDnsSdResponseListeners 回调。有什么我可能出错的想法吗?

最佳答案

我有一些在 Android 设备上使用 Wi-Fi Direct 的经验,我正在编写一个应用程序来临时连接设备并启用通过此链接聊天。

要在 Wi-Fi Direct 上被发现,两个设备都需要进行扫描,即。 mWifiP2pManager.discoverPeers()。奇怪的是,这也影响了 DnsTxtRecordServices 的发现。因此,我发现如果您在两台设备上启动 scanning(discoverPeers()) 然后启动 discoverPeers(),它就可以工作。

希望对您有所帮助! :)

关于android - 如何让点对点 WiFi 服务发现工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21722767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com