Statistiques
| Branche: | Révision:

teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ 794b5a8e

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

1
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
using System.Threading.Tasks;
12
using TestXamConnections.Models;
13
using TestXamConnections.Connection;
14
using TestXamConnections.Droid.Receivers;
15
using TestXamConnections.Helper;
16

    
17
namespace TestXamConnections.Droid.Connection
18
{
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
    public class InternConnectionService : IConnectionService
24
    {
25
        public EventHandler<byte[]> DataReceivedEvent { get; set; }
26
        public EventHandler<string> ConnectionFailedEvent { get; set; }
27

    
28
        private readonly IntentReceiver intentReceiver;
29
        private readonly Context appContext;
30

    
31
        /* La fonction connect dans le cas du connexion en interne
32
        *  Est en fait, la configuration de l'IntentReceiver
33
        */
34
        public async Task Connect(Dictionary<string, string> param)
35
        {
36
            if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY))
37
            {
38
                // Récupérations des intents recevable
39
                List<string> intents = new List<string>();
40
                intents.AddRange(param[ConnectionConstants.RECEIVABLE_INTENTS_KEY].Split(","));
41
                // Ajout des intents recevable au IntentFilter
42
                IntentFilter intentFilter = new IntentFilter();
43
                intents.ForEach(intent => intentFilter.AddAction(intent));
44

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

    
54
        public Task<bool> SendCommand(string command)
55
        {
56
            Intent launchIntent = appContext.PackageManager.GetLaunchIntentForPackage(command);
57
            if (launchIntent != null)
58
            {
59
                appContext.StartActivity(launchIntent);
60
                return Task.FromResult(true);
61
            }
62
            return Task.FromResult(false);
63
        }
64

    
65
        public InternConnectionService()
66
        {
67
            intentReceiver = new IntentReceiver();
68
            intentReceiver.OnIntentReceived += IntentReceived;
69
            IntentFilter intentFilter = new IntentFilter();
70
            appContext = Application.Context.ApplicationContext;
71
        }
72

    
73
        private void IntentReceived(object sender, Intent recIntent)
74
        {
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
            }
89
        }
90

    
91
        public Task<bool> SendCommand(byte[] hexValues)
92
        {
93
            throw new NotImplementedException();
94
        }
95
    }
96
}