2016-12-15 82 views
0

我們的目標是C#項目,讀取與藍牙的血壓測量設備的整個流。我們能夠開始連接,成功地檢索一些數據包,但在〜5秒後總是失去與設備的連接。我們忘了什麼?32feet藍牙API - 保持連接

public void Conn(BluetoothDeviceInfo blDevice) 
    { 
     try 
     { 

      BluetoothAddress addr = blDevice.DeviceAddress; 
      Guid myGuid = BluetoothService.SerialPort; 
      var endPoint = new BluetoothEndPoint(addr, myGuid); 
      client.Connect(endPoint); 
      BluetoothListener blist = new BluetoothListener(myGuid); 
      blist.Start(); 
      Console.WriteLine("Client connected"); 
      while (closed) 
      { 
       stream = client.GetStream(); 
       if (stream.CanRead) 
       { 
        byte[] myReadBuffer = new byte[10240]; 
        StringBuilder myCompleteMessage = new StringBuilder(); 
        int numberOfBytesRead = 0; 
        // Incoming message may be larger than the buffer size. 
        do 
        { 

         numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); 

         for (int j = 0; j < numberOfBytesRead; j++) 
          myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[j]); 
        } 
        while (stream.DataAvailable); 

        // Print out the received message to the console. 
        Console.WriteLine("Message : " + myCompleteMessage); 
       } 
       else 
       { 
        Console.WriteLine("Sorry. NetworkStream Error"); 
       } 
       Console.ReadLine(); 
      } 
     } 
     catch (InvalidOperationException e) 
     { 
      Console.WriteLine("No Connection"); 
     } 
    } 

輸出流實施例

Message : 0x02 0x22 0x58 0x61 0xE0 0x07 0x0C 0x0E 0x37 0xDE 0x65 0x01 0x04 0x72 0x48 0x23 0x91 0x44 0xCE 0x78 0xE1 0x7E 0xE8 0x85 0x37 0x1E 0x7A 0xEC 0xB5 0xC7 0x1E 0x7B 0xEC 0xAD 0xB7 0xDE 0x7A 0xEA 0xAD 0xB7 0xDE 0x7A 0xEB 0xAD 0xB7 0xDE 0x7A 0xEB 0xAD 0xC7 0x1E 0x7B 0xEC 0xB1 0xC7 0x5E 0x7B 0xED 0xB5 0xD7 0x5E 0x7B 0xED 0xB5 0xE7 0x9E 0x7B 0xEF 0xBD 0xF7 0xDE 0x7B 0xEF 0xBD 0x07 0x1F 0x7C 0xF1 0xC5 0x27 0x9F 0x7C 0xF2 0xCD 0x37 0xDF 0x7C 0xF3 0xD1 0x57 0x1F 0xFA 0x03 
Message : 0x02 0x22 0x58 0x62 0xE0 0x07 0x0C 0x0E 0x33 0xDF 0x65 0x01 0xF5 0xD5 0x57 0x5F 0x7D 0xF5 0xD9 0x67 0x9F 0x7D 0xF6 0xD5 0x57 0x1F 0x7D 0xF4 0xCD 0x17 0x1F 0x7C 0xEE 0xB5 0xD7 0x1E 0x7B 0xEC 0xAD 0xB7 0xDE 0x7A 0xEB 0xAD 0xA7 0x9E 0x7A 0xEA 0xA9 0xA7 0x9E 0x7A 0xEB 0xAD 0xA7 0x9E 0x7A 0xEA 0xA9 0xB7 0xDE 0x7A 0xEB 0xAD 0xB7 0xDE 0x7A 0xEB 0xAD 0xB7 0xDE 0x7A 0xEB 0xAD 0xB7 0x1E 0x7B 0xEC 0xB1 0xC7 0x1E 0x7B 0xEB 0xAD 0xB7 0xDE 0x7A 0xEB 0xB1 0xC7 0x1E 0xB6 0x03 

回答

0

該溶液很簡單。我只是忘了發送一個定期的LifeSignMsg像

 stream.Write(new byte[] { 0x02, 0x23, 0x00, 0x00, 0x03 }, 0, 5);