Révision 4fda4a87 TestBLE/TestBLE.Android/Services/BluetoothService.cs

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

  

Formats disponibles : Unified diff