private void Callback_OnServicesAlreadyDiscovered(object sender, ServicesAlreadyDiscoveredEventArgs servicesDiscoveredEventArgs)
        {
            var services = Services.ToList();

            if (servicesDiscoveredEventArgs.Status == AndroidBluetooth.GattStatus.Success)
            {
                OnServicesDiscovered?.Invoke(this, new ServicesDiscoveredEventArgs(
                                                 Map(servicesDiscoveredEventArgs.Status), Services));
            }
            else
            {
                ;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Start looking for <see cref="NetworkService"/> on the local network
        /// </summary>
        /// <param name="timeoutSeconds"></param>
        public async void StartServicesDiscoveryAsync(int timeoutSeconds)
        {
            var result = new List <NetworkService>();



            Timer timer = null;

            timer = new Timer(TimerElapsed, null, 0, timeoutSeconds * 100);

            _completed = false;

            void TimerElapsed(object state)
            {
                if (!_completed)
                {
                    _completed = true;
                    OnServiceDiscoveryCompleted?.Invoke(this, result);
                }
                timer.Dispose();
            }

            _startDate = DateTime.Now;
            _client    = new UdpClient();
            await _client.SendAsync(new byte[] { }, 0, new IPEndPoint(IPAddress.Broadcast, 12100));

            while (DateTime.Now - _startDate < TimeSpan.FromSeconds(timeoutSeconds))
            {
                var res = await _client.ReceiveAsync();

                var resStr = Encoding.UTF8.GetString(res.Buffer);

                result.AddRange(JsonConvert.DeserializeObject <NetworkService[]>(resStr));
                foreach (var item in result)
                {
                    if (item.AccessInfo == null)
                    {
                        item.AccessInfo = new NetowkrServiceAccessInfo();
                    }

                    item.AccessInfo.IP = res.RemoteEndPoint.Address.ToString();
                }
                OnServicesDiscovered?.Invoke(this, result);
            }
            _completed = true;
            OnServiceDiscoveryCompleted?.Invoke(this, result);
        }