commit d752dab75fb04ca11d1a63a843fe2725b70c62fc
Author: Martin Toutant <martin.toutant@inrae.fr>
Date:   Wed Aug 17 20:44:38 2022 +0200

    generic intent filter

diff --git a/TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs b/TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs
index e5e38d7..ba1800f 100644
--- a/TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs
+++ b/TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs
@@ -31,7 +31,7 @@ namespace TestXamConnections.Droid.Connection
         /* La fonction connect dans le cas du connexion en interne
         *  Est en fait, la configuration de l'IntentReceiver
         */
-        public Task Connect(Dictionary<string, string> param)
+        public async Task Connect(Dictionary<string, string> param)
         {
             if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY))
             {
@@ -43,11 +43,12 @@ namespace TestXamConnections.Droid.Connection
                 intents.ForEach(intent => intentFilter.AddAction(intent));
 
                 appContext.RegisterReceiver(intentReceiver, intentFilter);
-            } else
+            }
+            else
             {
                 ConnectionFailedEvent.Invoke(this, null);
             }
-            return null;
+            return;
         }
 
         public Task<bool> SendCommand(string command)
@@ -64,14 +65,27 @@ namespace TestXamConnections.Droid.Connection
         public InternConnectionService()
         {
             intentReceiver = new IntentReceiver();
-            intentReceiver.OnInternDataReceived += OnInternDataReceived;
+            intentReceiver.OnIntentReceived += IntentReceived;
             IntentFilter intentFilter = new IntentFilter();
             appContext = Application.Context.ApplicationContext;
         }
 
-        private void OnInternDataReceived(object sender, string value)
+        private void IntentReceived(object sender, Intent recIntent)
         {
-            DataReceivedEvent.Invoke(this, new byte[] { 0x06 });
+            switch (recIntent.Action)
+            {
+                case AgridentConstants.ACTION_AGRIDENT_SUCCESS:
+                    string readData = recIntent.GetStringExtra(AgridentConstants.KEY_BARCODE_DATA);
+                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(readData));
+                    break;
+
+                case AgridentConstants.ACTION_AGRIDENT_ERROR:
+                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes("ERROR"));
+                    break;
+
+                default:
+                    break;
+            }
         }
 
         public Task<bool> SendCommand(byte[] hexValues)
