teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / BluetoothConnectionService.cs @ c478f8e5
Historique | Voir | Annoter | Télécharger (4,644 ko)
1 | 58c56df1 | Martin Toutant | using Android.App; |
---|---|---|---|
2 | 0875d625 | Martin Toutant | using Android.Bluetooth; |
3 | using Java.Util; |
||
4 | 58c56df1 | Martin Toutant | using System; |
5 | using System.Collections.Generic; |
||
6 | 0875d625 | Martin Toutant | using System.IO; |
7 | 58c56df1 | Martin Toutant | using System.Linq; |
8 | 0875d625 | Martin Toutant | using System.Threading; |
9 | using System.Threading.Tasks; |
||
10 | 91ef3e8c | Martin Toutant | using TestXamConnections.Droid.Utils; |
11 | using TestXamConnections.Connection; |
||
12 | 58c56df1 | Martin Toutant | |
13 | 91ef3e8c | Martin Toutant | namespace TestXamConnections.Droid.Connection |
14 | 58c56df1 | Martin Toutant | { |
15 | /* Classe permettant d'établir une connection Bluetooth |
||
16 | * Avec socket RFCOMM connecté au SPP |
||
17 | */ |
||
18 | 91ef3e8c | Martin Toutant | public class BluetoothConnectionService : IConnectionService |
19 | 58c56df1 | Martin Toutant | { |
20 | 0875d625 | Martin Toutant | private readonly BluetoothAdapter bluetoothAdapter; |
21 | private Thread listeningThread; |
||
22 | private BluetoothSocket socket; |
||
23 | private readonly string sppUUID = "00001101-0000-1000-8000-00805f9b34fb"; |
||
24 | |||
25 | 91ef3e8c | Martin Toutant | public BluetoothConnectionService() |
26 | be694ed4 | Martin Toutant | { |
27 | 0875d625 | Martin Toutant | bluetoothAdapter = BluetoothAdapter.DefaultAdapter; |
28 | } |
||
29 | 91ef3e8c | Martin Toutant | |
30 | 1fd64302 | Martin Toutant | // Pour le bluetooth, le paramètre de connection est l'addresse MAC |
31 | be694ed4 | Martin Toutant | public async Task<bool> Connect(Dictionary<string, string> param) |
32 | 0875d625 | Martin Toutant | { |
33 | try |
||
34 | { |
||
35 | // Si jamais on est en train de scanner |
||
36 | // On annule le scan |
||
37 | if (bluetoothAdapter.IsDiscovering) |
||
38 | { |
||
39 | be694ed4 | Martin Toutant | _ = bluetoothAdapter.CancelDiscovery(); |
40 | 0875d625 | Martin Toutant | } |
41 | |||
42 | 1fd64302 | Martin Toutant | if (param.ContainsKey(ConnectionConstants.MAC_ADDR_KEY)) |
43 | 0875d625 | Martin Toutant | { |
44 | 1fd64302 | Martin Toutant | BluetoothDevice droidBtDevice = bluetoothAdapter.GetRemoteDevice(param[ConnectionConstants.MAC_ADDR_KEY]); |
45 | if (droidBtDevice != null) |
||
46 | 0875d625 | Martin Toutant | { |
47 | 1fd64302 | Martin Toutant | // Si le socket est occupé pour une autre connexion |
48 | // On ferme la connexion existante |
||
49 | if (socket != null) |
||
50 | { |
||
51 | be694ed4 | Martin Toutant | _ = await CloseConnection(); |
52 | 1fd64302 | Martin Toutant | } |
53 | 0875d625 | Martin Toutant | |
54 | 1fd64302 | Martin Toutant | // Creation du canal RFCOMM (non sécurisé) |
55 | socket = droidBtDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString(sppUUID)); |
||
56 | 0875d625 | Martin Toutant | |
57 | 1fd64302 | Martin Toutant | CancellationTokenSource cts = new CancellationTokenSource(); |
58 | 0875d625 | Martin Toutant | |
59 | 1fd64302 | Martin Toutant | cts.CancelAfter(50000); |
60 | await socket.ConnectAsync().WithCancellation(cts.Token); |
||
61 | 0875d625 | Martin Toutant | |
62 | 1fd64302 | Martin Toutant | if (listeningThread != null) |
63 | { |
||
64 | listeningThread.Abort(); |
||
65 | } |
||
66 | d14bc0b1 | Martin Toutant | |
67 | 1fd64302 | Martin Toutant | listeningThread = new Thread(async delagate => await ListeningAsync()); |
68 | listeningThread.Start(); |
||
69 | } |
||
70 | be694ed4 | Martin Toutant | } else |
71 | { |
||
72 | // L'addresse MAC n'a pas été spécifiée |
||
73 | return false; |
||
74 | 0875d625 | Martin Toutant | } |
75 | } |
||
76 | 1fd64302 | Martin Toutant | catch (Exception) |
77 | 0875d625 | Martin Toutant | { |
78 | be694ed4 | Martin Toutant | return false; |
79 | 0875d625 | Martin Toutant | } |
80 | be694ed4 | Martin Toutant | return true; |
81 | 0875d625 | Martin Toutant | } |
82 | |||
83 | d14bc0b1 | Martin Toutant | public EventHandler<byte[]> DataReceivedEvent { get; set; } |
84 | |||
85 | 0875d625 | Martin Toutant | // 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 | d14bc0b1 | Martin Toutant | // Trigger DataReceivedEvent |
97 | Application.SynchronizationContext.Post(_ => { DataReceivedEvent.Invoke(this, filledBuffer); }, null); |
||
98 | 0875d625 | Martin Toutant | } |
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 | 3fa496d6 | Martin Toutant | public Task<bool> SendCommand(byte[] command) |
122 | 0875d625 | Martin Toutant | { |
123 | 06a15704 | Martin Toutant | |
124 | 0875d625 | Martin Toutant | if (socket.IsConnected == false) |
125 | { |
||
126 | return Task.FromResult(false); |
||
127 | } |
||
128 | else |
||
129 | { |
||
130 | // Envoi de la commande |
||
131 | 3fa496d6 | Martin Toutant | socket.OutputStream.Write(command, 0, command.Length); |
132 | 0875d625 | Martin Toutant | socket.OutputStream.Flush(); |
133 | } |
||
134 | return Task.FromResult(true); |
||
135 | } |
||
136 | |||
137 | 1fd64302 | Martin Toutant | public Task<bool> SendCommand(string command) |
138 | { |
||
139 | throw new NotImplementedException(); |
||
140 | } |
||
141 | 58c56df1 | Martin Toutant | } |
142 | } |