Get the live data on TCP server from UWP (UPDATED)

前端 未结 1 1954
生来不讨喜
生来不讨喜 2021-01-26 19:26

Below is one method that basically sends the data to TCP Server.

         UPDATE BEGINS HERE: 
 //////////////////////////////////
 private string FormatValueBy         


        
相关标签:
1条回答
  • 2021-01-26 20:06
    while (true)
    {
        await streamWriter.WriteLineAsync(data);
        await streamWriter.FlushAsync();
    }
    

    The while(true) will keep repeating, meaning that it will always send 'data' at its current value. This is what causes your issue.

    In my opinion, you should keep a connection to your TCP server open outside of your 'TrySend' method, and use this method only to send the data. This way you won't need to use this loop.

    EDIT :

    Try this :

    private async void CharacteristicReadButton_Click()
        {
        while(true)
        {
                // BT_Code: Read the actual value from the device by using Uncached.
                GattReadResult result = await selectedCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
                if (result.Status == GattCommunicationStatus.Success)
                {
    
    
                    string formattedResult = FormatValueByPresentation(result.Value, presentationFormat);
                    rootPage.NotifyUser($"Read result: {formattedResult}", NotifyType.StatusMessage);
    
                    //string formattedResult = FormatValueByPresentation(result.Value, presentationFormat);
                    //rootPage.NotifyUser($"Read result: {formattedResult}", NotifyType.StatusMessage);
                }
                else
                {
                    rootPage.NotifyUser($"Read failed: {result.Status}", NotifyType.ErrorMessage);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题