diff --git a/TestXamConnections/TestXamConnections.Android/Connection/WifiConnectionService.cs b/TestXamConnections/TestXamConnections.Android/Connection/WifiConnectionService.cs
index 6f98bc7..dc7fb5d 100644
--- a/TestXamConnections/TestXamConnections.Android/Connection/WifiConnectionService.cs
+++ b/TestXamConnections/TestXamConnections.Android/Connection/WifiConnectionService.cs
@@ -22,17 +22,17 @@ namespace TestXamConnections.Droid.Connection
         public EventHandler<byte[]> DataReceivedEvent { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
         public EventHandler<string> ConnectionFailedEvent { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
 
-        public Task<bool> Connect(Dictionary<string, string> connectParam)
+        public Task<bool> SendCommand(byte[] hexValues)
         {
             throw new NotImplementedException();
         }
 
-        public Task<bool> SendCommand(byte[] hexValues)
+        public Task<bool> SendCommand(string command)
         {
             throw new NotImplementedException();
         }
 
-        public Task<bool> SendCommand(string command)
+        public Task Connect(Dictionary<string, string> connectParam)
         {
             throw new NotImplementedException();
         }
diff --git a/TestXamConnections/TestXamConnections.Android/Receivers/IntentReceiver.cs b/TestXamConnections/TestXamConnections.Android/Receivers/IntentReceiver.cs
index 705ff76..d80e137 100644
--- a/TestXamConnections/TestXamConnections.Android/Receivers/IntentReceiver.cs
+++ b/TestXamConnections/TestXamConnections.Android/Receivers/IntentReceiver.cs
@@ -8,21 +8,17 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using TestXamConnections.Models;
 
 namespace TestXamConnections.Droid.Receivers
 {
     public class IntentReceiver : BroadcastReceiver
     {
-
-        public EventHandler<string> OnInternDataReceived;
+        public EventHandler<Intent> OnIntentReceived;
 
         public override void OnReceive(Context context, Intent intent)
         {
-            StringBuilder sb = new StringBuilder();
-            sb.Append("Action:  " + intent.Action + "\n");
-            sb.Append("URI: " + intent.ToUri(IntentUriType.Scheme).ToString() + "\n");
-            string log = sb.ToString();
-            OnInternDataReceived.Invoke(this, log);
+            OnIntentReceived(this, intent);
         }
     }
 }
\ No newline at end of file
diff --git a/TestXamConnections/TestXamConnections/Models/AgridentReader.cs b/TestXamConnections/TestXamConnections/Models/AgridentReader.cs
index cb753a9..e186410 100644
--- a/TestXamConnections/TestXamConnections/Models/AgridentReader.cs
+++ b/TestXamConnections/TestXamConnections/Models/AgridentReader.cs
@@ -21,37 +21,47 @@ namespace TestXamConnections.Models
 
     public class AgridentReader
     {
-
         private IConnectionService ConnectionService { get; set; }
         public EventHandler<string> AgridentDataReceivedEvent { get; set; }
         public EventHandler<string> AgridentNORFIDEvent { get; set; }
+        public EventHandler AgridentConnectionFailedEvent { get; set; }
 
         public AgridentReader()
         {
             ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionInstance(ConnectionType.Intern);
-            ConnectionService.DataReceivedEvent += (s, args) => AgridentDataReceived(args);
+            ConnectionService.DataReceivedEvent += (s, args) => InternDataReceived(args);
+            ConnectionService.ConnectionFailedEvent += AgridentConnectionFailed;
         }
 
         ~AgridentReader()
         {
-            ConnectionService.DataReceivedEvent -= (s, args) => AgridentDataReceived(args);
+            ConnectionService.DataReceivedEvent -= (s, args) => InternDataReceived(args);
+            ConnectionService.ConnectionFailedEvent += AgridentConnectionFailed;
+        }
+
+        /* Fonction déclenchée à l'échec de la connexion
+         */
+        private void AgridentConnectionFailed(object sender, string e)
+        {
+            AgridentConnectionFailedEvent.Invoke(this, null);
         }
 
         /* Fonction déclenchée à la reception de données Agrident
          * Trigger l'évènement AgridentDataReceivedEvent
          */
-        private void AgridentDataReceived(byte[] args)
-        { 
+        private void InternDataReceived(byte[] intent)
+        {
+            string data = Encoding.ASCII.GetString(intent);
             // Si pas de valeur scannée
             // Evenement error
-            if (args[0] == 0x6)
+            if (data.Equals("ERROR"))
             {
                 AgridentNORFIDEvent.Invoke(this, null);
             } 
-            // Sinon c'est une valeur rfid
+            // Sinon c'est une valeur rfid -- NON, TODO
             else
             {
-                AgridentDataReceivedEvent.Invoke(this, Encoding.ASCII.GetString(args));
+                AgridentDataReceivedEvent.Invoke(this, data);
             }
         }
 
diff --git a/TestXamConnections/TestXamConnections/ViewModels/InternServiceViewModel.cs b/TestXamConnections/TestXamConnections/ViewModels/InternServiceViewModel.cs
index 7f8abe2..1d801a3 100644
--- a/TestXamConnections/TestXamConnections/ViewModels/InternServiceViewModel.cs
+++ b/TestXamConnections/TestXamConnections/ViewModels/InternServiceViewModel.cs
@@ -12,7 +12,7 @@ namespace TestXamConnections.ViewModels
     {
         private bool showRFID;
         private bool showError;
-        private AgridentReader agridentReader;
+        private readonly AgridentReader agridentReader;
         private string rfidResult;
         private string errorMessage;
 
@@ -56,10 +56,12 @@ namespace TestXamConnections.ViewModels
         {
             ShowRFID = false;
             ShowError = false;
+            RFIDResult = "";
+            ErrorMessage = "";
             agridentReader = new AgridentReader();
-            agridentReader.EnableService().Wait();
             agridentReader.AgridentDataReceivedEvent += OnRFIDResultReceived;
             agridentReader.AgridentNORFIDEvent += OnNoRFIDReceived;
+            agridentReader.EnableService().Wait();
             StartScanCommand = new Command(async () => await StartScan());
         }
 
