teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ 28e2e227
Historique | Voir | Annoter | Télécharger (3,677 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 | using TestXamConnections.Helper; |
||
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 | // Récupérations des intents recevable |
||
38 | 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 | public Task<bool> SendCommand(string command) |
||
54 | { |
||
55 | Intent launchIntent = appContext.PackageManager.GetLaunchIntentForPackage(command); |
||
56 | 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 | case AgridentConstants.ACTION_AGRIDENT_SERVICE_START: |
||
86 | DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(AgridentConstants.ACTION_AGRIDENT_SERVICE_START)); |
||
87 | break; |
||
88 | |||
89 | case AgridentConstants.ACTION_AGRIDENT_SERVICE_STOP: |
||
90 | DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(AgridentConstants.ACTION_AGRIDENT_SERVICE_STOP)); |
||
91 | d752dab7 | Martin Toutant | break; |
92 | |||
93 | default: |
||
94 | break; |
||
95 | } |
||
96 | 0875d625 | Martin Toutant | } |
97 | |||
98 | public Task<bool> SendCommand(byte[] hexValues) |
||
99 | { |
||
100 | throw new NotImplementedException(); |
||
101 | } |
||
102 | 58c56df1 | Martin Toutant | } |
103 | } |