コード例 #1
0
        private async void writeRemoteCanStop(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();
            var request  = await args.GetRequestAsync();

            if (request == null)
            {
                Debug.WriteLine("Write of Can Stop had a null request! Make sure the application manifest allows Bluetooth access.");
                deferral.Complete();
                return;
            }

            // The "Can Stop" characteristic is a single byte of data - 0 for false, and 1 for true.
            var reader = DataReader.FromBuffer(request.Value);

            if (reader.UnconsumedBufferLength > 0)
            {
                var value = reader.ReadByte();
                await eventDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    canStop = value > 0;
                    Debug.WriteLine("Can Stop is now: {0}", canStop);
                    StateChanged?.Invoke(this, null);
                });
            }

            if (request.Option == GattWriteOption.WriteWithResponse)
            {
                request.Respond();
            }

            deferral.Complete();
        }
コード例 #2
0
        //---------------------------------------------------------------------
        // Event callback for Write
        //---------------------------------------------------------------------

        protected virtual void Characteristic_WriteRequested(
            GattLocalCharacteristic sender,
            GattWriteRequestedEventArgs args
            )
        {
            Characteristic_WriteRequested(sender, args, args.GetDeferral());
        }
コード例 #3
0
        private async void OnWriteMessage(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();

            try
            {
                var req = await args.GetRequestAsync();

                var reader = DataReader.FromBuffer(req.Value);

                var buf = new byte[reader.UnconsumedBufferLength];
                reader.ReadBytes(buf);
                var xml = DMessenger.MessageEncoder.Decode(buf);
                var msg = new DMessenger.Message(xml);

                if (req.Option == GattWriteOption.WriteWithResponse)
                {
                    req.Respond();
                }
            }
            finally
            {
                deferral.Complete();
            }
        }
        //---------------------------------------------------------------------
        // Write request callback to receive the header length of an IPv6
        // packet that had its header compressed
        //---------------------------------------------------------------------

        protected override void Characteristic_WriteRequested(
            GattLocalCharacteristic sender,
            GattWriteRequestedEventArgs args
            )
        {
            // Receive the Write request into this characteristic's Value buffer
            base.Characteristic_WriteRequested(sender, args);
        }
        //---------------------------------------------------------------------
        // Write request callback to receive an IPv6 packet
        //---------------------------------------------------------------------

        protected override void Characteristic_WriteRequested(
            GattLocalCharacteristic sender,
            GattWriteRequestedEventArgs args
            )
        {
            // Receive the Write request into this characteristic's Value buffer
            base.Characteristic_WriteRequested(sender, args);

            // Caller does something with the packet after this
        }
コード例 #6
0
        private async void CommandCharacteristic_WriteRequested(GattLocalCharacteristic sender,
                                                                GattWriteRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();

            var request = await args.GetRequestAsync();

            if (request == null)
            {
                return;
            }

            if (request.Value.Length != 1 && request.Value.Length != 5)
            {
                if (request.Option == GattWriteOption.WriteWithResponse)
                {
                    request.RespondWithProtocolError(GattProtocolError.InvalidAttributeValueLength);
                }
                return;
            }

            var reader = DataReader.FromBuffer(request.Value);

            reader.ByteOrder = ByteOrder.LittleEndian;

            var commandCode = (CommandCode)reader.ReadByte();

            if (!Enum.IsDefined(typeof(CommandCode), commandCode))
            {
                if (request.Option == GattWriteOption.WriteWithResponse)
                {
                    request.RespondWithProtocolError(GattProtocolError.InvalidPdu);
                }
                return;
            }

            if (commandCode == CommandCode.Play && request.Value.Length == 5)
            {
                OnCommandReceived(commandCode, reader.ReadInt32());
            }
            else
            {
                OnCommandReceived(commandCode);
            }

            if (request.Option == GattWriteOption.WriteWithResponse)
            {
                request.Respond();
            }

            deferral.Complete();
        }
