teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / InternConnectionService.cs @ 1fd64302
Historique | Voir | Annoter | Télécharger (2,772 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 |
public EventHandler<string> ConnectionFailedEvent { get; set; } |
27 |
|
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) |
35 |
{ |
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 }); |
75 |
} |
76 |
|
77 |
public Task<bool> SendCommand(byte[] hexValues) |
78 |
{ |
79 |
throw new NotImplementedException(); |
80 |
} |
81 |
} |
82 |
} |