Example #1
0
        void _bluettothServerCallback_NotificationSent(object sender, BleEventArgs e)
        {
            if (_count == 0)
            {
                _sw = new Stopwatch();
                _sw.Start();
            }

            if (_count < 1000)
            {
                var chars  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                var random = new Random();
                var result = new string(
                    Enumerable.Repeat(chars, 20)
                    .Select(s => s[random.Next(s.Length)])
                    .ToArray());

                //_characteristic.SetValue(result);

                //_bluetoothServer.NotifyCharacteristicChanged(e.Device, _characteristic, false);

                _count++;
            }
            else
            {
                _sw.Stop();
                Console.WriteLine("Sent # {0} notifcations. Total kb:{2}. Time {3}(s). Throughput {1} bytes/s", _count,
                                  _count * 20.0f / _sw.Elapsed.TotalSeconds, _count * 20 / 1000, _sw.Elapsed.TotalSeconds);
            }
        }
Example #2
0
        void _bluettothServerCallback_CharacteristicReadRequest(object sender, BleEventArgs e)
        {
            ASCIIEncoding ascii = new ASCIIEncoding();

            try
            {
                if (_readRequestCount == 5)
                {
                    _notificationsStarted = !_notificationsStarted;
                    _readRequestCount     = 0;
                }
                else
                {
                    _readRequestCount++;
                    Console.WriteLine("Read req {0}", _readRequestCount);
                    Console.WriteLine("Uuid req {0}", e.Characteristic.Uuid.ToString());

                    byte[] bytes = null;

                    if (e.Characteristic.Uuid.Equals(UUID.FromString(App.DeviceNickname)))
                    {
                        bytes = Encoding.UTF8.GetBytes(App.DeviceNicknameValue);
                    }
                    else if (e.Characteristic.Uuid.Equals(UUID.FromString(App.DeviceContact)))
                    {
                        bytes = Encoding.UTF8.GetBytes(App.DeviceContactValue);
                    }
                    else if (e.Characteristic.Uuid.Equals(UUID.FromString(App.DeviceInfo)))
                    {
                        bytes = Encoding.UTF8.GetBytes(App.DeviceInfoValue);
                    }
                    else if (e.Characteristic.Uuid.Equals(UUID.FromString(App.DeviceMAC)))
                    {
                        bytes = Encoding.UTF8.GetBytes(App.DeviceMACValue);
                    }
                    else if (e.Characteristic.Uuid.Equals(UUID.FromString(App.IsInfected)))
                    {
                        bytes = Encoding.UTF8.GetBytes(App.IsInfectedValue);
                    }
                    else
                    {
                        bytes = e.Characteristic.GetValue();
                    }

                    string decoded = ascii.GetString(bytes);

                    _bluetoothServer.SendResponse(e.Device, e.RequestId, GattStatus.Success, e.Offset,
                                                  bytes);

                    return;
                }
            }
            catch (System.Exception ex)
            {
            }

            //if (_notificationsStarted)
            //{
            //    _count = 0;

            //    Console.WriteLine("Started notifcations!");

            //    e.Characteristic.SetValue(String.Format("Start {0}!", _readRequestCount));
            //    _bluetoothServer.SendResponse(e.Device, e.RequestId, GattStatus.Success, e.Offset,
            //        e.Characteristic.GetValue());
            //    _bluetoothServer.NotifyCharacteristicChanged(e.Device, e.Characteristic, false);
            //}
            //else
            //{
            //    Console.WriteLine("Stopped notifcations!");
            //    e.Characteristic.SetValue(String.Format("Stop {0}!", _readRequestCount));
            //    _bluetoothServer.SendResponse(e.Device, e.RequestId, GattStatus.Success, e.Offset,
            //        e.Characteristic.GetValue());
            //    _bluetoothServer.NotifyCharacteristicChanged(e.Device, e.Characteristic, false);
            //}
        }
Example #3
0
 private void _bluettothServerCallback_CharacteristicWriteRequest(object sender, BleEventArgs e)
 {
 }