コード例 #7
0
        private async void writeUserMessage(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();
            var request  = await args.GetRequestAsync();

            if (request == null)
            {
                Debug.WriteLine("Write of User Message had a null request! Make sure the application manifest allows Bluetooth access.");
                deferral.Complete();
                return;
            }

            try
            {
                // The "User Message" characteristic contains a UTF-8 string.
                // It's valid for the app to send a zero-length payload here, which means "no custom message".
                var bufferCount = request.Value.Length;
                if (bufferCount > 0)
                {
                    var stringValue = Encoding.UTF8.GetString(request.Value.ToArray());
                    await eventDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        userMessage = stringValue;
                        Debug.WriteLine("User Message is now: {0}", userMessage);
                        StateChanged?.Invoke(this, null);
                    });
                }
                else
                {
                    // If we have an empty buffer, we should null out the string.
                    await eventDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        userMessage = null;
                        Debug.WriteLine("User Message is now empty");
                        StateChanged?.Invoke(this, null);
                    });
                }
            }
            catch { }

            if (request.Option == GattWriteOption.WriteWithResponse)
            {
                request.Respond();
            }

            deferral.Complete();
        }
コード例 #8
0
        private async void BrightnessCharacteristic_WriteRequested(GattLocalCharacteristic sender,
                                                                   GattWriteRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();

            var request = await args.GetRequestAsync();

            if (request == null)
            {
                return;
            }

            if (request.Value.Length != 8)
            {
                if (request.Option == GattWriteOption.WriteWithResponse)
                {
                    request.RespondWithProtocolError(GattProtocolError.InvalidAttributeValueLength);
                }
                return;
            }

            var reader = DataReader.FromBuffer(request.Value);

            reader.ByteOrder = ByteOrder.LittleEndian;

            var brightness = reader.ReadDouble();

            if (brightness < 0 || brightness > 1)
            {
                if (request.Option == GattWriteOption.WriteWithResponse)
                {
                    request.RespondWithProtocolError(GattProtocolError.InvalidPdu);
                }
                return;
            }

            OnBrightnessChangeReceived(brightness);

            if (request.Option == GattWriteOption.WriteWithResponse)
            {
                request.Respond();
            }

            deferral.Complete();
        }
コード例 #9
0
        private async void CharacteristicWriteRequested(GattLocalCharacteristic glc, GattWriteRequestedEventArgs args)
        {
            var def = args.GetDeferral();
            var req = await args.GetRequestAsync();

            var rdr = DataReader.FromBuffer(req.Value);
            var str = rdr.ReadString(req.Value.Length);

            if (DIGITS.IsMatch(str))
            {
                SendKeys.SendWait(str + "{ENTER}");
            }

            if (req.Option == GattWriteOption.WriteWithResponse)
            {
                req.Respond();
            }

            def.Complete();
        }
コード例 #10
0
        /// <summary>
        /// handles Reads on the STDOUT characteristic.  If the command thread is done, restart command thread.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void WriteCharacteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            var deferral = args.GetDeferral();

            var request = await args.GetRequestAsync();

            var reader = DataReader.FromBuffer(request.Value);

            //string data = reader.ReadString(reader.UnconsumedBufferLength);
            byte[] data = new byte[reader.UnconsumedBufferLength];
            reader.ReadBytes(data);
            if (cmd.getCmdRunning() == true)
            {
                cmd.writeStdin(data);
                Indicate();
            }
            else
            {
                cmd.startCommand();
                Indicate();
            }

            if (request.Option == GattWriteOption.WriteWithResponse)
            {
                request.Respond();
            }

            deferral.Complete();
        }
        private async void OperatorCharacteristic_WriteRequestedAsync(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            using (args.GetDeferral())
            {
                // Get the request information.  This requires device access before an app can access the device's request.
                GattWriteRequest request = await args.GetRequestAsync();

                if (request == null)
                {
                    // No access allowed to the device.  Application should indicate this to the user.
                    return;
                }
                ProcessWriteCharacteristic(request, CalculatorCharacteristics.Operator);
            }
        }
