我已经实现了 Wifi P2P 用于使用 wifi 将数据从一个 android 手机传输到另一个。为此,发现服务是必要的。我不了解这个过程。P2p 本身,我们正在发现对等点并请求对等点,需要发现服务吗?
我发现发现是必要的并且总是得到内部错误,失败回调。需要帮助。
import java.util.HashMap;
import java.util.Map;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.net.wifi.p2p.WifiP2pManager.DnsSdServiceResponseListener;
import android.net.wifi.p2p.WifiP2pManager.DnsSdTxtRecordListener;
import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceInfo;
import android.net.wifi.p2p.nsd.WifiP2pDnsSdServiceRequest;
import android.net.wifi.p2p.nsd.WifiP2pServiceRequest;
import java.util.ArrayList;
import android.util.Log;
import android.widget.Toast;
public class WifiDiscoverservice {
WifiP2pManager mManager;
Channel mChannel;
WifiAdapter wifiadapter;
MainActivity activity;
Map<String, String> record;
final HashMap<String, String> buddies = new HashMap<String, String>();
public WifiDiscoverservice(WifiP2pManager mManager, Channel mChannel,MainActivity activity) {
this.activity = activity;
this.mChannel = mChannel;
this.mManager = mManager;
}
public void startRegistration() {
// Create a string map containing information about your service.
record = new HashMap<String, String>();
record.put("listenport", String.valueOf(8988));
record.put("buddyname", "John Doe" + (int) (Math.random() * 1000));
record.put("available", "visible");
WifiP2pDnsSdServiceInfo serviceInfo = WifiP2pDnsSdServiceInfo.newInstance("_test", "_presence._tcp", record);
WifiP2pServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
mManager.addLocalService(mChannel, serviceInfo, new ActionListener() {
public void onSuccess() {
System.out.println("success");
Toast.makeText(activity, "Local service success", Toast.LENGTH_LONG).show();
}
public void onFailure(int arg0) {
System.out.println(arg0);
Toast.makeText(activity, " Local service failure", Toast.LENGTH_LONG).show();
}
});
mManager.setDnsSdResponseListeners(mChannel, servListener, txtListener);
mManager.addServiceRequest(mChannel, serviceRequest,
new ActionListener() {
public void onSuccess() {
System.out.println("success");
Toast.makeText(activity, " servicerequest success", Toast.LENGTH_LONG).show();
}
public void onFailure(int code) {
System.out.println(code);
Toast.makeText(activity, " servicerequest failure", Toast.LENGTH_LONG).show();
}
});
mManager.discoverServices(mChannel, new ActionListener() {
@Override
public void onSuccess() {
System.out.println("success");
Toast.makeText(activity, "discovering success ", Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(int code) {
System.out.println(code);
Toast.makeText(activity, " discovering failure", Toast.LENGTH_LONG).show();
}
});
}
DnsSdTxtRecordListener txtListener = new DnsSdTxtRecordListener() {
@Override
public void onDnsSdTxtRecordAvailable(String fullDomain,
Map<String,String> record, WifiP2pDevice device) {
buddies.put(device.deviceAddress,
(String) record.get("buddyname"));
}
};
DnsSdServiceResponseListener servListener = new DnsSdServiceResponseListener() {
@Override
public void onDnsSdServiceAvailable(String instanceName,
String registrationType, WifiP2pDevice resourceType) {
resourceType.deviceName = buddies
.containsKey(resourceType.deviceAddress) ? buddies
.get(resourceType.deviceAddress)
: resourceType.deviceName;
ArrayList<WifiP2pDevice> list = new ArrayList<WifiP2pDevice>();
list.add(resourceType);
wifiadapter = new WifiAdapter(activity, list);
wifiadapter.notifyDataSetChanged();
}
};
}