示例#1
0
        public void OnReceiveFromDevice(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
        {
            Logger.Log($"characteristicChanged...");
            Logger.Log($"- Length={eventArgs.CharacteristicValue.Length}");
            if (eventArgs.CharacteristicValue.Length <= 0)
            {
                return;
            }

            byte[] data = new byte[eventArgs.CharacteristicValue.Length];
            Windows.Storage.Streams.DataReader.FromBuffer(eventArgs.CharacteristicValue).ReadBytes(data);

            // for log
            {
                var tmp = BitConverter.ToString(data);
                Logger.Log($"- Data...");
                Logger.Log($"{tmp}");
            }

            // parse
            {
                // [0] STAT
                if (data[0] == 0x81)
                {
                    Logger.Log($"PING");
                }
                else if (data[0] == 0x82)
                {
                    Logger.Log($"KEEPALIVE");
                    KeepAlive?.BeginInvoke(this, EventArgs.Empty, null, null);
                }
                else if (data[0] == 0x83)
                {
                    Logger.Log($"MSG");
                    IsReceived  = false;
                    receiveData = new BLEResponsePacket(data);
                }
                else if (data[0] == 0xbe)
                {
                    // CANCEL
                    Logger.Log($"CANCEL");
                }
                else if (data[0] == 0xbf)
                {
                    // ERROR
                    Logger.Log($"ERROR");
                    receiveData = new BLEResponsePacket(data);
                    IsReceived  = true;
                }
                else
                {
                    Logger.Log($"next MSG?");
                    receiveData.Add(data);
                }
            }

            if (receiveData != null)
            {
                if (receiveData.IsReceiveComplete())
                {
                    IsReceived = true;
                    Logger.Log("<<<Receive Complete>>>");
                }
            }
            return;
        }
示例#2
0
 public CTAPBLEReceiver()
 {
     receiveData = new BLEResponsePacket();
 }