Statistiques
| Branche: | Révision:

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

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

1 58c56df1 Martin Toutant
using Android.App;
2
using Android.Content;
3
using Android.OS;
4
using Android.Runtime;
5
using Android.Views;
6
using Android.Widget;
7
using System;
8
using System.Collections.Generic;
9
using System.Linq;
10
using System.Text;
11 0875d625 Martin Toutant
using System.Threading.Tasks;
12 91ef3e8c Martin Toutant
using TestXamConnections.Models;
13
using TestXamConnections.Connection;
14 1fd64302 Martin Toutant
using TestXamConnections.Droid.Receivers;
15 c32a628f Martin Toutant
using TestXamConnections.Device.RFID.Agrident;
16 58c56df1 Martin Toutant
17 91ef3e8c Martin Toutant
namespace TestXamConnections.Droid.Connection
18 58c56df1 Martin Toutant
{
19
    /* Classe permettant d'établir une avec une applications tiers
20
     * Pré-installée sur l'appareil
21
     * Basée sur filtrage d'intent sur BroadcastReceiver
22
     */
23 91ef3e8c Martin Toutant
    public class InternConnectionService : IConnectionService
24 58c56df1 Martin Toutant
    {
25 1fd64302 Martin Toutant
        public EventHandler<byte[]> DataReceivedEvent { get; set; }
26 0875d625 Martin Toutant
27 1fd64302 Martin Toutant
        private readonly IntentReceiver intentReceiver;
28
        private readonly Context appContext;
29
30
        /* La fonction connect dans le cas du connexion en interne
31
        *  Est en fait, la configuration de l'IntentReceiver
32
        */
33 be694ed4 Martin Toutant
        public Task<bool> Connect(Dictionary<string, string> param)
34 0875d625 Martin Toutant
        {
35 1fd64302 Martin Toutant
            if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY))
36
            {
37 8787b8fb Martin Toutant
                // Récupérations des intents recevable sous forme de csv
38 1fd64302 Martin Toutant
                List<string> intents = new List<string>();
39
                intents.AddRange(param[ConnectionConstants.RECEIVABLE_INTENTS_KEY].Split(","));
40
                // Ajout des intents recevable au IntentFilter
41
                IntentFilter intentFilter = new IntentFilter();
42
                intents.ForEach(intent => intentFilter.AddAction(intent));
43
44 be694ed4 Martin Toutant
                appContext.RegisterReceiver(intentReceiver, intentFilter);
45 d752dab7 Martin Toutant
            }
46
            else
47 1fd64302 Martin Toutant
            {
48 be694ed4 Martin Toutant
                return Task.FromResult(false);
49 1fd64302 Martin Toutant
            }
50 be694ed4 Martin Toutant
            return Task.FromResult(true);
51 1fd64302 Martin Toutant
        }
52
53 06a15704 Martin Toutant
        public Task<bool> SendCommand(IConvertible command)
54 1fd64302 Martin Toutant
        {
55 06a15704 Martin Toutant
            Intent launchIntent = appContext.PackageManager.GetLaunchIntentForPackage(command.ToString());
56 1fd64302 Martin Toutant
            if (launchIntent != null)
57
            {
58
                appContext.StartActivity(launchIntent);
59
                return Task.FromResult(true);
60
            }
61
            return Task.FromResult(false);
62
        }
63
64
        public InternConnectionService()
65
        {
66
            intentReceiver = new IntentReceiver();
67 d752dab7 Martin Toutant
            intentReceiver.OnIntentReceived += IntentReceived;
68 1fd64302 Martin Toutant
            IntentFilter intentFilter = new IntentFilter();
69
            appContext = Application.Context.ApplicationContext;
70
        }
71
72 d752dab7 Martin Toutant
        private void IntentReceived(object sender, Intent recIntent)
73 1fd64302 Martin Toutant
        {
74 d752dab7 Martin Toutant
            switch (recIntent.Action)
75
            {
76
                case AgridentConstants.ACTION_AGRIDENT_SUCCESS:
77
                    string readData = recIntent.GetStringExtra(AgridentConstants.KEY_BARCODE_DATA);
78
                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(readData));
79
                    break;
80
81
                case AgridentConstants.ACTION_AGRIDENT_ERROR:
82 be694ed4 Martin Toutant
                    DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(AgridentConstants.ACTION_AGRIDENT_ERROR));
83
                    break;
84
85 d752dab7 Martin Toutant
                default:
86
                    break;
87
            }
88 0875d625 Martin Toutant
        }
89
90
        public Task<bool> SendCommand(byte[] hexValues)
91
        {
92
            throw new NotImplementedException();
93
        }
94 58c56df1 Martin Toutant
    }
95
}