cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC™ 4

AnMo_4196841
New Contributor II

Hi friends,

I have problem with the example of Android Studio "Bluetooth LE Gatt". Any of you know what changes I have to do to the example working good?

The error is that need location permission but I dont know where I have to do de changes. I have put the next in the AndroidManifest.xml:

"<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>"

What more changes I have to do?

Thanks.

Antonio

0 Likes
35 Replies
DheerajK_81
Moderator
Moderator

Hello Antonio,

I'm sorry, I don't know which example you are referring to. Nevertheless, to add permission to your Android Application, you need to edit the AndroidManifest.xml file and add the permissions before the "<application>" label. Adding a snippet of this below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.cypress.cysmart"
   android:versionCode="10"
   android:versionName="${version_name}">

   <!--
  Declare this required feature if you want to make the app available to BLE-capable
  devices only. If you want to make your app available to devices that don't support BLE,
  you should omit this in the manifest. Instead, determine BLE capability by using
  PackageManager.hasSystemFeature(FEATURE_BLUETOOTH_LE)
  -->
   <uses-feature
   android:name="android.hardware.bluetooth_le"
   android:required="true" />

  <uses-permission android:name="android.permission.BLUETOOTH" />

  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  <uses-permission android:name="android.permission.INTERNET" />

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  <application
   android:name="com.cypress.cysmart.CySmartApplication"
   android:allowBackup="true"
   android:enabled="true"
   android:icon="@drawable/appicon"

Can you please attach your project? I will get an insight on what issues you are facing.

Also, I would recommend using the CySmart Android Source Code as the base for your application and developing on top of that.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi, the example is in the examples projects of android studio. When you open android studio, you click on android studio examples projects and search BluetoothLEGatt. When it charge, if you run it, you will had receive an error, you need location permission. To solve it, you have to add the next in android manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

And also you have to add the next in DeviceScanActivity:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {

  switch (requestCode) {

  case PERMISSION_REQUEST_COARSE_LOCATION: {

  if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

  // Permission granted, yay! Start the Bluetooth device scan.
  //scanLeDevice(true);
  } else {

  // Alert the user that this application requires the location permission to perform the scan.
  onPause();
  }

  }

  }

}

One time solve this problem, I want to receive the data of three different characteristic. If I change the UUID of heart rate (that is the example) and I put an UUID to receive the humidity data, I dont receive nothing, I only receive 0.

¿Why I receive 0 and dont receive the data of humidity?,¿What changes I have to do to add the other two characteristic? I attached the project like I have it now.

Thanks

0 Likes
DheerajK_81
Moderator
Moderator

Can you also attach the peripheral side of the project?

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Yes, of course. Here you have and also I attached the "Electri Circuit" where I have the DHT22 Sensor and Proximity Sensor.

This is a project made with PSoC Creator, I use the Pioneer Kit 4200 BLE.

0 Likes
AnMo_4196841
New Contributor II

DheerajK_81​ tell me something when you try to solve it. Thanks you very much.

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

I have solved the problem. Now in the app I receive all the data. To receive them, double click on the characteristic for which you want to obtain the data, the first time deactivates the notification of a characteristic, if it is the case that there is any active, the second time activates the notification of the characteristic. characteristic that you want to receive the data and shows it to you, but this does not do it exactly like this, since it only does it if you first get the proximity, then the temperature and then the humidity, once you get the humidity, either in the first place or after performing the above process, then it no longer allows you to get any of the other data. How could I make this always work for me? I have thought about incorporating a button, either for everyone or one for each feature, which has the function of deactivating notifications, to see what solution you could give me.

In this project I show the data in a text view but when the value of one characteristic changes or you receive the value of another characteristic, the previous one you lose. How could I store all the values ​​I receive in a list?

Thank you and regards, Antonio.

0 Likes
DheerajK_81
Moderator
Moderator

Yes, you can have buttons for enabling notifications for each characteristics like we do for CySmart app. But in your case, you have a single data field which you update based on the characteristic chosen. And in the Android code, you are disabling the notification of any active characteristic and then enabling it for the chosen one. This ensures that no other characteristic is updating the data field.

