32 英尺蓝牙 API - 保持连接

32feet Bluetooth API - Holding connection

我们的目标是使用蓝牙读取血压测量设备的整个数据流的 C# 项目。我们能够启动连接,成功检索到一些数据包,但总是在约 5 秒后失去与设备的连接。我们忘记了什么?

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 

解决方法很简单。我只是忘了定期发送 LifeSignMsg,例如

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