Révision d752dab7

Voir les différences:

TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs
31 31
        /* La fonction connect dans le cas du connexion en interne
32 32
        *  Est en fait, la configuration de l'IntentReceiver
33 33
        */
34
        public Task Connect(Dictionary<string, string> param)
34
        public async Task Connect(Dictionary<string, string> param)
35 35
        {
36 36
            if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY))
37 37
            {
......
43 43
                intents.ForEach(intent => intentFilter.AddAction(intent));
44 44

  
45 45
                appContext.RegisterReceiver(intentReceiver, intentFilter);
46
            } else
46
            }
47
            else
47 48
            {
48 49
                ConnectionFailedEvent.Invoke(this, null);
49 50
            }
50
            return null;
51
            return;
51 52
        }
52 53

  
53 54
        public Task<bool> SendCommand(string command)
......
64 65
        public InternConnectionService()
65 66
        {
66 67
            intentReceiver = new IntentReceiver();
67
            intentReceiver.OnInternDataReceived += OnInternDataReceived;
68
            intentReceiver.OnIntentReceived += IntentReceived;
68 69
            IntentFilter intentFilter = new IntentFilter();
69 70
            appContext = Application.Context.ApplicationContext;
70 71
        }
71 72

  
72
        private void OnInternDataReceived(object sender, string value)
73
        private void IntentReceived(object sender, Intent recIntent)
73 74
        {
74
            DataReceivedEvent.Invoke(this, new byte[] { 0x06 });
75
            switch (recIntent.Action)
76
            {
77
                case AgridentConstants.ACTION_AGRIDENT_SUCCESS:
78
                    string readData = recIntent.GetStringExtra(AgridentConstants.KEY_BARCODE_DATA);
79
                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(readData));
80
                    break;
81

  
82
                case AgridentConstants.ACTION_AGRIDENT_ERROR:
83
                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes("ERROR"));
84
                    break;
85

  
86
                default:
87
                    break;
88
            }
75 89
        }
76 90

  
77 91
        public Task<bool> SendCommand(byte[] hexValues)
TestXamConnections/TestXamConnections.Android/Connection/WifiConnectionService.cs
22 22
        public EventHandler<byte[]> DataReceivedEvent { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
23 23
        public EventHandler<string> ConnectionFailedEvent { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
24 24

  
25
        public Task<bool> Connect(Dictionary<string, string> connectParam)
25
        public Task<bool> SendCommand(byte[] hexValues)
26 26
        {
27 27
            throw new NotImplementedException();
28 28
        }
29 29

  
30
        public Task<bool> SendCommand(byte[] hexValues)
30
        public Task<bool> SendCommand(string command)
31 31
        {
32 32
            throw new NotImplementedException();
33 33
        }
34 34

  
35
        public Task<bool> SendCommand(string command)
35
        public Task Connect(Dictionary<string, string> connectParam)
36 36
        {
37 37
            throw new NotImplementedException();
38 38
        }
TestXamConnections/TestXamConnections.Android/Receivers/IntentReceiver.cs
8 8
using System.Collections.Generic;
9 9
using System.Linq;
10 10
using System.Text;
11
using TestXamConnections.Models;
11 12

  
12 13
namespace TestXamConnections.Droid.Receivers
13 14
{
14 15
    public class IntentReceiver : BroadcastReceiver
15 16
    {
16

  
17
        public EventHandler<string> OnInternDataReceived;
17
        public EventHandler<Intent> OnIntentReceived;
18 18

  
19 19
        public override void OnReceive(Context context, Intent intent)
20 20
        {
21
            StringBuilder sb = new StringBuilder();
22
            sb.Append("Action:  " + intent.Action + "\n");
23
            sb.Append("URI: " + intent.ToUri(IntentUriType.Scheme).ToString() + "\n");
24
            string log = sb.ToString();
25
            OnInternDataReceived.Invoke(this, log);
21
            OnIntentReceived(this, intent);
26 22
        }
27 23
    }
28 24
}
TestXamConnections/TestXamConnections/Models/AgridentReader.cs
21 21

  
22 22
    public class AgridentReader
23 23
    {
24

  
25 24
        private IConnectionService ConnectionService { get; set; }
26 25
        public EventHandler<string> AgridentDataReceivedEvent { get; set; }
27 26
        public EventHandler<string> AgridentNORFIDEvent { get; set; }
27
        public EventHandler AgridentConnectionFailedEvent { get; set; }
28 28

  
29 29
        public AgridentReader()
30 30
        {
31 31
            ConnectionService = DependencyService.Get<IConnectionServiceProvider>().GetConnectionInstance(ConnectionType.Intern);
32
            ConnectionService.DataReceivedEvent += (s, args) => AgridentDataReceived(args);
32
            ConnectionService.DataReceivedEvent += (s, args) => InternDataReceived(args);
33
            ConnectionService.ConnectionFailedEvent += AgridentConnectionFailed;
33 34
        }
34 35

  
35 36
        ~AgridentReader()
36 37
        {
37
            ConnectionService.DataReceivedEvent -= (s, args) => AgridentDataReceived(args);
38
            ConnectionService.DataReceivedEvent -= (s, args) => InternDataReceived(args);
39
            ConnectionService.ConnectionFailedEvent += AgridentConnectionFailed;
40
        }
41

  
42
        /* Fonction déclenchée à l'échec de la connexion
43
         */
44
        private void AgridentConnectionFailed(object sender, string e)
45
        {
46
            AgridentConnectionFailedEvent.Invoke(this, null);
38 47
        }
39 48

  
40 49
        /* Fonction déclenchée à la reception de données Agrident
41 50
         * Trigger l'évènement AgridentDataReceivedEvent
42 51
         */
43
        private void AgridentDataReceived(byte[] args)
44
        { 
52
        private void InternDataReceived(byte[] intent)
53
        {
54
            string data = Encoding.ASCII.GetString(intent);
45 55
            // Si pas de valeur scannée
46 56
            // Evenement error
47
            if (args[0] == 0x6)
57
            if (data.Equals("ERROR"))
48 58
            {
49 59
                AgridentNORFIDEvent.Invoke(this, null);
50 60
            } 
51
            // Sinon c'est une valeur rfid
61
            // Sinon c'est une valeur rfid -- NON, TODO
52 62
            else
53 63
            {
54
                AgridentDataReceivedEvent.Invoke(this, Encoding.ASCII.GetString(args));
64
                AgridentDataReceivedEvent.Invoke(this, data);
55 65
            }
56 66
        }
57 67

  
TestXamConnections/TestXamConnections/ViewModels/InternServiceViewModel.cs
12 12
    {
13 13
        private bool showRFID;
14 14
        private bool showError;
15
        private AgridentReader agridentReader;
15
        private readonly AgridentReader agridentReader;
16 16
        private string rfidResult;
17 17
        private string errorMessage;
18 18

  
......
56 56
        {
57 57
            ShowRFID = false;
58 58
            ShowError = false;
59
            RFIDResult = "";
60
            ErrorMessage = "";
59 61
            agridentReader = new AgridentReader();
60
            agridentReader.EnableService().Wait();
61 62
            agridentReader.AgridentDataReceivedEvent += OnRFIDResultReceived;
62 63
            agridentReader.AgridentNORFIDEvent += OnNoRFIDReceived;
64
            agridentReader.EnableService().Wait();
63 65
            StartScanCommand = new Command(async () => await StartScan());
64 66
        }
65 67

  

Formats disponibles : Unified diff