The only problem I see is, it doesn't update the data field fast enough. Sometimes it does update, sometimes it just displays the first characteristic I clicked and nothing changes after that.

I'm not understanding why this is so. You have an OnClickListener on the child items in the list and it is working properly when I tested. Looks like the Android side isn't the issue.

I checked the PSoC Creator project and noticed that you set the variables startNotificationProximity, humidity and temperature whenever the characteristic is clicked. But I don't see it being disabled anywhere.

If these variables which are used as flags aren't reset, the condition will always execute in the for loop for all of them.

If you want to display the values of all the characteristics, why don't you have a TextView for all the three instead of just one. That way, whenever the characteristics are enabled, the values will reflect in the TextView.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Regarding showing the values ​​in three different Text Views, it is not appropriate since in the PSoC Creator project I impose that the data will only be sent through BLE if the notification of a feature is active. Therefore, all three data could not be displayed at the same time. Also, my intention is to store in a ListView all the values ​​that are received so you would have all the data obtained throughout the time the app is running. That is, if humidity notification is enabled, then all the data that has been captured for a time is stored in a listview.

Regarding the notification button to enable or disable it, what would be the code needed to add that? It would be necessary to incorporate the button in the layout, define a button variable in the activity, link it with the button's id, and finally make an onClick and here you would no longer know what code to put. If you could help me out with that, I'm practically new to this world of BLE and Android programming and I'm still a bit lost.

Thank you. Greetings, Antonio.

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

Do you have new news about that I told you?

Regards

0 Likes
DheerajK_81
Moderator
Moderator

Hello,

Please find the working projects attached of both PSoC Creator peripheral side and Android Studio central app side.

Your PSoC Creator project had a lot of delays which led to the gatt update not working as expected and also disconnection. I have fixed that using timers and changing the connection intervals, advertising intervals etc.

When it comes to Android Side, there was some problem with the notification being written to the descriptor, I have fixed in the attached project. Also, regarding notification, right now the project merely enables the notification of the characteristic that is active and disables others.

Implementing the button for each characteristic to enable and disable the notification will require you to have a custom layout and also using a custom array adapter. Please refer here for more information on how to do so.

You can create a list view for containing the data values but again, you will need to modify the layout and add an adapter seperately for this.

(1) When temperature characteristic is clicked:

ble1.jpg

(2) When humidity is clicked:

ble2.jpg

There's a lot of UI improvements also you can do to display the name of the characteristics etc. Hope it works for you, let me know your observations

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81

First of all, thanks for everything.

Secondly, in the project of PSoC Creator I obtain an error when I try execute this. The problem is this:

" Unable to find component "cy_boot_v5_90" "

I was able to know that this problem was resolved when I update my components, what I did and the problem was solve, but when I executed the project and tried to obtain the different info of temperatura, humidity and proximity with the application of Android Studio that you sent me, I got wrong data for the differente characteristic, as you can see below:

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 0

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 1

D/AbsListView: onTouchUp() mTouchMode : 2

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af891 enable: true

D/BluetoothLeService: Humidity format UINT8.

D/BluetoothLeService: Received Humidity: 2

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 0

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 1

D/AbsListView: onTouchUp() mTouchMode : 0

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af891 enable: false

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af892 enable: true

D/BluetoothLeService: Temperature format uint8.

D/BluetoothLeService: Received temperature: 0

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 0

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 1

D/AbsListView: onTouchUp() mTouchMode : 0

D/AbsListView:  in onLayout changed

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 0

D/ViewRootImpl@f7617ff[DeviceControlActivity]: ViewPostIme pointer 1

D/AbsListView: onTouchUp() mTouchMode : 0

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af892 enable: false

D/BluetoothGatt: setCharacteristicNotification() - uuid: 1c9c78f8-dd31-4408-8ca4-766c8be7c261 enable: true

D/BluetoothLeService: Proximity format UINT32.

D/BluetoothLeService: Received proximity: 0

What can happen?

On the other hand, you added new lines to PSoC Creator project and there are some things that I don´t undestand. Then, I tell you what I dont understand if you can explain it to me.

