Exemplo n.º 1
0
        public Device(Plugin.BluetoothLE.IDevice dev)
        {
            device = dev;
            Name   = dev.Name;

            chararacteristicListener = device.WhenAnyCharacteristicDiscovered().Subscribe(characteristic =>
            {
                if (characteristic.Uuid.ToString() == BleService.CharacteristictReadUuid)
                {
                    readCharacteristic = characteristic;
                    OnStateChanged(DeviceState.Connected);
                    RaisePropertyChanged(nameof(ConnectState));
                    if (readCharacteristic.CanNotify())
                    {
                        readCharacteristicObserver = characteristic.RegisterAndNotify(true).Subscribe(r =>
                        {
                            Read?.Invoke(r.Data);
                        },
                                                                                                      exception =>
                        {
                            //   Crashes.TrackError(exception);
                        });
                    }
                }

                if (characteristic.Uuid.ToString() == BleService.CharacteristicWriteUuid)
                {
                    if (characteristic.CanWrite())
                    {
                        writeCharacteristic = characteristic;
                        OnStateChanged(DeviceState.Connected);
                        RaisePropertyChanged(nameof(ConnectState));
                    }
                }
            }, exception =>
            {
                //  Crashes.TrackError(exception);
            });
            stateListener = device.WhenStatusChanged().Subscribe(f =>
            {
                OnStateChanged(ConnectState);
            });
        }
Exemplo n.º 2
0
        private void ConnectToServer()
        {
            server.Connect();
            Server_Name.Text = "Currently connected to " + server.Name;

            server.WhenAnyCharacteristicDiscovered().Subscribe(characteristic =>
            {
                //Debug.WriteLine("Char" + characteristic.Uuid);
            });

            //set up identity management
            server.GetKnownCharacteristics(StaticGuids.buzzerServiceGuid, StaticGuids.identityCharGuid).Subscribe(identityChar =>
            {
                Debug.WriteLine("Found Identity char");
                this.identityChar = identityChar;

                //writing nickname allows server to create association b/w uuid and nickname
                identityChar.Write(Encoding.UTF8.GetBytes(nickname)).Subscribe(write =>
                {
                    Debug.WriteLine("Successful identity write");
                }, error =>
                {
                    Debug.WriteLine(error.GetBaseException());
                });

                identityChar.Read().Subscribe(read =>
                {
                    addOne(LoadLabel);
                    clientGuid = Guid.Parse(Encoding.UTF8.GetString(read.Data));
                    Debug.WriteLine("Found own GUID: " + clientGuid.ToString());
                }, error =>
                {
                    Debug.WriteLine(error.ToString());
                });
            });

            //set up lock status
            server.GetKnownCharacteristics(StaticGuids.buzzerServiceGuid, StaticGuids.lockStatusGuid).Subscribe(lockStatusChar =>
            {
                Debug.WriteLine("Found Lock char");
                lockStatusChar.EnableNotifications();
                lockStatusChar.WhenNotificationReceived().Subscribe(result =>
                {
                    Debug.WriteLine("Notification recieved" + Encoding.UTF8.GetString(result.Data));
                    string reply       = Encoding.UTF8.GetString(result.Data);
                    char tempLockState = reply[0];
                    Debug.WriteLine(reply.Substring(1, 36));
                    Guid buzzDevice = Guid.Parse(reply.Substring(1, 36));

                    //TODO fix risk that client guid is not yet found
                    if (buzzDevice == clientGuid)
                    {
                        tempLockState = 'U';
                    }

                    lockStatus = tempLockState;
                    addOne(LoadLabel);
                });
            });

            //set up buzz char
            server.GetKnownCharacteristics(StaticGuids.buzzerServiceGuid, StaticGuids.buzzCharGuid).Subscribe(buzzerChar =>
            {
                Debug.WriteLine("Found buzzer char");
                this.buzzerChar = buzzerChar;
                addOne(LoadLabel);
            });

            //set up team info
            server.GetKnownCharacteristics(StaticGuids.buzzerServiceGuid, StaticGuids.teamCharGuid).Subscribe(teamChar =>
            {
                Debug.WriteLine("Team char found");
                teamChar.EnableNotifications();
                teamChar.WhenNotificationReceived().Subscribe(result =>
                {
                    Debug.WriteLine("Team info recieved");
                    string teamInfoString = Encoding.UTF8.GetString(result.Data);
                    string[] teamInfo     = teamInfoString.Split(teamSplitter, 2, StringSplitOptions.None);
                    foreach (string teamString in teamInfo)
                    {
                        string[] names = teamString.Split(nameSplitter, 50, StringSplitOptions.None);

                        if (!teams.ContainsKey(names[0]))
                        {
                            teams[names[0]] = new List <Guid>();
                        }
                        List <Guid> teamMembers = teams[names[0]];
                        for (int i = 1; i < names.Length; i++)
                        {
                            teamMembers.Add(Guid.Parse(names[i]));
                        }
                    }
                    addOne(LoadLabel);
                });
            });
        }