public static void Register(BluetoothWT32 wt32)
        {
            if (client != null)
            {
                throw new Exception("InternalCommunications already registered.");
            }
            client = wt32;

            var wt32Parser = new MessageParser();
            wt32Parser.MessageReceived += m =>
            {
                if (m is InternalMessage)
                {
                    ProcessInternalMessage((InternalMessage)m);
                }
                else
                {
                    Manager.EnqueueMessage(m);
                }
            };
            wt32.BTCommandReceived += (s, l, c) =>
            {
                if (l == wt32.SPPLink)
                {
                    try
                    {
                        wt32Parser.Parse(c);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "iBus from WT32", "BT <");
                    }
                }
            };
            Manager.AfterMessageReceived += (m) =>
            {
                if (HasConnection)
                {
                    try
                    {
                        wt32.SendCommand(m.Message.Packet, wt32.SPPLink, "iBus data");
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "iBus to WT32", "> BT");
                    }
                }
            };
        }
Exemple #2
0
        private async void ReadingLoop(DataReader dataReader)
        {
            try
            {
                var parser = new MessageParser();
                parser.MessageReceived += parser_MessageReceived;
                while (true)
                {
                    var size = await dataReader.LoadAsync(1028);
                    var data = new byte[size];
                    dataReader.ReadBytes(data);

                    try
                    {
                        parser.Parse(data);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "Parsing message by SocketClient.");
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "imBMW socket client reading");
                lock (this)
                {
                    if (Socket != null)
                    {
                        Disconnect();
                        Reconnect();
                    }
                }
            }
        }