1.- I dont understand what is that this function do and the TimerCounter used in the design of this projecto (which is related to this part of code that is located in main.c):

void SensorScanISR()

{

    sensor_isr_Disable();

    updateSensorsFlag = 1;

    sensor_isr_ClearPending();

    SensorScanTimer_ClearInterrupt(SensorScanTimer_INTR_MASK_TC);

}

2.-  What do you obtain of the next line located in proximity.c?

proximity = (uint32*) (CapSense_PROXIMITY0_SNS0_DIFF_VALUE - MIN_PROXIMITY_SIGNAL);

3.- The next function located in DHT22.c convert a 8-bit binary number to a fractional decimal number, but what is the param "num" and where is its value assigned? why do you want obtain a fractional decimal number? :

float Fraction_Convert(uint8_t num)

4.- The function expectPulse(uint32_t pinState) and the first part of the function DHT_Read(uint8* humedad, uint8* temperatura) I suppose that they carry out the working time line oh the DHT22 sensor until that you calculated the checksum, after that the values of temperature and humidity are calculated, right?

5.- Finally, what is the function of PWM used in the design? And what do ADV_LED, CONN_LED, DISCONN_LED indicate?

Sorry if these are easy questions but like I told you I am new in this world and I want to know everything I am doing.

Regards, Antonio.

0 Likes
DheerajK_81
Moderator
Moderator

Hello Antonio,

Answering your questions:

" Unable to find component "cy_boot_v5_90" "

Sometimes if you are on an older cy_boot version you will get this error. Once you update components, this error should go away.

I got wrong data for the differente characteristic, as you can see below:

Please check the pins tab for the different connections and ensure you have connected the sensors correctly. Double-check the UUIDs of the characteristics in the PSoC Creator project and in Android Studio. Not sure if they change when you open it in your system. Did you modify anything?

I dont understand what is that this function do and the TimerCounter used in the design of this projecto (which is related to this part of code that is located in main.c):

void SensorScanISR()

{

    sensor_isr_Disable();

    updateSensorsFlag = 1;

    sensor_isr_ClearPending();

    SensorScanTimer_ClearInterrupt(SensorScanTimer_INTR_MASK_TC);

}

I have set up a timer to read the sensor values every 5s. The function you see above is the ISR of the Timer. It updates the flag updateSensorsFlag to 1. In the main function, this flag is being checked in the for loop. If this flag is true, then sensor values are read. This ensures that the BLE connection intervals are met and no disconnection takes place.

2.-  What do you obtain of the next line located in proximity.c?

proximity = (uint32*) (CapSense_PROXIMITY0_SNS0_DIFF_VALUE - MIN_PROXIMITY_SIGNAL);

Refer this code example for more details: https://www.cypress.com/documentation/code-examples/ce225691-psoc-4-capsense-proximity

3.- The next function located in DHT22.c convert a 8-bit binary number to a fractional decimal number, but what is the param "num" and where is its value assigned? why do you want obtain a fractional decimal number? :

DHT22 sensor returns DATA in a format that has an integral and decimal part. If you want the decimal part you can use the Fraction_Convert function. In your case, you can forget this because the format in which you send data over BLE is uint8. If you want to send a float value you can consider this.

4.- The function expectPulse(uint32_t pinState) and the first part of the function DHT_Read(uint8* humedad, uint8* temperatura) I suppose that they carry out the working time line oh the DHT22 sensor until that you calculated the checksum, after that the values of temperature and humidity are calculated, right?

Yeah, the value returned by the sensor is read and validated against the checksum.

5.- Finally, what is the function of PWM used in the design? And what do ADV_LED, CONN_LED, DISCONN_LED indicate?

The LEDs are used merely for BLE connection status indication.

  • DISCONN_LED - PWM Blinks the RED led every second
  • ADV_LED - BLUE LED comes on when the BLE advertising begins
  • CONN_LED - GREEN LED comes on when BLE is connected

Feel free to ask as many questions as you like. Happy to help. There is no right or wrong question

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Okey now all is clear, thanks you.

I hace checked the pins and the UUIDs and everything is correct and I have not changed anything.

