Exemplo n.º 1
0
        void SetupWrite()
        {
            if (this.onWrite == null)
            {
                return;
            }

            this.context
            .CharacteristicWrite
            .Where(x => x.Characteristic.Equals(this.Native))
            .Subscribe(ch =>
            {
                var peripheral = new Peripheral(ch.Device);
                var request    = new WriteRequest(this, peripheral, ch.Value, ch.Offset, ch.ResponseNeeded);
                var state      = this.onWrite(request);

                if (request.IsReplyNeeded)
                {
                    this.context.Server.SendResponse(
                        ch.Device,
                        ch.RequestId,
                        state.ToNative(),
                        ch.Offset,
                        ch.Value
                        );
                }
            })
            .DisposedBy(this.disposer);
        }
Exemplo n.º 2
0
        void SetupRead()
        {
            if (this.onRead == null)
            {
                return;
            }

            this.context
            .CharacteristicRead
            .Where(x => x.Characteristic.Equals(this.Native))
            .Subscribe(ch =>
            {
                var peripheral = new Peripheral(ch.Device);
                var request    = new ReadRequest(this, peripheral, ch.Offset);
                var result     = this.onRead(request);

                this.context.Server.SendResponse
                (
                    ch.Device,
                    ch.RequestId,
                    result.Status.ToNative(),
                    ch.Offset,
                    result.Data
                );
            })
            .DisposedBy(this.disposer);
        }
Exemplo n.º 3
0
        IPeripheral GetOrAdd(BluetoothDevice native)
        {
            lock (this.subscribers)
            {
                if (this.subscribers.ContainsKey(native.Address))
                {
                    return(this.subscribers[native.Address]);
                }

                var device = new Peripheral(native);
                this.subscribers.Add(native.Address, device);
                return(device);
            }
        }
Exemplo n.º 4
0
        public void Build()
        {
            this.Native = new BluetoothGattCharacteristic(
                this.Uuid.ToUuid(),
                this.properties,
                this.permissions
                );
            if (this.onRead != null)
            {
                this.context
                .Callbacks
                .CharacteristicRead
                .Where(x => x.Characteristic.Equals(this.Native))
                .Subscribe(ch =>
                {
                    var peripheral = new Peripheral(ch.Device);
                    var request    = new ReadRequest(this, peripheral, ch.Offset);
                    var result     = this.onRead(request);

                    this.context.Server.SendResponse
                    (
                        ch.Device,
                        ch.RequestId,
                        result.Status.ToNative(),
                        ch.Offset,
                        result.Data
                    );
                })
                .DisposedBy(this.disposer);
            }
            if (this.onSubscribe != null)
            {
                this.context
                .Callbacks
                .DescriptorWrite
                .Subscribe(x =>
                {
                })
                .DisposedBy(this.disposer);

                this.context
                .Callbacks
                .ConnectionStateChanged
                .Where(x => x.NewState == ProfileState.Disconnected)
                .Subscribe(x =>
                {
                    var peripheral = this.Remove(x.Device);
                    if (peripheral != null)
                    {
                        this.onSubscribe(new CharacteristicSubscription(this, peripheral, false));
                    }
                })
                .DisposedBy(this.disposer);

                //if (args.Descriptor.Equals(this.NotificationDescriptor))
                //.Where(x => )

                //                        if (args.Value.SequenceEqual(NotifyEnabledBytes) || args.Value.SequenceEqual(IndicateEnableBytes))
                //                        {
                //                            var device = this.GetOrAdd(args.Device);
                //                            ob.OnNext(new DeviceSubscriptionEvent(device, true));
                //                        }
                //                        else
                //                        {
                //                            var device = this.Remove(args.Device);
                //                            if (device != null)
                //                                ob.OnNext(new DeviceSubscriptionEvent(device, false));
                //                        }
            }
            if (this.onWrite != null)
            {
                this.context
                .Callbacks
                .CharacteristicWrite
                .Where(x => x.Characteristic.Equals(this.Native))
                .Subscribe(ch =>
                {
                    var peripheral = new Peripheral(ch.Device);
                    var request    = new WriteRequest(this, peripheral, ch.Value, ch.Offset, ch.ResponseNeeded);
                    var state      = this.onWrite(request);

                    if (request.IsReplyNeeded)
                    {
                        this.context.Server.SendResponse(
                            ch.Device,
                            ch.RequestId,
                            state.ToNative(),
                            ch.Offset,
                            ch.Value
                            );
                    }
                })
                .DisposedBy(this.disposer);
            }

            if (this.onSubscribe != null)
            {
                var ndesc = new BluetoothGattDescriptor(
                    Constants.NotifyDescriptorUuid,
                    GattDescriptorPermission.Read | GattDescriptorPermission.Write
                    );
                this.Native.AddDescriptor(ndesc);
            }
        }