我正在尝试使用两个具有对等 WiFi 连接的 Android 设备来构建服务器-客户端架构。我在客户端和服务器之间有明显的区别,所以我试图删掉不必要的代码。使用来自http://developer.android.com/training/connect-devices-wireless/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 回调。有什么想法我可能出错了吗?