Révision d14bc0b1 TestTeoBluetooth/TestTeoBluetooth.Android/Connection/BluetoothConnectionService.cs
TestTeoBluetooth/TestTeoBluetooth.Android/Connection/BluetoothConnectionService.cs | ||
---|---|---|
24 | 24 |
*/ |
25 | 25 |
public class BluetoothConnectionService : IConnectionService |
26 | 26 |
{ |
27 |
|
|
28 | 27 |
private readonly BluetoothAdapter bluetoothAdapter; |
29 | 28 |
private Thread listeningThread; |
30 | 29 |
private BluetoothSocket socket; |
31 | 30 |
private readonly string sppUUID = "00001101-0000-1000-8000-00805f9b34fb"; |
32 | 31 |
|
33 |
public static readonly BluetoothConnectionService Instance = new BluetoothConnectionService(); |
|
34 |
|
|
35 | 32 |
public BluetoothConnectionService() |
36 | 33 |
{ |
37 | 34 |
bluetoothAdapter = BluetoothAdapter.DefaultAdapter; |
... | ... | |
49 | 46 |
} |
50 | 47 |
|
51 | 48 |
BluetoothDevice droidBtDevice = bluetoothAdapter.GetRemoteDevice(btDevice.MACAddress); |
52 |
//Android.Bluetooth.BluetoothDevice droidBtDevice = adapter.BondedDevices.FirstOrDefault(x => (x.Address == bluetoothDevice.Address)); |
|
53 | 49 |
if (droidBtDevice != null) |
54 | 50 |
{ |
55 | 51 |
// Si le socket est occupé pour une autre connexion |
... | ... | |
67 | 63 |
cts.CancelAfter(50000); |
68 | 64 |
await socket.ConnectAsync().WithCancellation(cts.Token); |
69 | 65 |
|
70 |
if (listeningThread != null) listeningThread.Abort(); |
|
66 |
if (listeningThread != null) |
|
67 |
{ |
|
68 |
listeningThread.Abort(); |
|
69 |
} |
|
70 |
|
|
71 | 71 |
listeningThread = new Thread(async delagate => await ListeningAsync()); |
72 | 72 |
listeningThread.Start(); |
73 | 73 |
|
... | ... | |
82 | 82 |
return false; |
83 | 83 |
} |
84 | 84 |
|
85 |
public EventHandler<byte[]> DataReceivedEvent { get; set; } |
|
86 |
|
|
85 | 87 |
// Fonction d'écoute pour le Thread d'écoute |
86 | 88 |
private async Task ListeningAsync() |
87 | 89 |
{ |
... | ... | |
93 | 95 |
int byteAvailable = await socket.InputStream.ReadAsync(buffer, 0, buffer.Length); |
94 | 96 |
// Resize the byte array |
95 | 97 |
byte[] filledBuffer = buffer.Take(byteAvailable).ToArray(); |
96 |
Application.SynchronizationContext.Post(_ => { OnDataReceived(filledBuffer); }, null); |
|
98 |
// Trigger DataReceivedEvent |
|
99 |
Application.SynchronizationContext.Post(_ => { DataReceivedEvent.Invoke(this, filledBuffer); }, null); |
|
97 | 100 |
} |
98 | 101 |
} |
99 | 102 |
catch (IOException ex) |
... | ... | |
110 | 113 |
} |
111 | 114 |
} |
112 | 115 |
|
113 |
public EventHandler<string> DataReceivedEvent { get; set; } |
|
114 |
private string _bufferedData; |
|
115 |
|
|
116 |
/* |
|
117 |
* Fonction appelée à la reception de données |
|
118 |
* Trigger l'evenement DataReceivedEvent |
|
119 |
* |
|
120 |
* IMPORTANT: Les trames peuvent arriver découpées, |
|
121 |
* Donc les données sont gardées dans un buffer d'envoi |
|
122 |
* nommé _bufferedData |
|
123 |
*/ |
|
124 |
private void OnDataReceived(byte[] buffer) |
|
125 |
{ |
|
126 |
// Si le premier character est 2 (ASCII: STX) |
|
127 |
// Alors c'est le début de la trame. |
|
128 |
if (buffer[0] == 2) |
|
129 |
{ |
|
130 |
_bufferedData = ""; |
|
131 |
// Si character seul on quitte |
|
132 |
if (buffer.Length == 1) |
|
133 |
{ |
|
134 |
return; |
|
135 |
} |
|
136 |
// On enlève STX |
|
137 |
buffer = buffer.Skip(1).ToArray(); |
|
138 |
// On mets le début de la trame dans buffer d'envoi |
|
139 |
_bufferedData += Encoding.ASCII.GetString(buffer); |
|
140 |
} |
|
141 |
|
|
142 |
// Si le dernier character est 13 (ASCII: CR) |
|
143 |
// Alors la trame est terminée |
|
144 |
if (buffer[^1] == 13) |
|
145 |
{ |
|
146 |
// On enlève CR |
|
147 |
buffer = buffer.SkipLast(1).ToArray(); |
|
148 |
// Conversion en chaîne de caractères |
|
149 |
// Et on complète le buffer d'envoi |
|
150 |
_bufferedData += Encoding.ASCII.GetString(buffer); |
|
151 |
// Trigger evènement |
|
152 |
DataReceivedEvent.Invoke(this, _bufferedData); |
|
153 |
} |
|
154 |
} |
|
155 | 116 |
|
156 | 117 |
private Task<bool> CloseConnection() |
157 | 118 |
{ |
Formats disponibles : Unified diff