For the DHT22 sensor the problem can be stay in the next part of code?

uint8 checksum = (data[0] + data[1] + data[2] + data[3]) & 0xFF;

   

    if((checksum == data[4]) && (data[4] != 0))

    { 

        *humidity = (int)data[0]; //+ Fraction_Convert(data[1]);

        *temperature = (int)data[2]; //+ Fraction_Convert(data[3]);

       

        return true;

    }

    else

        return false;

Maybe the issue is here because the data of humidity, for example, is the sum of the 8 first bits (data[0]+data[1]) or not?

I try to put the code that I have in my old project, but I dont obtain correct data.

    *humidity     = (int)(data[0] * 256 + data[1]);     

And the proximity value continues to give me 0. What can be the issue?

0 Likes
DheerajK_81
Moderator
Moderator

Please read this blog to understand the data being returned from the DHT sensor: Interfacing DHT-11 with PSoC 6

Data[0] - Humidity (whole number part)

Data[1] - Humidity (fractional part)          

Data[2] - Temperature (whole number part)

Data[3] - Temperature (fractional part)

Data[4] - Checksum

Since you are sending uint8 value over BLE and not float, the fractional part can be discarded. Only the whole number part is enough.

Regarding proximity, please follow the code example to implement it. I have implemented only humidity and temperature portion in the example project I provided. I have however created functions for sending proximity values over BLE in the updateProximity() function.

You need to implement getProximity() function by following the code example. You don't need to change anything else. The sensor scanning functions have been implemented for proximity in the ISR.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

DheerajK_81​ So why am I not getting temperature and humidity data well?

0 Likes
DheerajK_81
Moderator
Moderator

Please debug and check the values being returned from the sensor. If you have an analyzer, analyze the scope shots and check what might be going wrong. You can attach the scope shots here so that we can get insights too. I'm assuming you have all the pin connections right.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

DheerajK_81

Hi, I dont have analyzer. I do a debug and I obtain this:

0 DHT_Read(uint8 * humidity = 0x20001bbd <humidityValue> "\003", uint8 * humidity@entry = 0x20001bbd <humidityValue> "\003", uint8 * temperature = 0x20001bbc <tempValue> "", uint8 * temperature@entry = 0x20001bbc <tempValue> "") DHT22.c 181 0x000007D6 (All)

DEBUG.JPG

0 Likes
DheerajK_81
Moderator
Moderator

Hello Antonio,

From the screenshots attached to the debug window, you can see that the Humidity and Temperature values are incorrect. The code I attached should have worked, not sure if your sensor uses different timing. Please modify the .c file of DHT according to the sensor datasheet you have.

Regarding the notifications not being enabled, can you please tell me if you are using the PSoC Creator project I sent or yours? If you run the Android Application and the PSoC Creator project I sent you, the notifications work as expected. You only need to make sure the DHT sensor values are being returned correctly.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81

Regarding notifications, I am working with my project, both from PSoC Creator and Android Studio, since I receive all the data correctly and that with the CySmart application everything works well, I do not see the inconvenience of continuing to work with the project PSoC Creator of mine, I don't know if you understand me. Speaking of the Android application, I have already tried the code that you told me for notifications and it does not work for me, could I not make it work in one of the two ways that I have passed it before? With the first one that is separated into 3 different paragraphs, I only receive humidity and I receive the data that I put on you, while for the last form that happened to you yesterday, I receive the 3 data correctly, but with the problem that I have to follow the order I told you and once I get damp I can no longer change anyone else.

Let's see if you know how I can make it work that way, it's the only thing I have left to finish my work.

Greetings, thanks.

0 Likes
DheerajK_81
Moderator
Moderator

Hello antoniam_91​,

Since DHT readings are obtained correctly on your project, please copy the code for the DHT part into the project I attached. The reason I'm asking you to migrate to my project is that I have synchronized the code with the help of interrupts to be able to send the data via BLE within the specified connection interval.

In your project, you will notice that although you get the readings on the Android App, it doesn't update as fast and plus it hangs when you keep switching between the services.

