private static async Task device_DisconnectedAsync(Device device, BlueZEventArgs e) { try { Console.WriteLine($"Disconnected from {await device.GetAddressAsync()}"); await Task.Delay(TimeSpan.FromSeconds(15)); Console.WriteLine($"Attempting to reconnect to {await device.GetAddressAsync()}..."); await device.ConnectAsync(); } catch (Exception ex) { Console.Error.WriteLine(ex); } }
private static async Task device_ConnectedAsync(Device device, BlueZEventArgs e) { try { if (e.IsStateChange) { Console.WriteLine($"Connected to {await device.GetAddressAsync()}"); } else { Console.WriteLine($"Already connected to {await device.GetAddressAsync()}"); } } catch (Exception ex) { Console.Error.WriteLine(ex); } }
private static async Task adapter_PoweredOnAsync(Adapter adapter, BlueZEventArgs e) { try { if (e.IsStateChange) { Console.WriteLine("Bluetooth adapter powered on."); } else { Console.WriteLine("Bluetooth adapter already powered on."); } Console.WriteLine("Starting scan..."); await adapter.StartDiscoveryAsync(); } catch (Exception ex) { Console.Error.WriteLine(ex); } }
private static async Task device_ServicesResolvedAsync(Device device, BlueZEventArgs e) { try { if (e.IsStateChange) { Console.WriteLine($"Services resolved for {await device.GetAddressAsync()}"); } else { Console.WriteLine($"Services already resolved for {await device.GetAddressAsync()}"); } var servicesUUIDs = await device.GetUUIDsAsync(); Console.WriteLine($"Device offers {servicesUUIDs.Length} service(s)."); // foreach (var uuid in servicesUUIDs) // { // Console.WriteLine(uuid); // } var service = await device.GetServiceAsync(s_serviceUUID); if (service == null) { Console.WriteLine($"Service UUID {s_serviceUUID} not found. Do you need to pair first?"); return; } var characteristic = await service.GetCharacteristicAsync(s_characteristicUUID); if (characteristic == null) { Console.WriteLine($"Characteristic UUID {s_characteristicUUID} not found within service {s_serviceUUID}."); return; } Console.WriteLine(); // Subscribe to the characteristic's value. Be notified of updates. characteristic.Value += characteristic_Value; // Attempt to read the current value. Some characteristics only support Notify. byte[] value; try { Console.WriteLine("Reading current characteristic value..."); value = await characteristic.GetValueAsync(); } catch (Exception ex) { Console.Error.WriteLine($"Error reading characteristic value: {ex.Message}"); return; } PrintCharacteristicValue(s_characteristicUUID, value); } catch (Exception ex) { Console.Error.WriteLine(ex); } }
private Task Device_Connected(Device sender, BlueZEventArgs eventArgs) { throw new NotImplementedException(); }
private Task Device_ServicesResolved(Device sender, BlueZEventArgs eventArgs) { throw new NotImplementedException(); }
private async Task OnDisconnected(Device sender, BlueZEventArgs eventArgs) { Console.WriteLine($"{await sender.GetNameAsync()} has disconnected. Will attempt reconnect on next attempt."); powerStateCharacteristic = null; }