private async void ResultCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
        {
            // BT_Code: Process a read request.
            using (args.GetDeferral())
            {
                // Get the request information.  This requires device access before an app can access the device's request.
                GattReadRequest request = await args.GetRequestAsync();

                if (request == null)
                {
                    // No access allowed to the device.  Application should indicate this to the user.
                    rootPage.NotifyUser("Access to device not allowed", NotifyType.ErrorMessage);
                    return;
                }

                var writer = new DataWriter();
                writer.ByteOrder = ByteOrder.LittleEndian;
                writer.WriteInt32(resultVal);

                // Can get details about the request such as the size and offset, as well as monitor the state to see if it has been completed/cancelled externally.
                // request.Offset
                // request.Length
                // request.State
                // request.StateChanged += <Handler>

                // Gatt code to handle the response
                request.RespondWithValue(writer.DetachBuffer());
            }
        }
        private async void WifiListCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
        {
            using (args.GetDeferral())
            {
                // Get the request information.  This requires device access before an app can access the device's request.
                GattReadRequest request = await args.GetRequestAsync();

                if (request == null)
                {
                    // No access allowed to the device.  Application should indicate this to the user.
                    return;
                }

                var writer = new DataWriter();

                var result = JsonConvert.SerializeObject(
                    new WIfiNetworkPayload
                {
                    AvailableAdapters =
                        this.WiFiAdapter.NetworkReport.AvailableNetworks.Select(x => new WifiNetwork {
                        Ssid = x.Ssid
                    })
                });

                writer.WriteString(result);
                //var wifiAdapters =
                //writer.WriteString("{ \"AvailableAdapters\": [{ \"Ssid\": \"ilab\" }, { \"Ssid\": \"uqconnect\" }] }");

                // Gatt code to handle the response
                request.RespondWithValue(writer.DetachBuffer());
            }
        }
Example #3
0
        /// <summary>
        /// Read request callback to update the value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected override bool ReadRequested(GattSession session, GattReadRequest request)
        {
            DataWriter writer = new DataWriter();

            int maxPayloadSize = session.MaxPduSize - 1;

            // make sure our source data is bigger than a single read request to make sure a ReadBlobRequest is done
            if (longCharacteristicData.Length < maxPayloadSize)
            {
                // This should not be required as the server should only be processing one request at a time
                // but it's better to be safe than sorry
                lock (dataLock)
                {
                    longCharacteristicData = new byte[(int)(maxPayloadSize * 2.5)];

                    for (int i = 0; i < longCharacteristicData.Length; i++)
                    {
                        longCharacteristicData[i] = (byte)(i % 10);
                    }
                }
            }

            // finish getting the read request
            int offset = (int)request.Offset;

            // calculate the size of the data we send back
            int chunk = Math.Min(maxPayloadSize, longCharacteristicData.Length - offset);

            Debug.WriteLine($"UpdateValue: payloadSize: {maxPayloadSize}, chunk {chunk}");

            // prep the data we send back
            var readValue = String.Empty;
            var buffer    = new byte[chunk];

            buffer.Initialize();

            // copy from source to target
            Array.Copy(longCharacteristicData, longCharacteristicReadOffset, buffer, 0, chunk);

            // write to our internal Value which will be used to send back the data
            writer.WriteBytes(buffer);

            readValue = buffer.BytesToString();
            Debug.WriteLine("MicrosoftReadLongCharacteristic: Read request value: {readValue}");

            // Update our characteristics value.
            Value = writer.DetachBuffer();

            // Respond back to the caller.
            request.RespondWithValue(Value);
            return(true);
        }
        private async void RossCharacteristic_ReadRequestedAsync(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
        {
            using (args.GetDeferral())
            {
                // Get the request information.  This requires device access before an app can access the device's request.
                GattReadRequest request = await args.GetRequestAsync();

                if (request == null)
                {
                    // No access allowed to the device.  Application should indicate this to the user.
                    return;
                }

                var writer = new DataWriter();

                writer.WriteString("1.5.14.0");

                // Gatt code to handle the response
                request.RespondWithValue(writer.DetachBuffer());
            }
        }
 protected override bool ReadRequested(GattSession session, GattReadRequest request)
 {
     UpdateCurrentTimeValue();
     request.RespondWithValue(Value);
     return(true);
 }