teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ master
Historique | Voir | Annoter | Télécharger (3,097 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 InternConnexionService : IConnexionService |
18 |
{ |
19 |
public EventHandler<byte[]> DonneesRecuesEvent { 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> Connexion(Dictionary<string, string> param) |
28 |
{ |
29 |
if (param.ContainsKey(ConnexionConstantes.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[ConnexionConstantes.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> EnvoiCommande(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 InternConnexionService() |
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 AgridentConstantes.ACTION_AGRIDENT_SUCCESS: |
72 |
string readData = recIntent.GetStringExtra(AgridentConstantes.KEY_BARCODE_DATA); |
73 |
DonneesRecuesEvent.Invoke(this, Encoding.ASCII.GetBytes(readData)); |
74 |
break; |
75 |
|
76 |
case AgridentConstantes.ACTION_AGRIDENT_ERROR: |
77 |
DonneesRecuesEvent.Invoke(this, Encoding.ASCII.GetBytes(AgridentConstantes.ACTION_AGRIDENT_ERROR)); |
78 |
break; |
79 |
|
80 |
default: |
81 |
break; |
82 |
} |
83 |
} |
84 |
} |
85 |
} |