BTConnector.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Android.Bluetooth;
  2. using Android.Content;
  3. using System;
  4. namespace MyLibrary.Bluetooth
  5. {
  6. public class BLEConnector
  7. {
  8. private BluetoothGatt _bluetoothGatt;
  9. private BluetoothGattCallback _gattCallback;
  10. public BLEConnector()
  11. {
  12. _gattCallback = new CustomGattCallback();
  13. }
  14. /// <summary>
  15. /// Connect to the specified BluetoothDevice.
  16. /// </summary>
  17. /// <param name="device">The BluetoothDevice to connect to.</param>
  18. /// <param name="context">The Android context required for the connection.</param>
  19. public void Connect(BluetoothDevice device, Context context)
  20. {
  21. if (device == null)
  22. {
  23. throw new ArgumentNullException(nameof(device), "Device cannot be null");
  24. }
  25. // Initiate the connection
  26. _bluetoothGatt = device.ConnectGatt(context, false, _gattCallback);
  27. }
  28. /// <summary>
  29. /// Disconnect the current connection.
  30. /// </summary>
  31. public void Disconnect()
  32. {
  33. _bluetoothGatt?.Disconnect();
  34. _bluetoothGatt?.Close();
  35. _bluetoothGatt = null;
  36. }
  37. /// <summary>
  38. /// Custom GATT callback to handle Bluetooth events.
  39. /// </summary>
  40. private class CustomGattCallback : BluetoothGattCallback
  41. {
  42. public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
  43. {
  44. base.OnConnectionStateChange(gatt, status, newState);
  45. if (status == GattStatus.Success && newState == ProfileState.Connected)
  46. {
  47. Console.WriteLine("Connected to device!");
  48. gatt.DiscoverServices();
  49. }
  50. else if (newState == ProfileState.Disconnected)
  51. {
  52. Console.WriteLine("Disconnected from device.");
  53. }
  54. else
  55. {
  56. Console.WriteLine($"Connection state changed: {newState}, Status: {status}");
  57. }
  58. }
  59. public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
  60. {
  61. base.OnServicesDiscovered(gatt, status);
  62. if (status == GattStatus.Success)
  63. {
  64. Console.WriteLine("Services discovered:");
  65. foreach (var service in gatt.Services)
  66. {
  67. Console.WriteLine($"Service: {service.Uuid}");
  68. }
  69. }
  70. else
  71. {
  72. Console.WriteLine($"Failed to discover services. Status: {status}");
  73. }
  74. }
  75. public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
  76. {
  77. base.OnCharacteristicRead(gatt, characteristic, status);
  78. if (status == GattStatus.Success)
  79. {
  80. Console.WriteLine($"Characteristic Read: {characteristic.Uuid}, Value: {BitConverter.ToString(characteristic.GetValue())}");
  81. }
  82. }
  83. public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
  84. {
  85. base.OnCharacteristicWrite(gatt, characteristic, status);
  86. if (status == GattStatus.Success)
  87. {
  88. Console.WriteLine($"Characteristic Write Successful: {characteristic.Uuid}");
  89. }
  90. }
  91. public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
  92. {
  93. base.OnCharacteristicChanged(gatt, characteristic);
  94. Console.WriteLine($"Characteristic Changed: {characteristic.Uuid}, Value: {BitConverter.ToString(characteristic.GetValue())}");
  95. }
  96. }
  97. }
  98. }