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