コード例 #12
0
        // Helper function for async Write callback
        protected virtual async void Characteristic_WriteRequested(
            GattLocalCharacteristic sender,
            GattWriteRequestedEventArgs args,
            Deferral deferral
            )
        {
            Debug.WriteLine("base.Characteristic_WriteRequested Entry");

            GattWriteRequest request = null;

            // Get an event deferral for async operations if we don't have one
            if (deferral == null)
            {
                deferral = args.GetDeferral();
            }

            //
            // Normally, GetRequestAsync() is recommended to run on a UI thread
            // because, even with paired devices, the device may prompt the
            // user for consent. But, we're running in a background service so
            // we won't run this on a UI thread. According to one of the devs
            // on the core Bluetooth team, because this is for a "test
            // application," consent prompts are not currently part of MS's
            // policy and it will be auto-accepted.
            //
            request = await args.GetRequestAsync();

            if (request == null)
            {
                Debug.WriteLine("Could not retrieve request from the " +
                                "GattWriteRequestedEventArgs. Request was " +
                                "null."
                                );
                goto Exit;
            }

            // Set the value
            Value = request.Value;

            // Respond with completion notification if requested
            if (request.Option == GattWriteOption.WriteWithResponse)
            {
                Debug.WriteLine("Completing write request with response.");
                request.Respond();
            }
            else
            {
                Debug.WriteLine("Completing write request without response.");
            }

            // Debugging print!
            //byte[] data;
            //CryptographicBuffer.CopyToByteArray(Value, out data);

            //if(data == null)
            //{
            //    Debug.WriteLine("Value after write completion was NULL. :(");
            //}
            //else
            //{
            //    Debug.WriteLine($"Write completed. Received packet from {args.Session.DeviceId.ToString()}, value is now {Utilities.BytesToString(data)}");
            //}

Exit:

            deferral.Complete();

            Debug.WriteLine("base.Characteristic_WriteRequested Exit");
        }
        private async void OnboardingRequestCharacteristic_WriteRequestedAsync(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            using (args.GetDeferral())
            {
                var request = await args.GetRequestAsync();

                var reader = DataReader.FromBuffer(request.Value);

                //var bytes = new byte[4098];

                var result = reader.ReadString(reader.UnconsumedBufferLength);

                //var result = Encoding.UTF8.GetString(bytes);

                rootPage.NotifyUser($"Onboarding Request : {result}", NotifyType.StatusMessage);

                // Complete the request if needed
                if (request.Option == GattWriteOption.WriteWithResponse)
                {
                    request.Respond();
                }
            }
        }
コード例 #14
0
 private async void Op1Characteristic_WriteRequestedAsync(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
 {
     // BT_Code: Processing a write request.
     using (args.GetDeferral())
     {
         PlayMusic();
     }
 }
コード例 #15
0
        // GattLocalCharacteristic

        private async void OnCharacteristicWriteRequested(GattLocalCharacteristic characteristic, GattWriteRequestedEventArgs args)
        {
            Utils.Info("OnWriteRequested: {0}", args.Session.DeviceId.Id);

            using (var derferral = args.GetDeferral())
            {
                var request = await args.GetRequestAsync().AsTask();

                lock (mLockObject)
                {
                    if ((mStatus != Status.Ready) && (mStatus != Status.Advertise))
                    {
                        Utils.Error("invalid status: {0}", mStatus.ToString());
                        request.RespondWithProtocolError(GattProtocolError.RequestNotSupported);
                        return;
                    }

                    var context = mSubscribedCentrals.Where(ctx => ctx.client.Session.DeviceId.Id == args.Session.DeviceId.Id).FirstOrDefault();
                    if (context == null)
                    {
                        Utils.Error("not subscribed");
                        request.RespondWithProtocolError(GattProtocolError.InsufficientAuthentication);
                        return;
                    }

                    if ((request.Option != GattWriteOption.WriteWithResponse) || (request.Offset != 0) || (request.Value == null))
                    {
                        Utils.Error("invalid parameter");
                        request.RespondWithProtocolError(GattProtocolError.InvalidPdu);
                        return;
                    }

                    request.Respond();

                    if (context.connectionId == 0) // Negotiation complete
                    {
                        Utils.Info("negotiation complete");

                        context.connectionId = mNextConnectionId++;

                        mCallback.OnConnect(context.connectionId);
                    }
                    else
                    {
                        ProcessReceiveBuffer(context, request.Value.AsBytes());
                    }
                }
            }
        }
コード例 #16
0
        /// <summary>
        /// Base implementation for the write callback
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void Characteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            Debug.WriteLine("Characteristic_WriteRequested: Write Requested");

            // Grab the event deferral before performing any async operations in the handler.
            var deferral = args.GetDeferral();

            // In order to get the remote request, access to the device must be provided by the user.
            // This can be accomplished by calling BluetoothLEDevice.RequestAccessAsync(), or by getting the request on the UX thread.
            //
            // Note that subsequent calls to RequestAccessAsync or GetRequestAsync for the same device do not need to be called on the UX thread.
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(
                async() =>
            {
                // Grab the request
                var request = await args.GetRequestAsync();

                Debug.WriteLine($"Characteristic_WriteRequested - Length {request.Value.Length}, State: {request.State}, Offset: {request.Offset}");

                // WriteRequested overriden if needed to set up the Value
                // if not - set the Value here
                if (!WriteRequested(args.Session, request))
                {
                    // Set the characteristic Value
                    Value = request.Value;
                }
                else
                {
                    Debug.WriteLine("Characteristic_WriteRequested: Looks like write completed in derived class");
                }

                // Complete request (respond with confirmation in case of WRITE REQ) even if the overriden WriteRequested did not change the Value.
                // As side effect this will show in nRF Connect that the value has changed even if it hasn't.
                if (request.Option == GattWriteOption.WriteWithResponse)
                {
                    Debug.WriteLine("Characteristic_WriteRequested: Completing request with responds");
                    request.Respond();
                }
                else
                {
                    Debug.WriteLine("Characteristic_WriteRequested: Completing request without responds");
                }

                byte[] data;
                CryptographicBuffer.CopyToByteArray(Value, out data);

                if (data == null)
                {
                    Debug.WriteLine("Characteristic_WriteRequested: Value after write complete was NULL");
                }
                else
                {
                    Debug.WriteLine($"Characteristic_WriteRequested: New Value: {data.BytesToString()}");
                }

                deferral.Complete();
            });
        }
