Exemplo n.º 1
0
 private void CreateBuzzChar()
 {
     buzzChar = buzzerService.AddCharacteristic(StaticGuids.buzzCharGuid,
                                                CharacteristicProperties.Write, GattPermissions.Read | GattPermissions.Write);
     buzzChar.WhenWriteReceived().Subscribe(writeRequest =>
     {
         if (lockStatus == "O")
         {
             buzzDevice       = writeRequest.Device.Uuid;
             string buzzName  = deviceIdentities[buzzDevice];
             Buzz_Player.Text = buzzName;
             lockStatus       = "C";
             lockStatusChar.Broadcast(Encoding.UTF8.GetBytes(lockStatus + buzzDevice.ToString() + buzzName));
         }
     });
 }
Exemplo n.º 2
0
        public void Send_Message(BluetoothMessageTypes mess, byte[] data = null)
        {
            int datLen = 0;

            if (data != null)
            {
                datLen = data.Length;
            }

            byte[] broadcast = new byte[datLen + NUM_BYTES_IN_MESSAGE_TYPE];
            byte[] messBytes = Mess_To_Byte(mess);

            for (int i = 0; i < NUM_BYTES_IN_MESSAGE_TYPE; ++i)
            {
                broadcast[i] = messBytes[i];
            }

            for (int i = 0; i < datLen; ++i)
            {
                broadcast[i + NUM_BYTES_IN_MESSAGE_TYPE] = messBytes[i];
            }

            if (notifyCharacteristic != null)
            {
                notifyCharacteristic.Broadcast(broadcast);
            }
        }
