public async Task SetupNotifications(string bleDeviceId, Dictionary <string, string> telemetryMap) { BLEDevice = await BLEService.Connect(bleDeviceId); TelemetryMap = telemetryMap; foreach (var gatt in telemetryMap) { var telemetryField = gatt.Value; var pair = new GattPair(gatt.Key); var service = await BLEDevice.GetServiceAsync(pair.ServiceId); if (service != null) // service could be null if mapping has old values related to other devices { var characteristic = await service.GetCharacteristicAsync(pair.CharacteristicId); if (characteristic != null) // like above but for characteristic { if (telemetryField != null) { await BLEService.EnableNotification(characteristic); } else { await BLEService.DisableNotification(characteristic); } } } } MessagingCenter.Send(new ResultMessage <IDevice>(BLEDevice), Constants.BLE_DEVICE_READY); }
public async Task <bool> CheckServiceAndCharateristics() { if (this.BLEDevice == null || this.BLEDevice.State != Plugin.BLE.Abstractions.DeviceState.Connected) { return(false); } this.MTU = await BLEDevice.RequestMtuAsync(517); var service = await BLEDevice.GetServiceAsync(uuid_service); if (service == null) { return(false); } Charateristic = await service.GetCharacteristicAsync(uuid_characteristic); if (Charateristic == null) { return(false); } return(true); }
public async void LED1() { var service = await BLEDevice.GetServiceAsync(Guid.Parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")); var WriteCharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")); var ReadCharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")); //byte[] data = new byte[] { 0x31 }; if (WriteCharacteristic != null) { if (WriteCharacteristic.CanWrite) { byte[] data = new byte[] { 0x31 }; await WriteCharacteristic.WriteAsync(data); //byte[] readdata = new byte; if (ReadCharacteristic.CanRead) { byte[] readdata = await ReadCharacteristic.ReadAsync(); if (readdata == new byte[] { 0x31 }) { byte[] confirm = new byte[] { 0x33 }; await WriteCharacteristic.WriteAsync(confirm); await Task.Delay(1000); await WriteCharacteristic.WriteAsync(new byte[] { 0x36 }); } } } } }
public async void LED4() { var service = await BLEDevice.GetServiceAsync(Guid.Parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")); var characteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")); //var RXcharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")); //byte[] data = new byte[] { 0x31 }; if (characteristic != null) { if (characteristic.CanWrite) { byte[] data = new byte[] { 0x35 }; await characteristic.WriteAsync(data); } } }
public async void ReadTest() { var service = await BLEDevice.GetServiceAsync(Guid.Parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")); //var WriteCharacteristic = await service.Get(); //var ReadCharacteristic = await service.GetCharacteristicsAsync(); var WriteCharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")); var ReadCharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")); System.Diagnostics.Debug.WriteLine(ReadCharacteristic); Debug.WriteLine("Write Characteristic = " + WriteCharacteristic); System.Diagnostics.Debug.WriteLine("Read Characteristic CanRead = " + ReadCharacteristic.CanRead); await WriteCharacteristic.WriteAsync(new byte[] { 0x31 }); ReadCharacteristic.ValueUpdated += (s, e) => { Debug.WriteLine("New value : ", e.Characteristic.Value); }; await ReadCharacteristic.StartUpdatesAsync(); if (ReadCharacteristic.CanRead) { var readdata = await ReadCharacteristic.ReadAsync(); System.Diagnostics.Debug.WriteLine(readdata); if (readdata == new byte[] { 0x31 }) { byte[] confirm = new byte[] { 0x33 }; await WriteCharacteristic.WriteAsync(confirm); await Task.Delay(1000); await WriteCharacteristic.WriteAsync(new byte[] { 0x36 }); } } }
/* * public async void WriteToBLE() * { * var services = await BLEDevice.GetServicesAsync(); * Debug.WriteLine("Test"); * if (services == null) * return; * * var characteristics = await services[0].GetCharacteristicsAsync(); * var characteristic = characteristics[0]; * * if (characteristic != null) * { * if (characteristic.CanWrite) * { * byte[] buf = new byte[] { 0x31 }; * if (ledStatus) * { * //buf[1] = 0x31; * await characteristic.WriteAsync(buf); * } * else * { * //buf[1] = 0x31; * await characteristic.WriteAsync(buf); * } * ledStatus = !ledStatus; * } * } * * }*/ public async void LEDSON(int lednumber) { var service = await BLEDevice.GetServiceAsync(Guid.Parse("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")); var WriteCharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")); var ReadCharacteristic = await service.GetCharacteristicAsync(Guid.Parse("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")); //byte[] data = new byte[] { 0x31 }; byte[] ledbyte = BitConverter.GetBytes(lednumber); Array.Reverse(ledbyte); byte[] data = ledbyte; if (WriteCharacteristic != null) { if (WriteCharacteristic.CanWrite) { //byte[] data = new byte[] { 0x31 }; await WriteCharacteristic.WriteAsync(data); //byte[] readdata = new byte; if (ReadCharacteristic.CanRead) { byte[] readdata = await ReadCharacteristic.ReadAsync(); if (readdata == new byte[] { 0x31 }) { byte[] confirm = new byte[] { 0x33 }; await WriteCharacteristic.WriteAsync(confirm); await Task.Delay(1000); await WriteCharacteristic.WriteAsync(new byte[] { 0x36 }); } } } } }