teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ 06a15704
Historique | Voir | Annoter | Télécharger (3,711 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 |
|
27 |
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 |
public Task<bool> Connect(Dictionary<string, string> param) |
34 |
{ |
35 |
if (param.ContainsKey(ConnectionConstants.RECEIVABLE_INTENTS_KEY)) |
36 |
{ |
37 |
// Récupérations des intents recevable sous forme de csv |
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 |
appContext.RegisterReceiver(intentReceiver, intentFilter); |
45 |
} |
46 |
else |
47 |
{ |
48 |
return Task.FromResult(false); |
49 |
} |
50 |
return Task.FromResult(true); |
51 |
} |
52 |
|
53 |
public Task<bool> SendCommand(IConvertible command) |
54 |
{ |
55 |
Intent launchIntent = appContext.PackageManager.GetLaunchIntentForPackage(command.ToString()); |
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 |
intentReceiver.OnIntentReceived += IntentReceived; |
68 |
IntentFilter intentFilter = new IntentFilter(); |
69 |
appContext = Application.Context.ApplicationContext; |
70 |
} |
71 |
|
72 |
private void IntentReceived(object sender, Intent recIntent) |
73 |
{ |
74 |
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 |
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 |
break; |
92 |
|
93 |
default: |
94 |
break; |
95 |
} |
96 |
} |
97 |
|
98 |
public Task<bool> SendCommand(byte[] hexValues) |
99 |
{ |
100 |
throw new NotImplementedException(); |
101 |
} |
102 |
} |
103 |
} |