Beispiel #1
0
        private async Task <string> waitSendRequest(Request req)
        {
            if (connectionState != ConnectionState._connected)
            {
                return(lastError = "Error: No connection available");
            }

            if (req == null)
            {
                return(lastError = "Error: Unable to send request");
            }

            Log.WriteLine("~~~Waiting : " + req.index);
            bool isanswered = await req.IsAnswered();

            if (!isanswered)
            {
                Log.WriteLine("~~~not answered : " + req.index);
                return(lastError = "Error: Did not received an answer");
            }

            IBuffer myresponse  = GattConvert.ToIBufferFromArray(req.GetResponse());
            string  strresponse = GattConvert.ToHexString(myresponse);

            strresponse = strresponse.Replace("-", "");
            Log.WriteLine("<---Response : " + req.index + " in " + Environment.CurrentManagedThreadId + "with " + strresponse);
            return(strresponse);
        }
        // Send one packet of BLE frame
        private async Task <bool> send_one_TX_Packet(int num)
        {
            int len    = 0;
            int offset = 0;

            if (SPPOverLECharacteristic == null)
            {
                setUpSPP();
                await SPPOverLECharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
                    GattClientCharacteristicConfigurationDescriptorValue.Notify);
            }
            if (SPPOverLECharacteristic == null)
            {
                throw new InvalidOperationException("Invalid BLE Characteristics");
            }


            // Check if last packet
            if (num > 0)
            {
                offset = m_TX_Buffer_rest + ((num - 1) * LEN_PACKET);
                len    = LEN_PACKET;
            }
            else
            {
                offset = 0;
                len    = m_TX_Buffer_rest;
            }

            // Extract frame from buffer
            byte[] packet = new byte[len + 1];
            packet[0] = (byte)offset;

            for (int i = 0; i < len; i++)
            {
                packet[i + 1] = m_TX_buffer[offset + i];
            }

            Log.WriteLine("~~~ send: " + CurrentRequest.index + " data=[" + BitConverter.ToString(packet, 0, packet.Length) + "]");

            Windows.Storage.Streams.IBuffer packetBuffer = GattConvert.ToIBufferFromArray(packet);

            if (SPPOverLECharacteristic != null)
            {
                GattCommunicationStatus status = await SPPOverLECharacteristic.WriteValueAsync(packetBuffer, GattWriteOption.WriteWithoutResponse);

                return(status == GattCommunicationStatus.Success);
            }

            return(false);
        }