I think the best way forward is since the project I attached is tested to work both on Android side and PSoC side, please make use of that project. If you face problems with the DHT code I have written, you can make use of your DHT code inside my project. You should be able to get the readings correctly.

You do not need to edit anything in the Android Project I attached, you can run it as is. I have tested it and attached screenshots in the previous interactions to show how seamlessly the values get updated.

In your project, since everything is written inside the for loop the timing cannot be predicted and hence you see issues on the BLE side. Not that it won't work, it may or may not. The behavior is unpredictable.

Hope this makes sense. Let me know if you need any clarifications

Regards,
Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

Okey, I will try to migrate to your project, if I have some question, I told you.

Thank you

Antonio

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

I wrote you a private message, I dont know if you have seen it. I have migrated my DHT22 code, but I dont obtain any valor in the app.

I dont know why dont I obtain any valor, can you do this migration?

Regards, Antonio.

0 Likes
DheerajK_81
Moderator
Moderator

Hello Antonio,

Can you isolate the BLE component and then run your tests on the DHT? Once you have DHT11 code working, you can then work on the BLE part. The DHT22 sensor I have works perfectly with the code I attached. If the timings are different for your sensor, please check the datasheet and ensure you follow them.

If you can't get it to work, please attach your latest project and the sensor datasheet.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

Here I attach you my PSoC project and the DHT22 datasheet. I see that you critical time is very similar mine. I tried again to run your project and I get the same (humidity=2 and temperatura=0), I also changed the sensor and I get the same. Please make the change and check if it works well with your components

Regards, Antonio.

0 Likes
DheerajK_81
Moderator
Moderator

I added just the DHT files I sent you to the project you attached and I was able to observe the values correctly. I have attached the modified project. Note that I have isolated the BLE functionalities just so that we can get the DHT working on your side first. The timings are the same as the datasheet, so it should work,

dht22_vals.PNG

Please let me know what values you observe when you run this project.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

DheerajK_81

I dont get the data well, I attach the photos. I add the BLE part to check in cysmart app but nothing. I dont understand why we dont obtain the same value with the same project.

0 Likes
DheerajK_81
Moderator
Moderator

Do you have another sensor to test this with? Please check with any of your earlier working codes to see if you get the values correctly.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

Yes I have other similar sensor. I try three days ago to put the other sensor but I get similar result. With my project I obtain the correct data with the two sensor.

0 Likes
AnMo_4196841
New Contributor II

I think the error are in this part of code:

if((checksum == data[4]) && (data[4] != 0))

    {

        /* Pass the temperature and humidity data only when checksum is matched and it is not equal to 0 */

        *humidity = (int)data[0]; //+ Fraction_Convert(data[1]);

        *temperature = (int)data[2]; //+ Fraction_Convert(data[3]);

       

        return true;

    }

In my project, I obtain the correct value of the data and I use the two first bytes to calculate humidity and the two second bytes to calculate temperature. However, you only use data[0] to calculate humidity and data [2] to calculate temperature.

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​, I need to finish the project this week. Do you have any idea to solve the problem?

0 Likes
AnMo_4196841
New Contributor II

Hi my friend DheerajK_81​,

Do you have any new news?

0 Likes
AnMo_4196841
New Contributor II

Hi my friend DheerajK_81​,

I have tried several things with the projects you sent me and I don't get anything wll result.


Going back to my projects that I had (which I am attaching to you again in this message), the PSoC Creator project I have tested with the CySmart application and it works perfectly, on the other hand with my Android Studio application (Attached here also "BluetoothLeGatt1") humidity works without problem but if I click on the temperature or proximity it gives me the following error:

D/AbsListView: onTouchUp() mTouchMode : 0

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af891 enable: false

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af891 enable: true

D/ViewRootImpl@361f641[DeviceControlActivity]: ViewPostIme pointer 0

D/ViewRootImpl@361f641[DeviceControlActivity]: ViewPostIme pointer 1

D/AbsListView: onTouchUp() mTouchMode : 0

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af891 enable: false

D/BluetoothGatt: setCharacteristicNotification() - uuid: 51f76976-e4f8-4d9a-aa70-3168cc7af892 enable: true

