コード例 #1
0
 private Task _ReadInputStreamTaskAsync()
 {
     return(Task.Run(() =>
     {
         var inputStream = RfcommConnection.InputStream;
         bool disconnected = false;
         while (true)
         {
             byte[] buffer = new byte[255];
             var readSize = inputStream.Read(buffer, 0, buffer.Length);
             if (readSize == 0)
             {
                 disconnected = true;
                 break;
             }
             var readBytes = new byte[readSize];
             Array.Copy(buffer, 0, readBytes, 0, readSize);
             var msg = new RXReceiveMessage
             {
                 RXConnection = this,
                 Bytes = readBytes
             };
             OnReceived?.Invoke(this, msg);
         }
         if (disconnected)
         {
             RfcommConnection.Dispose();
             ConnectionState = RXConnectionState.Destoryed;
             OnConnectionStateChanged?.Invoke(this, ConnectionState);
         }
     }));
 }
コード例 #2
0
        Task ReadInputSreamAsync()
        {
            return(Task.Run(() =>
            {
                bool disconnected = false;
                while (true)
                {
                    int readBufferSize = 255;
                    byte[] buffer = new byte[readBufferSize];
                    var readSize = RfcommConnection.InputStream.Read(buffer, 0, readBufferSize);
                    if (readSize == 0)
                    {
                        disconnected = true;
                        break;
                    }

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < readSize; i++)
                    {
                        sb.AppendFormat("{0:X2} ", buffer[i]);
                    }
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        RXList.Add(sb.ToString());
                    });
                }
                RfcommConnection.Dispose();
                Device.BeginInvokeOnMainThread(() =>
                {
                    Navigation.PopAsync();
                });
            }));
        }