Exemplo n.º 3
0
        public void SendToClients(string text)
        {
            var bytes = Encoding.UTF8.GetBytes(text);

            //Can specify specific devices as an optional paramter
            _serverReadWriteCharacteristic.Broadcast(bytes);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 发送鼠标事件
        /// </summary>
        /// <param name="mouseEvent"></param>
        /// <param name="scrollInfo"></param>
        public void SendMouseEvent(MouseEvent mouseEvent, int scrollInfo)
        {
            byte[] bytesToSend     = new byte[2 * sizeof(int)];
            var    mouseEventBytes = BitConverter.GetBytes((int)mouseEvent);
            var    scrollInfoBytes = BitConverter.GetBytes(scrollInfo);

            mouseEventBytes.CopyTo(bytesToSend, 0);
            scrollInfoBytes.CopyTo(bytesToSend, sizeof(int));
            Parallel.ForEach(mouseEventCharacteristic.SubscribedDevices, device => mouseEventCharacteristic.Broadcast(bytesToSend, device));
        }
Exemplo n.º 5
0
        private void CreateTeamChar()
        {
            teamChar = buzzerService.AddCharacteristic(StaticGuids.teamCharGuid,
                                                       CharacteristicProperties.Indicate | CharacteristicProperties.Notify,
                                                       GattPermissions.Read | GattPermissions.Write
                                                       );

            //when a new device is added give all team information
            teamChar.WhenDeviceSubscriptionChanged().Subscribe(e =>
            {
                teamChar.Broadcast(teamInfo(), e.Device);
            });
        }
Exemplo n.º 6
0
        private void CreateLockStatusChar()
        {
            //create characteristic to inform connected devices about the lock out
            //status of the buzzer unit

            //lockStatusGuid = Guid.NewGuid();
            lockStatusChar = buzzerService.AddCharacteristic(StaticGuids.lockStatusGuid,
                                                             CharacteristicProperties.Indicate | CharacteristicProperties.Notify,
                                                             GattPermissions.Read | GattPermissions.Write
                                                             );

            lockStatusChar.WhenDeviceSubscriptionChanged().Subscribe(e =>
            {
                Debug.WriteLine("Got subscriptions: " + e.ToString());
                Debug.WriteLine("UUID" + lockStatusChar.Uuid.ToString());
                //TODO factor this out into a method
                lockStatusChar.Broadcast(Encoding.UTF8.GetBytes(lockStatus + buzzDevice.ToString()));
            });
        }
Exemplo n.º 7
0
        public void SendMousePostion(CursorPoint cursorPoint)
        {
            byte[] posDataBytes = new byte[2 * sizeof(double)];
            var    xBytes       = BitConverter.GetBytes(cursorPoint.X);
            var    yBytes       = BitConverter.GetBytes(cursorPoint.Y);

            xBytes.CopyTo(posDataBytes, 0);
            yBytes.CopyTo(posDataBytes, sizeof(double));
            try
            {
                Parallel.ForEach(mouseMotionCharacteristic.SubscribedDevices, device => mouseMotionCharacteristic.Broadcast(posDataBytes, device));
            }
            catch
            {
                ;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 发送目录请求
        /// </summary>
        /// <param name="path"></param>
        public void SendDictionaryRequest(string path)
        {
            var pathBytes = Encoding.UTF8.GetBytes(path);

            Parallel.ForEach(fileManageCharacteristic.SubscribedDevices, device => fileManageCharacteristic.Broadcast(pathBytes, device));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 发送键盘输入的字符串
        /// </summary>
        /// <param name="stringToSend"></param>
        public void SendString(string stringToSend)
        {
            var data = Encoding.UTF8.GetBytes(stringToSend);

            Parallel.ForEach(stringOperationCharacteristic.SubscribedDevices, device => stringOperationCharacteristic.Broadcast(data, device));
        }
Exemplo n.º 10
0
 /// <summary>
 /// 发送按键控制
 /// </summary>
 /// <param name="data"></param>
 public void SendKeyboardControl(byte[] data)
 {
     Parallel.ForEach(keyboardOpCharacteristic.SubscribedDevices, device => keyboardOpCharacteristic.Broadcast(data, device));
 }
Exemplo n.º 11
0
 private void updateTeams(string team, Guid guid)
 {
     teamChar.Broadcast(Encoding.UTF8.GetBytes(team + "::" + guid.ToString()));
 }
Exemplo n.º 12
0
        private void BuildCharacteristics(Plugin.BluetoothLE.Server.IGattService service, Guid characteristicId, ClientPage clientPage, CharacteristicsType type)
        {
            //Plugin.BluetoothLE.Server.IGattCharacteristic characteristic = service.AddCharacteristic(characteristicId,
            //    CharacteristicProperties.Notify | CharacteristicProperties.Read |
            //    CharacteristicProperties.Write | CharacteristicProperties.WriteNoResponse,
            //    GattPermissions.Read | GattPermissions.Write );

            //characteristic.WhenReadReceived().Subscribe(x =>
            //{
            //    string write = "@@@@";
            //    if (string.IsNullOrWhiteSpace(write))
            //    {
            //        write = "0000";
            //    }

            //    x.Value = Encoding.UTF8.GetBytes(write);
            //});

            //characteristic.WhenWriteReceived().Subscribe(x =>
            //{
            //    string write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
            //    clientPage.ReceivedText(write);
            //});

            if (type == CharacteristicsType.RX_Notify)
            {
                Plugin.BluetoothLE.Server.IGattCharacteristic RX_NotifyCharacteristic =
                    service.AddCharacteristic(characteristicId, CharacteristicProperties.Notify, GattPermissions.Read);

                RX_NotifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e => {
                    RX_NotifyCharacteristic.Broadcast(Encoding.UTF8.GetBytes("Device successfully subscribed"));
                });


                //IDisposable notifyBroadcast = null;
                //notifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e =>
                //{
                //    var @event = e.IsSubscribed ? "Subscribed" : "Unsubcribed";

                //    if (notifyBroadcast == null)
                //    {
                //        this.notifyBroadcast = Observable
                //            .Interval(TimeSpan.FromSeconds(1))
                //            .Where(x => notifyCharacteristic.SubscribedDevices.Count > 0)
                //            .Subscribe(_ =>
                //            {
                //                Debug.WriteLine("Sending Broadcast");
                //                var dt = DateTime.Now.ToString("g");
                //                var bytes = Encoding.UTF8.GetBytes(dt);
                //                notifyCharacteristic.Broadcast(bytes);
                //            });
                //    }
                //});
            }

            if (type == CharacteristicsType.TX_Write)
            {
                Plugin.BluetoothLE.Server.IGattCharacteristic TX_WriteCharacteristic =
                    service.AddCharacteristic(characteristicId, CharacteristicProperties.Write, GattPermissions.Write);

                TX_WriteCharacteristic.WhenWriteReceived().Subscribe(x =>
                {
                    string write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
                    clientPage.ReceivedText(write);
                });
            }
        }
Exemplo n.º 13
0
        private async void Start_Server()
        {
            var server  = CrossBleAdapter.Current.CreateGattServer();
            var service = server.AddService(Guid.NewGuid(), true);

            var characteristic = service.AddCharacteristic(
                Guid.Parse(Constants.GUID_SEND),
                CharacteristicProperties.Read | CharacteristicProperties.Write | CharacteristicProperties.WriteNoResponse,
                GattPermissions.Read | GattPermissions.Write
                );

            notifyCharacteristic = service.AddCharacteristic
                                   (
                Guid.Parse(Constants.GUID_NOTIFY),
                CharacteristicProperties.Indicate | CharacteristicProperties.Notify,
                GattPermissions.Read | GattPermissions.Write
                                   );

            IDisposable notifyBroadcast = null;

            notifyCharacteristic.WhenDeviceSubscriptionChanged().Subscribe(e =>
            {
                var @event = e.IsSubscribed ? "Subscribed" : "Unsubcribed";

                if (notifyBroadcast == null)
                {
                    notifyBroadcast = Observable
                                      .Interval(TimeSpan.FromSeconds(1))
                                      .Where(x => notifyCharacteristic.SubscribedDevices.Count > 0)
                                      .Subscribe(_ =>
                    {
                        Debug.WriteLine("Sending Broadcast");
                        var dt    = DateTime.Now.ToString("g");
                        var bytes = Encoding.UTF8.GetBytes(dt);
                        notifyCharacteristic.Broadcast(bytes);
                    });
                }
            });

            characteristic.WhenReadReceived().Subscribe(x =>
            {
                // you must set a reply value
                x.Value = Mess_To_Byte(BluetoothMessageTypes.ACKNOWLEDGE);

                BluetoothMessageTypes mess = Byte_To_Mess(x.Value);
                byte[] data = null;

                if (x.Value.Length >= NUM_BYTES_IN_MESSAGE_TYPE)
                {
                    data = new byte[x.Value.Length - NUM_BYTES_IN_MESSAGE_TYPE];

                    for (int i = 0; i < data.Length; ++i)
                    {
                        data[i] = x.Value[i + NUM_BYTES_IN_MESSAGE_TYPE];
                    }
                }

                BluetoothEventArgs e = new BluetoothEventArgs(mess, data);
                BluetoothRecHandeler.DynamicInvoke(e);

                x.Status = GattStatus.Success; // you can optionally set a status, but it defaults to Success
            });
            characteristic.WhenWriteReceived().Subscribe(x =>
            {
                var write = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length);
                // do something value
            });

            await server.Start(new Plugin.BluetoothLE.Server.AdvertisementData
            {
                LocalName = "TestServer"
            });

            var scanner = CrossBleAdapter.Current.Scan().Subscribe(scanResult =>
            {
                // do something with it
                // the scanresult contains the device, RSSI, and advertisement packet
            });

            scanner.Dispose(); // to stop scanning
        }