D/AndroidRuntime: Shutting down VM

E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.android.bluetoothlegatt, PID: 13941

    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference

        at com.example.android.bluetoothlegatt.BluetoothLeService.setCharacteristicNotification(BluetoothLeService.java:358)

        at com.example.android.bluetoothlegatt.DeviceControlActivity$3.onChildClick(DeviceControlActivity.java:166)

The error is produced when enabling the notifications of a characteristic (in this case temperature and proximity). The code I have written for this part is as follows:

/**

*
*
Enables or disables notification in a delivery feature.

*
* @param characteristic Characteristic to  act.
* @param enabled If is true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
  boolean enabled) {

   if (mBluetoothAdapter == null || mBluetoothGatt == null) {

  Log.w(TAG, "BluetoothAdapter no inicializado");
  return;
   }

   mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

  if (UUID_HUMIDITY_MEASUREMENT.equals(characteristic.getUuid())) {

  BluetoothGattDescriptor descriptor = characteristic.getDescriptor(

  UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG_HUM));
   descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
   mBluetoothGatt.writeDescriptor(descriptor);
   }

   if (UUID_TEMPERATURE_MEASUREMENT.equals(characteristic.getUuid())) {

  BluetoothGattDescriptor descriptor = characteristic.getDescriptor(

  UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG_TEMP));
   descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
   mBluetoothGatt.writeDescriptor(descriptor);
   }

   if (UUID_PROXIMITY_MEASUREMENT.equals(characteristic.getUuid())) {

  BluetoothGattDescriptor descriptor = characteristic.getDescriptor(

  UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG_PROX));
   descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
   mBluetoothGatt.writeDescriptor(descriptor);
   }

}

I do not understand why I get the humidity data well and the others do not, please explain it to me if you know.

Regards, Antonio.

0 Likes
DheerajK_81
Moderator
Moderator

Hello Antonio,

In the following function, please make these changes.

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
  boolean enabled) {

   if (mBluetoothAdapter == null || mBluetoothGatt == null) {

  Log.w(TAG, "BluetoothAdapter no inicializado");
  return;
   }

   mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

}

You don't need the rest of the code to set the descriptor based on the characteristics etc. The function mBluetoothGatt.setCharacteristicNotification(characteristic, enabled) does this for you and is enough.

Let me know your observations with the changes.

Regards,

Dheeraj

0 Likes
AnMo_4196841
New Contributor II

Hi DheerajK_81​,

I try to put only the code that you told me and it doesn´t work.

In the PSoC Creator project I wrote that to obtain the data of the different characteristics you have to put the notification enabled, if I do it like you tell me I only obtain 0 for all characteristic and the data isn´t shown on the LCD, that means that the notification in not enabled.

However, as I had, I got the humidity data, so that meant the notification was turned on. What can I do?

Regards, Antonio.

0 Likes
AnMo_4196841
New Contributor II

DheerajK_81

Sorry, with the last part of code that I sent you I only obtain humidity data.

Whit the next part of code I obtain all characteristics data if I continue the order that I told you to show its (proximity, temperature, humidity).

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
  boolean enabled) {

  if (mBluetoothAdapter == null || mBluetoothGatt == null) {

  Log.w(TAG, "BluetoothAdapter no inicializado");
  return;
  }

  mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

  if (UUID_HUMIDITY_MEASUREMENT.equals(characteristic.getUuid())

  ||UUID_TEMPERATURE_MEASUREMENT.equals(characteristic.getUuid())

  ||UUID_PROXIMITY_MEASUREMENT.equals(characteristic.getUuid())) {

  BluetoothGattDescriptor descriptor = characteristic.getDescriptor(

  UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG_HUM));
  descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  mBluetoothGatt.writeDescriptor(descriptor);
  }

If I change CLIENT_CHARACTERISTIC_CONFIG_HUM in the line UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG_HUM)); for another one like CLIENT_CHARACTERISTIC_CONFIG_TEMP or CLIENT_CHARACTERISTIC_CONFIG_PROX. I cant obtain nothing.

0 Likes