Révision 1fd64302 TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs
TestXamConnections/TestXamConnections.Android/Connection/InternConnectionService.cs | ||
---|---|---|
11 | 11 |
using System.Threading.Tasks; |
12 | 12 |
using TestXamConnections.Models; |
13 | 13 |
using TestXamConnections.Connection; |
14 |
using TestXamConnections.Droid.Receivers; |
|
15 |
using TestXamConnections.Helper; |
|
14 | 16 |
|
15 | 17 |
namespace TestXamConnections.Droid.Connection |
16 | 18 |
{ |
... | ... | |
20 | 22 |
*/ |
21 | 23 |
public class InternConnectionService : IConnectionService |
22 | 24 |
{ |
23 |
public EventHandler<byte[]> DataReceivedEvent { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } |
|
25 |
public EventHandler<byte[]> DataReceivedEvent { get; set; } |
|
26 |
public EventHandler<string> ConnectionFailedEvent { get; set; } |
|
24 | 27 |
|
25 |
public Task<bool> Connect(Device device) |
|
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 Task Connect(Dictionary<string, string> param) |
|
26 | 35 |
{ |
27 |
throw new NotImplementedException(); |
|
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 |
appContext.RegisterReceiver(intentReceiver, intentFilter); |
|
46 |
} else |
|
47 |
{ |
|
48 |
ConnectionFailedEvent.Invoke(this, null); |
|
49 |
} |
|
50 |
return null; |
|
51 |
} |
|
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 |
intentReceiver.OnInternDataReceived += OnInternDataReceived; |
|
68 |
IntentFilter intentFilter = new IntentFilter(); |
|
69 |
appContext = Application.Context.ApplicationContext; |
|
70 |
} |
|
71 |
|
|
72 |
private void OnInternDataReceived(object sender, string value) |
|
73 |
{ |
|
74 |
DataReceivedEvent.Invoke(this, new byte[] { 0x06 }); |
|
28 | 75 |
} |
29 | 76 |
|
30 | 77 |
public Task<bool> SendCommand(byte[] hexValues) |
Formats disponibles : Unified diff