1 2 3 Previous Next 35 Replies Latest reply on Jun 1, 2020 9:32 AM by AnMo_4196841

    Bluetooth LE Gatt

    AnMo_4196841

      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

        • 1. Re: Bluetooth LE Gatt
          DheerajK_81

          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

          • 2. Re: Bluetooth LE Gatt
            AnMo_4196841

            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

            • 3. Re: Bluetooth LE Gatt
              DheerajK_81

              Can you also attach the peripheral side of the project?

               

              Regards,

              Dheeraj

              • 4. Re: Bluetooth LE Gatt
                AnMo_4196841

                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.

                • 5. Re: Bluetooth LE Gatt
                  AnMo_4196841

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

                  • 6. Re: Bluetooth LE Gatt
                    AnMo_4196841

                    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.

                    • 7. Re: Bluetooth LE Gatt
                      DheerajK_81

                      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

                      • 8. Re: Bluetooth LE Gatt
                        AnMo_4196841

                        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.

                        • 9. Re: Bluetooth LE Gatt
                          AnMo_4196841

                          Hi DheerajK_81,

                           

                          Do you have new news about that I told you?

                           

                          Regards

                          • 10. Re: Bluetooth LE Gatt
                            DheerajK_81

                            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

                            • 11. Re: Bluetooth LE Gatt
                              AnMo_4196841

                              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.

                              • 12. Re: Bluetooth LE Gatt
                                DheerajK_81

                                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

                                • 13. Re: Bluetooth LE Gatt
                                  AnMo_4196841

                                  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?

                                  • 14. Re: Bluetooth LE Gatt
                                    DheerajK_81

                                    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

                                    1 2 3 Previous Next