Create a new function to send command to devices (BLE)

I wanna create a bluetooth app that can control my devices.
I use flutter_blue and modify its example.
(https://pub.dev/packages/flutter_blue)

I have successfully a litle by adding the following lines (from line 178)

             (c) => CharacteristicTile(
                characteristic: c,
                onReadPressed: () => c.read(),
                onWritePressed: () async {                      
                  List<int> value = await c.read();
                  if (value.toString() == "[1]"){
                    await c.write([0]);
                  }
                  else{
                    await c.write([1]);
                  }
                  await c.read();
                },

`And I can toggle the light in my device by pressing in this button

But, I wanna create a specific button to toggle the light. As you can see I created a RaisedButton.
Although I have created a button, I do not know how to create a _toggle function for it? Probably pass some parameters to that function. I have tried many ways but it seems that I have not gone in the right direction

_turnOn(List<BluetoothService> services) async { 
//List<BluetoothService> services = await device.discoverServices();
services.forEach((service) async {
// do something with service
  var characteristics = service.characteristics;
  for(BluetoothCharacteristic c in characteristics) {
      List<int> value = await c.read();
      print(value);
      await c.write([1]);
  }      
});                       

}

Hello, and welcome!
This part of the code for the onWritePressed callback looks exactly like a toggle to me: it reads the value, if it is ‘on’ then write it ‘off’, else (is ‘off’) then write it ‘on’.

onWritePressed: () async {                      
   List<int> value = await c.read();
    if (value.toString() == "[1]"){
        await c.write([0]);
    }
    else {
        await c.write([1]);
    }
    await c.read();
 }

I don’t fully understand the last c.read(), but there will be a reason for it.

Hth

The c.read() is to update the data on the screen, and the onWritePressed callback is work well !
But as you can see, I want to toogle the light by pressing the ON_OFF button

         RaisedButton(
          child: Text("ON_OFF"),
          onPressed: null,
        ),

But I can’t create a new function to do it.

I see, it seems you should pass the specific service you want to toggle on/off to the onPressed callback function of this RaisedButton, but at the moment I cannot tell you how to do (I don’t have enough experience with streams i suppose), sorry.

Thank you about idea,
I have succeeded when do that

  for(BluetoothCharacteristic c in characteristics) {
  if (service.uuid.toString() == "bae55b96-7d19-458d-970c-50613d801bc9"){
    if (c.uuid.toString() == "76e137ac-b15f-49d7-9c4c-e278e6492ad9") {
      List<int> value = await c.read();
      if (value.toString() == "[1]"){
        await c.write([0]);
      }
      else if (value.toString() == "[0]"){
        await c.write([1]);
      }          
    }
  }

But as I see it, there are many services and characteristics in the device.
I wondered if there was any way to tell which service and characteristic I needed to send commands to ?

I’ve been reading more about the flutter_blue API, and about bluetooth in general here: https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html. It’s just a guess, but maybe descriptors can be usefull to identify the right service?

Characteristic User Description Descriptor

As the name implies, this descriptor contains a user-readable description for the characteristic within which it is placed. This is a UTF-8 string that could read, for example, “Temperature in the living room.”

From the package’s API doc:

var descriptors = characteristic.descriptors;
for(BluetoothDescriptor d in descriptors) {
    List<int> value = await d.read();
    print(value);
}

hth.

Thank you, I will try it

I followed your idea. And this is the result:

D/FlutterBluePlugin(13804): [onDescriptorRead] uuid: 00002902-0000-1000-8000-00805f9b34fb status: 0
I/flutter (13804): [0, 0]
D/FlutterBluePlugin(13804): [onDescriptorRead] uuid: 00002901-0000-1000-800 0-0805f9b34fb status: 0
I/flutter (13804): [110, 117, 108, 108]

:confused: well, not too helpful indeed. I was hoping you could get the Characteristic User Description Descriptor out of the descriptors. It seems that’s not the case, sorry.

This may not be relevant to you anymore but maybe this can help someone in the future. I have spent that last day figuring this out.

Like you, I am using that same example, I’ve added a raised button and this is what I had to put in the onPressed function.

              var SERVICE = '0000aaaa-ead2-11e7-80c1-9a214cf093ae';
              var CHARACTERISTIC = '00005555-ead2-11e7-80c1-9a214cf093ae';

              List<BluetoothService> services = await device.discoverServices();
              services.forEach((service) {

                if (service.uuid.toString() == SERVICE) {
                  service.characteristics.forEach((characteristic) {
                    if (characteristic.uuid.toString() == CHARACTERISTIC) {
                      characteristic.write(utf8.encode(jsonEncode(_Credentials.toJson())));
                      print(service.deviceId);

can you please send me your project file