Révision 1fd64302 TestXamConnections/TestXamConnections.Android/Connection/BluetoothConnectionService.cs
TestXamConnections/TestXamConnections.Android/Connection/BluetoothConnectionService.cs | ||
---|---|---|
16 | 16 |
using TestXamConnections.Droid.Utils; |
17 | 17 |
using TestXamConnections.Models; |
18 | 18 |
using TestXamConnections.Connection; |
19 |
using TestXamConnections.Helper; |
|
19 | 20 |
|
20 | 21 |
namespace TestXamConnections.Droid.Connection |
21 | 22 |
{ |
... | ... | |
34 | 35 |
bluetoothAdapter = BluetoothAdapter.DefaultAdapter; |
35 | 36 |
} |
36 | 37 |
|
37 |
public async Task<bool> Connect(Device btDevice) |
|
38 |
// 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) |
|
38 | 41 |
{ |
39 | 42 |
try |
40 | 43 |
{ |
... | ... | |
45 | 48 |
bluetoothAdapter.CancelDiscovery(); |
46 | 49 |
} |
47 | 50 |
|
48 |
BluetoothDevice droidBtDevice = bluetoothAdapter.GetRemoteDevice(btDevice.MACAddress); |
|
49 |
if (droidBtDevice != null) |
|
51 |
if (param.ContainsKey(ConnectionConstants.MAC_ADDR_KEY)) |
|
50 | 52 |
{ |
51 |
// Si le socket est occupé pour une autre connexion |
|
52 |
// On ferme la connexion existante |
|
53 |
if (socket != null) |
|
53 |
BluetoothDevice droidBtDevice = bluetoothAdapter.GetRemoteDevice(param[ConnectionConstants.MAC_ADDR_KEY]); |
|
54 |
if (droidBtDevice != null) |
|
54 | 55 |
{ |
55 |
await CloseConnection(); |
|
56 |
} |
|
56 |
// Si le socket est occupé pour une autre connexion |
|
57 |
// On ferme la connexion existante |
|
58 |
if (socket != null) |
|
59 |
{ |
|
60 |
await CloseConnection(); |
|
61 |
} |
|
57 | 62 |
|
58 |
// Creation du canal RFCOMM (non sécurisé) |
|
59 |
socket = droidBtDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString(sppUUID)); |
|
63 |
// Creation du canal RFCOMM (non sécurisé)
|
|
64 |
socket = droidBtDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString(sppUUID));
|
|
60 | 65 |
|
61 |
CancellationTokenSource cts = new CancellationTokenSource(); |
|
66 |
CancellationTokenSource cts = new CancellationTokenSource();
|
|
62 | 67 |
|
63 |
cts.CancelAfter(50000); |
|
64 |
await socket.ConnectAsync().WithCancellation(cts.Token); |
|
68 |
cts.CancelAfter(50000);
|
|
69 |
await socket.ConnectAsync().WithCancellation(cts.Token);
|
|
65 | 70 |
|
66 |
if (listeningThread != null) |
|
67 |
{ |
|
68 |
listeningThread.Abort(); |
|
69 |
} |
|
71 |
if (listeningThread != null)
|
|
72 |
{
|
|
73 |
listeningThread.Abort();
|
|
74 |
}
|
|
70 | 75 |
|
71 |
listeningThread = new Thread(async delagate => await ListeningAsync()); |
|
72 |
listeningThread.Start(); |
|
73 |
|
|
74 |
return true; |
|
76 |
listeningThread = new Thread(async delagate => await ListeningAsync()); |
|
77 |
listeningThread.Start(); |
|
78 |
} |
|
75 | 79 |
} |
76 | 80 |
} |
77 |
catch (Exception ex)
|
|
81 |
catch (Exception) |
|
78 | 82 |
{ |
79 |
System.Diagnostics.Debug.WriteLine(ex);
|
|
83 |
Application.SynchronizationContext.Post(_ => { ConnectionFailedEvent.Invoke(this, param[ConnectionConstants.MAC_ADDR_KEY]); }, null);
|
|
80 | 84 |
} |
81 |
|
|
82 |
return false; |
|
83 | 85 |
} |
84 | 86 |
|
85 | 87 |
public EventHandler<byte[]> DataReceivedEvent { get; set; } |
... | ... | |
135 | 137 |
return Task.FromResult(true); |
136 | 138 |
} |
137 | 139 |
|
140 |
public Task<bool> SendCommand(string command) |
|
141 |
{ |
|
142 |
throw new NotImplementedException(); |
|
143 |
} |
|
138 | 144 |
} |
139 | 145 |
} |
Formats disponibles : Unified diff