Révision 4fda4a87

Voir les différences:

TestBLE/TestBLE.Android/Services/BluetoothService.cs
125 125
            }
126 126
        }
127 127

  
128
        
129

  
130 128
        public EventHandler<string> DataReceivedEvent { get; set; }
131 129
        private string _bufferedData;
132 130

  
......
170 168
            }
171 169
        }
172 170

  
173
        private void OnDataReceivedP(byte[] noEmptybuffer)
174
        {
175
            bufferedData += Encoding.UTF8.GetString(noEmptybuffer, 0, noEmptybuffer.Count());
176

  
177
            if (bufferedData.Contains("\r\n"))
178
            {
179
                if (bufferedData.EndsWith("\r\n"))
180
                {
181
                    bufferedData = bufferedData.Replace("\r\n", "|");
182
                    string[] datas = bufferedData.Split('|');
183
                    foreach (var str in datas)
184
                    {
185
                        str.Replace('\x00'.ToString(), "");
186
                        if (!string.IsNullOrWhiteSpace(str) && (str.Length > 0))
187
                        {
188
                            DataReceivedEvent?.Invoke(this, str);
189
                            //FormsMessenger.Send (new TagMessage (str), this);
190
                        }
191
                    }
192

  
193
                    bufferedData = "";
194
                }
195
                else
196
                {
197
                    bufferedData = bufferedData.Replace("\r\n", "|");
198
                    string[] datas = bufferedData.Split('|');
199
                    for (int i = 0; i < datas.Count() - 1; i++)
200
                    {
201
                        if (!string.IsNullOrEmpty(datas[i]))
202
                        {
203
                            DataReceivedEvent?.Invoke(this, datas[i]);
204
                            //FormsMessenger.Send (new TagMessage (datas [i]), this);
205
                        }
206
                    }
207

  
208
                    bufferedData = datas[datas.Count() - 1];
209
                }
210
            }
211
        }
212

  
213 171
        private void ScanFinished(object sender, string e)
214 172
        {
215 173
            scanCompletionSource?.SetResult(true);
......
260 218
            return ret;
261 219
        }
262 220

  
263
        public Task<bool> SendHexValues(string[] hexValues)
221
        public Task<bool> SendHexValues(byte[] hexValues)
