Flutter bluetooth low energy: custom characteristic and service

47 Views Asked by At

I am very new to Flutter so maybe this is a silly question but I have been stucked for long time so I would like to have some help here. I am trying to develop an app for android which could connect to a bluetooth low energy device. This app will connect with an esp32 microcontroller which I am also programming. I would like to stablish communication between a mobile app and the microcontroler.

I have tried to achieve this by using the bluetooth_low_energy: ^5.0.6 library. I have tested the exampled showed and it works perfect! However, I would like to select my device and do not have to select the charasteristic nor the service.

I would like to have a button which would send data as a command for the esp32 or request it, making some change to the gui. Here are a couple of examples:

  • Sending a 1 with a certain characteristic or service would set a gpio to 1
  • Sending a 1 with a certain characteristic or service would get back data of a sensor

From my point of view, I should attach some characteristic and service to every button I would like to include in my app. I have tried to define the services and the characteristic as:

ElevatedButton(
              onPressed: () async {
                GattService customService = GattService(
                  uuid: parseUUID('0000180a-0000-1000-8000-00805f9b34fb'),
                  characteristics: [], // Agrega las características según sea necesario.
                );

                /* Definicion caracteristica */

                GattCharacteristic customCharacteristic = GattCharacteristic(
                    uuid: parseUUID('ffeeddcc-bbaa-9988-7766-554433221100'),
                    properties: [GattCharacteristicProperty.write],
                    descriptors: []);

                this.characteristic.value =
                    customCharacteristic; // Aqui se asigna la caracteristica que se quiere emplear
                final writeType = this.writeType.value;
                final canWrite = customCharacteristic.properties.contains(
                  GattCharacteristicProperty.write,
                );
                final canWriteWithoutResponse =
                    customCharacteristic.properties.contains(
                  GattCharacteristicProperty.writeWithoutResponse,
                );
                if (writeType == GattCharacteristicWriteType.withResponse &&
                    !canWrite &&
                    canWriteWithoutResponse) {
                  this.writeType.value =
                      GattCharacteristicWriteType.withoutResponse;
                }
                if (writeType == GattCharacteristicWriteType.withoutResponse &&
                    !canWriteWithoutResponse &&
                    canWrite) {
                  this.writeType.value =
                      GattCharacteristicWriteType.withResponse;
                }

                final value = await CentralManager.instance
                    .readCharacteristic(customCharacteristic);
                const type = LogType.read;
                final log = Log(type, value);
                logs.value = [...logs.value, log];
              },
              child: const Text('TEST BUTTON'),
            )

However, I am not able to make it work and I do not know how to fix it. Could somebody please help me with this problem?

Thank you very much in advanced!

0

There are 0 best solutions below