This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Android, setOnCheckedChangeListener, changing characteristic after button press

Hello.

I would like to ask about writing data from Smartphone to Thingy. How to change characteristic using setOnCheckedChangeListener.

Is it correct way to change characteristics values?

ToggleButton tog_red2 = (ToggleButton) findViewById(R.id.toggleButton8);
        tog_red2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b)
                {
                    color=6;
                }
                if(b==false)
                {

                    color=7;
                }
                bluetoothGatt=devicesDiscovered.get(0).connectGatt(MainActivity.this,false, gattCallback);         //connecting with Thingy      


            }
        });

 public BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            //Log.d("Connect", newState+"");

          }


        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);

            UUID SERVICE_UUID=UUID.fromString           ("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
            UUID CHARACTERISTIC_UUID_TX=UUID.fromString    ("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");

            BluetoothGattService service = gatt.getService(SERVICE_UUID);
            BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID_TX);


            if(color==6)
            {
                characteristic.setValue(data_breath_red);
                gatt.writeCharacteristic(characteristic); //albo to albo to niżej
            }
            if (color==7)
            {
                characteristic.setValue(data_breath_blue);
                gatt.writeCharacteristic(characteristic); //albo to albo to niżej
            }
            
            ...

in this case every time when I want to send data  need to connect again.

I think that it is not good idea but don't have idea how to do that in another way.

  • Everything works good now:

    ToggleButton tog_red = (ToggleButton)findViewById(R.id.toggleButton6);
          tog_red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
    
                  BluetoothGattService mCustomService = bluetoothGatt.getService(UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"));
                  BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"));
                  mReadCharacteristic.setValue(data_breath_blue);
                  bluetoothGatt.writeCharacteristic(mReadCharacteristic);
    
              }
              }
    
          });

    Problem solved

  • Great! Thank you for sharing your solution here on the forum.

    Best regards,

    Simon

Related