teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / BluetoothConnectionService.cs @ 340558f2
Historique | Voir | Annoter | Télécharger (4,646 ko)
1 |
using Android.App; |
---|---|
2 |
using Android.Bluetooth; |
3 |
using Java.Util; |
4 |
using System; |
5 |
using System.Collections.Generic; |
6 |
using System.IO; |
7 |
using System.Linq; |
8 |
using System.Threading; |
9 |
using System.Threading.Tasks; |
10 |
using TestXamConnections.Droid.Utils; |
11 |
using TestXamConnections.Connection; |
12 |
|
13 |
namespace TestXamConnections.Droid.Connection |
14 |
{ |
15 |
/* Classe permettant d'établir une connection Bluetooth |
16 |
* Avec socket RFCOMM connecté au SPP |
17 |
*/ |
18 |
public class BluetoothConnexionService : IConnexionService |
19 |
{ |
20 |
private readonly BluetoothAdapter bluetoothAdapter; |
21 |
private Thread listeningThread; |
22 |
private BluetoothSocket socket; |
23 |
private readonly string sppUUID = "00001101-0000-1000-8000-00805f9b34fb"; |
24 |
|
25 |
public BluetoothConnexionService() |
26 |
{ |
27 |
bluetoothAdapter = BluetoothAdapter.DefaultAdapter; |
28 |
} |
29 |
|
30 |
// Pour le bluetooth, le paramètre de connection est l'addresse MAC |
31 |
public async Task<bool> Connexion(Dictionary<string, string> param) |
32 |
{ |
33 |
try |
34 |
{ |
35 |
// Si jamais on est en train de scanner |
36 |
// On annule le scan |
37 |
if (bluetoothAdapter.IsDiscovering) |
38 |
{ |
39 |
_ = bluetoothAdapter.CancelDiscovery(); |
40 |
} |
41 |
|
42 |
if (param.ContainsKey(ConnexionConstantes.MAC_ADDR_KEY)) |
43 |
{ |
44 |
BluetoothDevice droidBtDevice = bluetoothAdapter.GetRemoteDevice(param[ConnexionConstantes.MAC_ADDR_KEY]); |
45 |
if (droidBtDevice != null) |
46 |
{ |
47 |
// Si le socket est occupé pour une autre connexion |
48 |
// On ferme la connexion existante |
49 |
if (socket != null) |
50 |
{ |
51 |
_ = await CloseConnection(); |
52 |
} |
53 |
|
54 |
// Creation du canal RFCOMM (non sécurisé) |
55 |
socket = droidBtDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString(sppUUID)); |
56 |
|
57 |
CancellationTokenSource cts = new CancellationTokenSource(); |
58 |
|
59 |
cts.CancelAfter(50000); |
60 |
await socket.ConnectAsync().WithCancellation(cts.Token); |
61 |
|
62 |
if (listeningThread != null) |
63 |
{ |
64 |
listeningThread.Abort(); |
65 |
} |
66 |
|
67 |
listeningThread = new Thread(async delagate => await ListeningAsync()); |
68 |
listeningThread.Start(); |
69 |
} |
70 |
} else |
71 |
{ |
72 |
// L'addresse MAC n'a pas été spécifiée |
73 |
return false; |
74 |
} |
75 |
} |
76 |
catch (Exception) |
77 |
{ |
78 |
return false; |
79 |
} |
80 |
return true; |
81 |
} |
82 |
|
83 |
public EventHandler<byte[]> DonneesRecuesEvent { get; set; } |
84 |
|
85 |
// Fonction d'écoute pour le Thread d'écoute |
86 |
private async Task ListeningAsync() |
87 |
{ |
88 |
try |
89 |
{ |
90 |
byte[] buffer = new byte[1024]; |
91 |
while (socket.IsConnected) |
92 |
{ |
93 |
int byteAvailable = await socket.InputStream.ReadAsync(buffer, 0, buffer.Length); |
94 |
// Resize the byte array |
95 |
byte[] filledBuffer = buffer.Take(byteAvailable).ToArray(); |
96 |
// Trigger DataReceivedEvent |
97 |
Application.SynchronizationContext.Post(_ => { DonneesRecuesEvent.Invoke(this, filledBuffer); }, null); |
98 |
} |
99 |
} |
100 |
catch (IOException ex) |
101 |
{ |
102 |
System.Diagnostics.Debug.WriteLine(ex); |
103 |
} |
104 |
catch (ThreadAbortException taEx) |
105 |
{ |
106 |
System.Diagnostics.Debug.WriteLine(taEx); |
107 |
} |
108 |
catch (Exception ex) |
109 |
{ |
110 |
System.Diagnostics.Debug.WriteLine(ex); |
111 |
} |
112 |
} |
113 |
|
114 |
|
115 |
private Task<bool> CloseConnection() |
116 |
{ |
117 |
while (socket.IsConnected) socket.Close(); |
118 |
return Task.FromResult(true); |
119 |
} |
120 |
|
121 |
public Task<bool> EnvoiCommande(byte[] command) |
122 |
{ |
123 |
|
124 |
if (socket.IsConnected == false) |
125 |
{ |
126 |
return Task.FromResult(false); |
127 |
} |
128 |
else |
129 |
{ |
130 |
// Envoi de la commande |
131 |
socket.OutputStream.Write(command, 0, command.Length); |
132 |
socket.OutputStream.Flush(); |
133 |
} |
134 |
return Task.FromResult(true); |
135 |
} |
136 |
|
137 |
public Task<bool> SendCommand(string command) |
138 |
{ |
139 |
throw new NotImplementedException(); |
140 |
} |
141 |
} |
142 |
} |