Statistiques
| Branche: | Révision:

teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ c478f8e5

Historique | Voir | Annoter | Télécharger (3,089 ko)

1
using Android.App;
2
using Android.Content;
3
using System;
4
using System.Collections.Generic;
5
using System.Text;
6
using System.Threading.Tasks;
7
using TestXamConnections.Connection;
8
using TestXamConnections.Droid.Receivers;
9
using TestXamConnections.Device.RFID.Agrident;
10

    
11
namespace TestXamConnections.Droid.Connection
12
{
13
    /* Classe permettant d'établir une avec une applications tiers
14
     * Pré-installée sur l'appareil
15
     * Basée sur filtrage d'intent sur BroadcastReceiver
16
     */
17
    public class InternConnectionService : IConnectionService
18
    {
19
        public EventHandler<byte[]> DataReceivedEvent { get; set; }
20

    
21
        private readonly IntentReceiver intentReceiver;
22
        private readonly Context appContext;
23

    
24
        /* La fonction connect dans le cas du connexion en interne
25
        *  Est en fait, la configuration de l'IntentReceiver
26
        */
27
        public Task<bool> Connect(Dictionary<string, string> param)
28
        {
29
            if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY))
30
            {
31
                // Récupérations des intents recevable sous forme de csv
32
                List<string> intents = new List<string>();
33
                intents.AddRange(param[ConnectionConstants.RECEIVABLE_INTENTS_KEY].Split(","));
34
                // Ajout des intents recevable au IntentFilter
35
                IntentFilter intentFilter = new IntentFilter();
36
                intents.ForEach(intent => intentFilter.AddAction(intent));
37

    
38
                appContext.RegisterReceiver(intentReceiver, intentFilter);
39
            }
40
            else
41
            {
42
                return Task.FromResult(false);
43
            }
44
            return Task.FromResult(true);
45
        }
46

    
47
        public Task<bool> SendCommand(byte[] commandBytes)
48
        {
49
            string command = Encoding.ASCII.GetString(commandBytes);
50
            Intent launchIntent = appContext.PackageManager.GetLaunchIntentForPackage(command);
51
            if (launchIntent != null)
52
            {
53
                appContext.StartActivity(launchIntent);
54
                return Task.FromResult(true);
55
            }
56
            return Task.FromResult(false);
57
        }
58

    
59
        public InternConnectionService()
60
        {
61
            intentReceiver = new IntentReceiver();
62
            intentReceiver.OnIntentReceived += IntentReceived;
63
            IntentFilter intentFilter = new IntentFilter();
64
            appContext = Application.Context.ApplicationContext;
65
        }
66

    
67
        private void IntentReceived(object sender, Intent recIntent)
68
        {
69
            switch (recIntent.Action)
70
            {
71
                case AgridentConstants.ACTION_AGRIDENT_SUCCESS:
72
                    string readData = recIntent.GetStringExtra(AgridentConstants.KEY_BARCODE_DATA);
73
                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(readData));
74
                    break;
75

    
76
                case AgridentConstants.ACTION_AGRIDENT_ERROR:
77
                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(AgridentConstants.ACTION_AGRIDENT_ERROR));
78
                    break;
79

    
80
                default:
81
                    break;
82
            }
83
        }
84
    }
85
}