OnDnsSdTxtRecordAvailable is not fired some times

I am developing the the application for transferring data in offline.I used the IDnsSdTxtRecordListener for sensing the near by device.And implement the function OnDnsSdTxtRecordAvailable,this function is not called some times.I need to get the IP address of the nearby devices.

If anyone have an idea,Please suggest me ,How to solve/Overcome this issue.

Thanks in advance

Tagged:

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post more details about this question?

  • SabariShankarSabariShankar Member ✭✭
    edited January 29

    Step 1 - Register the service
    WifiP2pManager _manager;
    WifiManager wm = (WifiManager)GetSystemService(WifiService);
    if (wm.IsWifiEnabled)
    {
    _manager = (WifiP2pManager)GetSystemService(Android.Content.Context.WifiP2pService);
    _channel = _manager.Initialize(ApplicationContext, MainLooper, null);

                ssid = string.Empty;
                WifiInfo wifiInfo = wm.ConnectionInfo;
                if (wifiInfo.SupplicantState == SupplicantState.Completed)
                {
                    ssid = wifiInfo.SSID;
                }
                string Ipaddress = Formatter.FormatIpAddress(wm.ConnectionInfo.IpAddress);
                IDictionary<string, string> record = new Dictionary<string, string>();
                record.Add("IPAddress", Ipaddress);
                record.Add("ssid", ssid);
                record.Add("Test", "AndroidDevice");              
    
                _manager.ClearLocalServices(_channel, null);               
    
                myTimer.Enabled = true;
    
                serviceInfo = WifiP2pDnsSdServiceInfo.NewInstance(DefaultInstanceName, DefaultregistrationType, record);
                _manager.AddLocalService(_channel, serviceInfo, new MyActionListner(this,true, "Add Local Services", () => { }));
             }     
    

    Step 2 - Fire this function when detect the new device in the same WIFI
    here,txtRecordMap list have the values such as ip address,ssid and test value "AndroidDevice" Which was registered with local service

     public void OnDnsSdTxtRecordAvailable(string fullDomainName, IDictionary<string, string> txtRecordMap, WifiP2pDevice srcDevice)
        {
            if (fullDomainName.Contains(DefaultregistrationType) && txtRecordMap.ContainsKey("IPAddress") && txtRecordMap.ContainsKey("ssid")&& txtRecordMap.ContainsKey("AndroidDevice"))
            {
                if (txtRecordMap["ssid"] == ssid)
                {
                    foreach (WiFiP2pService wifiservice in DevicesList)
                    {
                        if (wifiservice.device.DeviceAddress == srcDevice.DeviceAddress)
                        {
                            wifiservice.IpAddress = txtRecordMap["IPAddress"];
                            return;
                        }
                    }
                    WiFiP2pService service = new WiFiP2pService();
                    service.DevicesName= txtRecordMap["ConnecteddevicesName"];
                    service.device = srcDevice;
                    service.IpAddress = txtRecordMap["IPAddress"];
                    DevicesList.Add(service);
                    ConnectedDevicesCount.Text ="Connected Devices: "+ DevicesList.Count.ToString();
                }
            }
        }  
    

    Issue is OnDnsSdTxtRecordAvailable function triggered slowly.

Sign In or Register to comment.