Code example for WifiP2pManager

Methods: addLocalService, addServiceRequest, discoverServices, initialize, setDnsSdResponseListeners

3
        return Service.START_NOT_STICKY;
    } 
 
    public void register(){ 
        mManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        mChannel = mManager.initialize(this, getMainLooper(), null);
        Map record = new HashMap();
        record.put("listenport", String.valueOf(0));
        record.put("buddyname", "John Doe" + (int) (Math.random() * 1000));
        record.put("available", "visible");
 
        // Service information.  Pass it an instance name, service type 
        // _protocol._transportlayer , and the map containing 
        // information other devices will want once they connect to this one. 
        WifiP2pDnsSdServiceInfo serviceInfo =
                WifiP2pDnsSdServiceInfo.newInstance("_nebula", "_ftp._tcp", record);
 
        // Add the local service, sending the service info, network channel, 
        // and listener that will be used to indicate success or failure of 
        // the request. 
        mManager.addLocalService(mChannel, serviceInfo, new WifiP2pManager.ActionListener() {
            @Override 
            public void onSuccess() { 
                // Command successful! Code isn't necessarily needed here, 
                // Unless you want to update the UI or add logging statements. 
            } 
 
            @Override 
            public void onFailure(int arg0) {
                // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY 
            } 
        }); 
    } 
 
    public void discover(){ 
        WifiP2pManager.DnsSdTxtRecordListener txtListener = new WifiP2pManager.DnsSdTxtRecordListener() {
            @Override 
            public void onDnsSdTxtRecordAvailable( 
                    String fullDomain, Map record, WifiP2pDevice device) {
                Log.d("BM", "DnsSdTxtRecord available -" + record.toString());
                //buddies.put(device.deviceAddress, record.get("buddyname")); 
            } 
        }; 
 
        WifiP2pManager.DnsSdServiceResponseListener servListener = new WifiP2pManager.DnsSdServiceResponseListener() {
            @Override 
            public void onDnsSdServiceAvailable(String instanceName, String registrationType,
                                                WifiP2pDevice resourceType) {
                Log.d("BM", "Device: " + resourceType.deviceName);
                Log.d("BM", "Address: " + resourceType.deviceAddress);
 
                Log.d("BM", "onBonjourServiceAvailable " + instanceName);
 
 
                /*WifiP2pConfig config = new WifiP2pConfig(); 
                config.deviceAddress = resourceType.deviceAddress; 
                config.wps.setup = WpsInfo.PBC; 
 
                mManager.connect(mChannel, config, new WifiP2pManager.ActionListener() { 
 
                    @Override 
                    public void onSuccess() { 
                        Log.d("BM", "Connected"); 
                    } 
 
                    @Override 
                    public void onFailure(int reason) { 
                        Log.d("BM", "Failure"); 
                    } 
                });*/ 
            } 
        }; 
 
        mManager.setDnsSdResponseListeners(mChannel, servListener, txtListener);
 
        WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
        mManager.addServiceRequest(mChannel,
                serviceRequest,
                new WifiP2pManager.ActionListener() {
                    @Override 
                    public void onSuccess() { 
                        // Success! 
                    } 
 
                    @Override 
                    public void onFailure(int code) {
                        // Command failed.  Check for P2P_UNSUPPORTED, ERROR, or BUSY 
                    } 
                }); 
 
        mManager.discoverServices(mChannel, new WifiP2pManager.ActionListener() {
 
            @Override 
            public void onSuccess() { 
                // Success! 
            }