コード例 #17
0
        public static async void Op1Characteristic_WriteRequestedAsync(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            // BT_Code: Processing a write request.
            using (args.GetDeferral())
            {
                ApplicationData.Current.LocalSettings.Values["Op1ReceivedEver"] = "yes";
                // Get the request information.  This requires device access before an app can access the device's request.
                GattWriteRequest request = await args.GetRequestAsync();

                if (request == null)
                {
                    // No access allowed to the device.  Application should indicate this to the user.
                    return;
                }
                ProcessWriteCharacteristic(request, SDKTemplate.Calculator.CalculatorCharacteristics.Operand1);
                //UpdateUX();
            }
        }
コード例 #18
0
 /// <summary>
 /// Event handler for Alert Notification Control point
 /// </summary>
 /// <param name="sender">The source of the Write request</param>
 /// <param name="args">Details about the request</param>
 private void AlertNotificationCtrlPntOnWriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
 {
     throw new NotImplementedException();
 }
コード例 #19
0
        /// <summary>
        /// Base implementation for the write callback
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private async void Characteristic_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            Debug.WriteLine("Characteristic_WriteRequested: Write Requested");

            // Grab the event deferral before performing any async operations in the handler.
            var deferral = args.GetDeferral();

            // In order to get the remote request, access to the device must be provided by the user.
            // This can be accomplished by calling BluetoothLEDevice.RequestAccessAsync(), or by getting the request on the UX thread.
            //
            // Note that subsequent calls to RequestAccessAsync or GetRequestAsync for the same device do not need to be called on the UX thread.
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(
                async() =>
            {
                // Grab the request
                var request = await args.GetRequestAsync();

                Debug.WriteLine($"Characteristic_WriteRequested - Length {request.Value.Length}, State: {request.State}, Offset: {request.Offset}");

                if (!WriteRequested(args.Session, request))
                {
                    // Set the characteristic Value
                    Value = request.Value;

                    // Respond with completed
                    if (request.Option == GattWriteOption.WriteWithResponse)
                    {
                        Debug.WriteLine("Characteristic_WriteRequested: Completing request with responds");
                        request.Respond();
                    }
                    else
                    {
                        Debug.WriteLine("Characteristic_WriteRequested: Completing request without responds");
                    }
                }

                // everything below this is debug. Should implement this on non-UI thread based on
                // https://github.com/Microsoft/Windows-task-snippets/blob/master/tasks/UI-thread-task-await-from-background-thread.md
                byte[] data;
                CryptographicBuffer.CopyToByteArray(Value, out data);

                if (data == null)
                {
                    Debug.WriteLine("Characteristic_WriteRequested: Value after write complete was NULL");
                }
                else
                {
                    Debug.WriteLine($"Characteristic_WriteRequested: New Value: {data.BytesToString()}");
                }

                deferral.Complete();
            });
        }
コード例 #20
0
        private async void HidControlPoint_WriteRequested(GattLocalCharacteristic sender, GattWriteRequestedEventArgs args)
        {
            try
            {
                var deferral     = args.GetDeferral();
                var writeRequest = await args.GetRequestAsync();

                Debug.WriteLine("Value written to HID Control Point: " + GetStringFromBuffer(writeRequest.Value));
                // Control point only supports WriteWithoutResponse.
                deferral.Complete();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Failed to handle write to Hid Control Point due to: " + e.Message);
            }
        }