teotestbluetooth / TestXamConnections / TestXamConnections.Android / Connection / BluetoothConnectionService.cs @ 1fd64302
Historique | Voir | Annoter | Télécharger (4,851 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 | 0875d625 | Martin Toutant | { |
35 | 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 | public EventHandler<string> ConnectionFailedEvent { get; set; } |
||
40 | public async Task Connect(Dictionary<string, string> param) |
||
41 | 0875d625 | Martin Toutant | { |
42 | try |
||
43 | { |
||
44 | // Si jamais on est en train de scanner |
||
45 | // On annule le scan |
||
46 | if (bluetoothAdapter.IsDiscovering) |
||
47 | { |
||
48 | bluetoothAdapter.CancelDiscovery(); |
||
49 | } |
||
50 | |||
51 | 1fd64302 | Martin Toutant | if (param.ContainsKey(ConnectionConstants.MAC_ADDR_KEY)) |
52 | 0875d625 | Martin Toutant | { |
53 | 1fd64302 | Martin Toutant | BluetoothDevice droidBtDevice = bluetoothAdapter.GetRemoteDevice(param[ConnectionConstants.MAC_ADDR_KEY]); |
54 | if (droidBtDevice != null) |
||
55 | 0875d625 | Martin Toutant | { |
56 | 1fd64302 | Martin Toutant | // Si le socket est occupé pour une autre connexion |
57 | // On ferme la connexion existante |
||
58 | if (socket != null) |
||
59 | { |
||
60 | await CloseConnection(); |
||
61 | } |
||
62 | 0875d625 | Martin Toutant | |
63 | 1fd64302 | Martin Toutant | // Creation du canal RFCOMM (non sécurisé) |
64 | socket = droidBtDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString(sppUUID)); |
||
65 | 0875d625 | Martin Toutant | |
66 | 1fd64302 | Martin Toutant | CancellationTokenSource cts = new CancellationTokenSource(); |
67 | 0875d625 | Martin Toutant | |
68 | 1fd64302 | Martin Toutant | cts.CancelAfter(50000); |
69 | await socket.ConnectAsync().WithCancellation(cts.Token); |
||
70 | 0875d625 | Martin Toutant | |
71 | 1fd64302 | Martin Toutant | if (listeningThread != null) |
72 | { |
||
73 | listeningThread.Abort(); |
||
74 | } |
||
75 | d14bc0b1 | Martin Toutant | |
76 | 1fd64302 | Martin Toutant | listeningThread = new Thread(async delagate => await ListeningAsync()); |
77 | listeningThread.Start(); |
||
78 | } |
||
79 | 0875d625 | Martin Toutant | } |
80 | } |
||
81 | 1fd64302 | Martin Toutant | catch (Exception) |
82 | 0875d625 | Martin Toutant | { |
83 | 1fd64302 | Martin Toutant | Application.SynchronizationContext.Post(_ => { ConnectionFailedEvent.Invoke(this, param[ConnectionConstants.MAC_ADDR_KEY]); }, null); |
84 | 0875d625 | Martin Toutant | } |
85 | } |
||
86 | |||
87 | d14bc0b1 | Martin Toutant | public EventHandler<byte[]> DataReceivedEvent { get; set; } |
88 | |||
89 | 0875d625 | Martin Toutant | // Fonction d'écoute pour le Thread d'écoute |
90 | private async Task ListeningAsync() |
||
91 | { |
||
92 | try |
||
93 | { |
||
94 | byte[] buffer = new byte[1024]; |
||
95 | while (socket.IsConnected) |
||
96 | { |
||
97 | int byteAvailable = await socket.InputStream.ReadAsync(buffer, 0, buffer.Length); |
||
98 | // Resize the byte array |
||
99 | byte[] filledBuffer = buffer.Take(byteAvailable).ToArray(); |
||
100 | d14bc0b1 | Martin Toutant | // Trigger DataReceivedEvent |
101 | Application.SynchronizationContext.Post(_ => { DataReceivedEvent.Invoke(this, filledBuffer); }, null); |
||
102 | 0875d625 | Martin Toutant | } |
103 | } |
||
104 | catch (IOException ex) |
||
105 | { |
||
106 | System.Diagnostics.Debug.WriteLine(ex); |
||
107 | } |
||
108 | catch (ThreadAbortException taEx) |
||
109 | { |
||
110 | System.Diagnostics.Debug.WriteLine(taEx); |
||
111 | } |
||
112 | catch (Exception ex) |
||
113 | { |
||
114 | System.Diagnostics.Debug.WriteLine(ex); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | |||
119 | private Task<bool> CloseConnection() |
||
120 | { |
||
121 | while (socket.IsConnected) socket.Close(); |
||
122 | return Task.FromResult(true); |
||
123 | } |
||
124 | |||
125 | public Task<bool> SendCommand(byte[] hexValues) |
||
126 | { |
||
127 | if (socket.IsConnected == false) |
||
128 | { |
||
129 | return Task.FromResult(false); |
||
130 | } |
||
131 | else |
||
132 | { |
||
133 | // Envoi de la commande |
||
134 | socket.OutputStream.Write(hexValues, 0, hexValues.Length); |
||
135 | socket.OutputStream.Flush(); |
||
136 | } |
||
137 | return Task.FromResult(true); |
||
138 | } |
||
139 | |||
140 | 1fd64302 | Martin Toutant | public Task<bool> SendCommand(string command) |
141 | { |
||
142 | throw new NotImplementedException(); |
||
143 | } |
||
144 | 58c56df1 | Martin Toutant | } |
145 | } |