teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ c478f8e5
Historique | Voir | Annoter | Télécharger (3,089 ko)
1 | 58c56df1 | Martin Toutant | using Android.App; |
---|---|---|---|
2 | using Android.Content; |
||
3 | using System; |
||
4 | using System.Collections.Generic; |
||
5 | using System.Text; |
||
6 | 0875d625 | Martin Toutant | using System.Threading.Tasks; |
7 | 91ef3e8c | Martin Toutant | using TestXamConnections.Connection; |
8 | 1fd64302 | Martin Toutant | using TestXamConnections.Droid.Receivers; |
9 | c32a628f | Martin Toutant | using TestXamConnections.Device.RFID.Agrident; |
10 | 58c56df1 | Martin Toutant | |
11 | 91ef3e8c | Martin Toutant | namespace TestXamConnections.Droid.Connection |
12 | 58c56df1 | Martin Toutant | { |
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 | 91ef3e8c | Martin Toutant | public class InternConnectionService : IConnectionService |
18 | 58c56df1 | Martin Toutant | { |
19 | 1fd64302 | Martin Toutant | public EventHandler<byte[]> DataReceivedEvent { get; set; } |
20 | 0875d625 | Martin Toutant | |
21 | 1fd64302 | Martin Toutant | 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 | be694ed4 | Martin Toutant | public Task<bool> Connect(Dictionary<string, string> param) |
28 | 0875d625 | Martin Toutant | { |
29 | 1fd64302 | Martin Toutant | if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY)) |
30 | { |
||
31 | 8787b8fb | Martin Toutant | // Récupérations des intents recevable sous forme de csv |
32 | 1fd64302 | Martin Toutant | 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 | be694ed4 | Martin Toutant | appContext.RegisterReceiver(intentReceiver, intentFilter); |
39 | d752dab7 | Martin Toutant | } |
40 | else |
||
41 | 1fd64302 | Martin Toutant | { |
42 | be694ed4 | Martin Toutant | return Task.FromResult(false); |
43 | 1fd64302 | Martin Toutant | } |
44 | be694ed4 | Martin Toutant | return Task.FromResult(true); |
45 | 1fd64302 | Martin Toutant | } |
46 | |||
47 | 3fa496d6 | Martin Toutant | public Task<bool> SendCommand(byte[] commandBytes) |
48 | 1fd64302 | Martin Toutant | { |
49 | 3fa496d6 | Martin Toutant | string command = Encoding.ASCII.GetString(commandBytes); |
50 | Intent launchIntent = appContext.PackageManager.GetLaunchIntentForPackage(command); |
||
51 | 1fd64302 | Martin Toutant | 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 | d752dab7 | Martin Toutant | intentReceiver.OnIntentReceived += IntentReceived; |
63 | 1fd64302 | Martin Toutant | IntentFilter intentFilter = new IntentFilter(); |
64 | appContext = Application.Context.ApplicationContext; |
||
65 | } |
||
66 | |||
67 | d752dab7 | Martin Toutant | private void IntentReceived(object sender, Intent recIntent) |
68 | 1fd64302 | Martin Toutant | { |
69 | d752dab7 | Martin Toutant | 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 | be694ed4 | Martin Toutant | DataReceivedEvent.Invoke(this, Encoding.ASCII.GetBytes(AgridentConstants.ACTION_AGRIDENT_ERROR)); |
78 | break; |
||
79 | |||
80 | d752dab7 | Martin Toutant | default: |
81 | break; |
||
82 | } |
||
83 | 0875d625 | Martin Toutant | } |
84 | 58c56df1 | Martin Toutant | } |
85 | } |