264 222
        {
265
            byte[] data = new byte[hexValues.Length];
266

  
267 223
            if (socket.IsConnected == false)
268 224
            {
269 225
                return Task.FromResult(false);
270 226
            } else
271 227
            {
272
                data = hexValuesToByteArray(hexValues);
273 228
                // Envoi de la commande
274
                socket.OutputStream.Write(data, 0, hexValues.Length);
229
                socket.OutputStream.Write(hexValues, 0, hexValues.Length);
275 230
                socket.OutputStream.Flush();
276 231
            }
277 232

  
TestBLE/TestBLE.Android/obj/Debug/110/_TelemetryProps
22 22
AndroidDexTool=d8
23 23
AndroidLinkTool=
24 24
AndroidEnableProfiledAot=
25
AndroidInstallAfterBuild=False
26
AndroidSessionId=
25
AndroidInstallAfterBuild=True
26
AndroidSessionId=bd18f6e1-ac31-4cd1-a87a-7f35fc9fe283
27 27
BuildConfiguration=Debug
28 28
TemplateName={c9e5eea5-ca05-42a1-839b-61506e0a37df}
TestBLE/TestBLE/Models/TeoBluetoothCommands.cs
6 6
{
7 7
    public static class TeoBluetoothCommand
8 8
    {
9
        public enum AvailableCommands
9
        public enum CommandType
10 10
        {
11 11
            // Voir documentation TEO INRAE 
12 12
            // Codes ASCII
......
25 25
            EmissionPoids
26 26
        }
27 27

  
28
        public static byte[] GetCommand(AvailableCommands c)
28
        // Retourne la chaine d'octets d'une commande Teo (prog INRAE)
29
        public static byte[] GetCommandCode(CommandType c)
29 30
        {
30 31
            // Les commandes envoyées sont sur 3 octets
31 32
            byte[] ret = new byte[3];
......
37 38
            // Commande
38 39
            switch (c)
39 40
            {
40
                case AvailableCommands.ModeOrdre:
41
                case CommandType.ModeOrdre:
41 42
                    ret[1] = 0x61; 
42 43
                    break;
43 44

  
44
                case AvailableCommands.SortieOrdreGardeBluetooth:
45
                case CommandType.SortieOrdreGardeBluetooth:
45 46
                    ret[1] = 0x6B; 
46 47
                    break;
47 48

  
48
                case AvailableCommands.SortieOrdreEtBluetooth:
49
                case CommandType.SortieOrdreEtBluetooth:
49 50
                    ret[1] = 0x67; 
50 51
                    break;
51 52

  
52
                case AvailableCommands.ModePeseeSimple:
53
                case CommandType.ModePeseeSimple:
53 54
                    ret[1] = 0x62; 
54 55
                    break;
55 56

  
56
                case AvailableCommands.SortiePeseeSimple:
57
                case CommandType.SortiePeseeSimple:
57 58
                    ret[1] = 0x65; 
58 59
                    break;
59 60

  
60
                case AvailableCommands.TareP1:
61
                case CommandType.TareP1:
61 62
                    ret[1] = 0x74; 
62 63
                    break;
63 64

  
64
                case AvailableCommands.TareP2:
65
                case CommandType.TareP2:
65 66
                    ret[1] = 0x6A; 
66 67
                    break;
67 68

  
68
                case AvailableCommands.ZeroP1:
69
                case CommandType.ZeroP1:
69 70
                    ret[1] = 0x7A; 
70 71
                    break;
71 72

  
72
                case AvailableCommands.ZeroP2:
73
                case CommandType.ZeroP2:
73 74
                    ret[1] = 0x64; 
74 75
                    break;
75 76

  
76
                case AvailableCommands.InfosMetrologie:
77
                case CommandType.InfosMetrologie:
77 78
                    ret[1] = 0x73; 
78 79
                    break;
79 80

  
80
                case AvailableCommands.EmissionPoids:
81
                case CommandType.EmissionPoids:
81 82
                    ret[1] = 0x63; 
82 83
                    break;
83 84
                // TODO
84
                case AvailableCommands.PeseeStabilisee:
85
                case CommandType.PeseeStabilisee:
85 86
                    break;
86 87

  
87
                case AvailableCommands.InterrompPS:
88
                case CommandType.InterrompPS:
89
                    break;
90

  
91
                default:
92
                    break;
93
            }
94
            return ret;
95
        }
96

  
97
        // Retourne le nom d'une commande Teo (prog INRAE)
98
        public static string GetCommandName(CommandType c)
99
        {
100
            // Les commandes envoyées sont sur 3 octets
101
            string ret = "";
102

  
103
            // Commande
104
            switch (c)
105
            {
106
                case CommandType.ModeOrdre:
107
                    ret = "Mode attente ordre";
108
                    break;
109

  
110
                case CommandType.SortieOrdreGardeBluetooth:
111
                    ret = "Sortie mode ordre";
112
                    break;
113

  
114
                case CommandType.SortieOrdreEtBluetooth:
115
                    ret = "Sortie mode ordre & couper bluetooth";
116
                    break;
117

  
118
                case CommandType.ModePeseeSimple:
119
                    ret = "Mode simple pesage";
120
                    break;
121

  
122
                case CommandType.SortiePeseeSimple:
123
                    ret = "Sortie mode simple pesage";
124
                    break;
125

  
126
                case CommandType.TareP1:
127
                    ret = "Tare plateau 1";
128
                    break;
129

  
130
                case CommandType.TareP2:
131
                    ret = "Tare plateau 2";
132
                    break;
133

  
134
                case CommandType.ZeroP1:
135
                    ret = "Zero plateau 1";
136
                    break;
137

  
138
                case CommandType.ZeroP2:
139
                    ret = "Zero plateau 2";
140
                    break;
141

  
142
                case CommandType.InfosMetrologie:
143
                    ret = "Informations métrologie";
144
                    break;
145

  
146
                case CommandType.EmissionPoids:
147
                    ret = "Emission poids";
148
                    break;
149
                // TODO
150
                case CommandType.PeseeStabilisee:
151
                    ret = "Pesée stabilisée - NON implémenté";
152
                    break;
153

  
154
                case CommandType.InterrompPS:
155
                    ret = "STOP Pesée stabilisée - NON implémenté";
88 156
                    break;
89 157

  
90 158
                default:
TestBLE/TestBLE/Services/IBluetoothService.cs
13 13
        Task<bool> Pair(BTDevice bluetoothDevice);
14 14
        Task<bool> Connect(BTDevice bluetoothDevice);
15 15
        Task<bool> Disconnect(BTDevice bluetoothDevice);
16
        Task<bool> SendHexValues(string[] hexValues);
16
        Task<bool> SendHexValues(byte[] hexValues);
17 17
        ICollection<BTDevice> GetBondedDevices();
18 18
        EventHandler<string> DataReceivedEvent { get; set; }
19 19
        ObservableCollection<BTDevice> DeviceList { get; set; }
TestBLE/TestBLE/ViewModels/CommandItemViewModel.cs
1 1
using System.Threading.Tasks;
2 2
using System.Windows.Input;
3
using TestBLE.Models;
3 4
using TestBLE.Services;
4 5
using Xamarin.Forms;
5 6

  
......
10 11
        public IBluetoothService BluetoothService { get; set; }
11 12

  
12 13
        private string name;
13
        private string[] values;
14
        private TeoBluetoothCommand.CommandType toSendCommand;
14 15

  
15
        public string[] Values
16
        public TeoBluetoothCommand.CommandType ToSendCommand
16 17
        {
17
            get { return values; }
18
            set { SetProperty( ref values, value); }
18
            get { return toSendCommand; }
19
            set { SetProperty( ref toSendCommand, value); }
19 20
        }
20 21

  
21 22
        public string Name
......
26 27

  
27 28
        private async Task<bool> SendValue()
28 29
        {
29
            await BluetoothService.SendHexValues(values);
30
            await BluetoothService.SendHexValues(TeoBluetoothCommand.GetCommandCode(ToSendCommand));
30 31
            return true;
31 32
        }
32 33

  
33 34
        public ICommand SendValueCommand { private set; get; }
34 35

  
35
        public CommandItemViewModel()
36
        public CommandItemViewModel(TeoBluetoothCommand.CommandType cmd)
36 37
        {
38
            Name = TeoBluetoothCommand.GetCommandName(cmd);
37 39
            BluetoothService = DependencyService.Get<IBluetoothService>();
38 40
            SendValueCommand = new Command(async () => await SendValue());
39 41
        }
TestBLE/TestBLE/ViewModels/DeviceViewModel.cs
7 7
using TestBLE.Models;
8 8
using TestBLE.Services;
9 9
using Xamarin.Forms;
10
using static TestBLE.Models.TeoBluetoothCommand;
10 11

  
11 12
namespace TestBLE.ViewModels
12 13
{
......
24 25
            set { SetProperty(ref isVisibleData, value); }
25 26
        }
26 27

  
27

  
28 28
        public bool IsConnected
29 29
        {
30 30
            get { return isConnected; }
......
37 37
            set { SetProperty(ref device, value); }
38 38
        }
39 39

  
40

  
41 40
        public string Data
42 41
        {
43 42
            get { return data; }
......
67 66

  
68 67
            // Ajout manuel des commandes
69 68
            AvailableCommandsList = new ObservableCollection<CommandItemViewModel>();
70
            AvailableCommandsList.Add(new CommandItemViewModel()
69
            foreach (CommandType _cmd in Enum.GetValues(typeof(CommandType))
71 70
            {
72
                Name = "Mode ordre",
73
                Values = new string[] { "02", "61", "0D" }
74
            });
75
            AvailableCommandsList.Add(new CommandItemViewModel()
76
            {
77
                Name = "Lancer pesée",
78
                Values = new string[] { "02", "63", "0D" }
79
            });
71

  
72
            }
80 73
        }
81 74

  
82 75
        ~DeviceViewModel()
TestBLE/TestBLE/Views/DeviceView.xaml
61 61
                            Color="#4f5a64" />
62 62
                        <StackLayout
63 63
                            x:Name="commandsList"
64
                            Padding="0,15"
64
                            Margin="0,10"
65
                            Padding="0"
65 66
                            BindableLayout.ItemsSource="{Binding AvailableCommandsList}"
66 67
                            HorizontalOptions="FillAndExpand"
67 68
                            IsVisible="{Binding IsConnected}"
68 69
                            Orientation="Vertical"
69
                            Spacing="10">
70
                            Spacing="0">
70 71
                            <BindableLayout.ItemTemplate>
71 72
                                <DataTemplate>
72 73
                                    <StackLayout
73
                                        Margin="0"
74
                                        Padding="0,5"
74 75
                                        xe:Commands.Tap="{Binding SendValueCommand}"
75 76
                                        xe:TouchEffect.Color="#34a9ff"
76 77
                                        Spacing="0">
......
79 80
                                            HorizontalTextAlignment="Center"
80 81
                                            Text="{Binding Name}"
81 82
                                            TextColor="White" />
82
                                        <Label
83
                                            FontAttributes="Italic"
84
                                            FontSize="12"
85
                                            HorizontalTextAlignment="Center"
86
                                            Text="{Binding Values}"
87
                                            TextColor="DarkGray" />
88 83
                                    </StackLayout>
89 84
                                </DataTemplate>
90 85
                            </BindableLayout.ItemTemplate>

Formats disponibles : Unified diff