Facebook
From Lousy Bat, 7 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 309
  1. //KOD wykonywany na wcisniecie przycisku w aplikacji - to dziala
  2. private void button_send_Click(object sender, RoutedEventArgs e)
  3.         {
  4.            string command;
  5.            
  6.             short kp,kd,w1,w2,w3,w4;
  7.             if (!short.TryParse(textBox_kp.Text, out kp))
  8.                 kp = 0;  
  9.             if (!short.TryParse(textBox_kd.Text, out kd))
  10.                 kd = 0;
  11.             if (!short.TryParse(textBox_W1.Text, out w1))
  12.                 w1 = 0;
  13.             if (!short.TryParse(textBox_W2.Text, out w2))
  14.                 w2 = 0;
  15.             if (!short.TryParse(textBox_W3.Text, out w3))
  16.                 w3 = 0;
  17.             if (!short.TryParse(textBox_W4.Text, out w4))
  18.                 w4 = 0;
  19.             command = ":#" + kp + "," + kd + "," + w1 + "," + w2 + "," + w3 + "," + w4 + ";";
  20.             blue.Send(command);
  21.             if(!blue.polaczono)
  22.             {
  23.                 myRectangle.Fill = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 155, 0, 0));
  24.                 MessageBox.Show("Brak połączenia bluetooth");
  25.             }
  26.         }
  27.  
  28.  
  29.  
  30. //********************METODY DO WYSYLANIA DANYCH Z APLIKACJI WINDOWS PHONE**********************
  31.  public async void Send(string WhatToSend)
  32.         {
  33.             if (!polaczono)
  34.             {
  35.                 return; // Stop
  36.             }
  37.             else              
  38.             {
  39.                 var datab = GetBufferFromByteArray(UTF8Encoding.UTF8.GetBytes(WhatToSend));
  40.                 try
  41.                 {
  42.                     await BTSock.OutputStream.WriteAsync(datab);   // Wysłanie zmiennej typu string jako ciąg bajtów
  43.                 }
  44.                 catch (Exception ex)
  45.                 {
  46.                     MessageBox.Show("Nie udało się wysłać wiadomości: n" + ex.Message);
  47.                 }
  48.             }
  49.         }    //Metoda do wysyłania zmiennej typu string
  50.  
  51.         public async void Send(byte[] array)
  52.         {
  53.             if (!polaczono)
  54.             {
  55.                 // MessageBox.Show("Brak połaczenie, nie udało się wysłać");
  56.                 return; // Stop
  57.             }
  58.             else
  59.             {
  60.                 var datab = GetBufferFromByteArray(array);
  61.                 try
  62.                 {
  63.                     await BTSock.OutputStream.WriteAsync(datab); // Wysłanie ciągu bajtów
  64.                 }
  65.                 catch (Exception ex)
  66.                 {
  67.                     MessageBox.Show("Nie udało się wysłać wiadomości: n" + ex.Message);
  68.                 }
  69.             }
  70.         }    //Metoda do ciągu bajtów
  71.  
  72.  
  73. //********************** Task do odbioru danych *********************************
  74.  
  75.  public async Task<string> DoCommand()
  76.         {
  77.             StringBuilder strBuilder = new StringBuilder();
  78.             using (StreamSocket BTSock = new StreamSocket())
  79.             {
  80.                 using (DataReader reader = new DataReader(BTSock.InputStream))
  81.                 {
  82.                     reader.InputStreamOptions = InputStreamOptions.Partial;
  83.                     await reader.LoadAsync(8192);
  84.                     while (reader.UnconsumedBufferLength > 0)
  85.                     {
  86.                         strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength));
  87.                         await reader.LoadAsync(8192);
  88.                     }
  89.                     reader.DetachStream();
  90.                 }
  91.             }
  92.             return (strBuilder.ToString());
  93.         }