cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 4 MCU

MaSa_2723681
New Contributor II

Hello, Using the CY8KIT-042-BLE-A kit and made a demo using the CySmart Android App found here:

https://www.cypress.com/documentation/software-and-drivers/cysmart-mobile-app

How do I remove the GATT DB Screen from the Carousel View? While GATT DB service is very handy for debug I would like to remove it for my demo and be able to easily put it back for debug. I tried things like mPager.removeViewsInLayout(3, 1); and  mPager.removeViewAt(3); but these attempts just made the app crash.

From CySmart Android App User Guide.pdf

pastedImage_2.png

Thanks!

Mark

0 Likes
Reply
1 Solution
DheerajK_81
Moderator
Moderator

The reason you see the error is because when you remove the view, you haven't notified the adapter about this change and hence results in a null pointer exception as shown below:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference

Also, this assumes the position and in case there are more services or no services, the app will crash. Better than hardcoding this way, you could modify the function "prepareData" in "ServiceDiscoveryFragment.java" in the following way (lines: 43, 44, 47 and 111):

private void prepareData(List<BluetoothGattService> gattServices) {

   boolean mFindMeSet = false;

   boolean mProximitySet = false;

   boolean mGattSet = false;

   if (gattServices == null)

   return;

   // Clear all array list before entering values.
   mGattServiceData.clear();

   mGattServiceFindMeData.clear();

   mGattServiceMasterData.clear();

   // Loops through available GATT Services.
   for (BluetoothGattService gattService : gattServices) {

  HashMap<String, BluetoothGattService> currentServiceData = new HashMap<String, BluetoothGattService>();

  UUID uuid = gattService.getUuid();

   // Optimization code for FindMe Profile
   if (uuid.equals(UUIDDatabase.UUID_IMMEDIATE_ALERT_SERVICE)) {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   if (!mGattServiceFindMeData.contains(currentServiceData)) {

   mGattServiceFindMeData.add(currentServiceData);

  }

   if (!mFindMeSet) {

  mFindMeSet = true;

   mGattServiceData.add(currentServiceData);

  }

  }

   // Optimization code for Proximity Profile
   else if (uuid.equals(UUIDDatabase.UUID_LINK_LOSS_SERVICE)

  || uuid.equals(UUIDDatabase.UUID_TRANSMISSION_POWER_SERVICE)) {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   if (!mGattServiceProximityData.contains(currentServiceData)) {

   mGattServiceProximityData.add(currentServiceData);

  }

   if (!mProximitySet) {

  mProximitySet = true;

   mGattServiceData.add(currentServiceData);

  }

  }// Optimization code for GATTDB
   else if (uuid.equals(UUIDDatabase.UUID_GENERIC_ACCESS_SERVICE)

  || uuid.equals(UUIDDatabase.UUID_GENERIC_ATTRIBUTE_SERVICE)) {

   //currentServiceData.put(LIST_UUID, gattService);
  //mGattDbServiceData.add(currentServiceData);
   if (!mGattSet) {

  mGattSet = true;

   //mGattServiceData.add(currentServiceData);
   }

  } //Optimization code for HID
   else if (uuid.equals(UUIDDatabase.UUID_HID_SERVICE)) {

   /**
  * Special handling for KITKAT devices
  */
   if (android.os.Build.VERSION.SDK_INT < 21) {

  Logger.e("Kitkat RDK device found");

  List<BluetoothGattCharacteristic> allCharacteristics = gattService.getCharacteristics();

  List<BluetoothGattCharacteristic> RDKCharacteristics = new ArrayList<BluetoothGattCharacteristic>();

  List<BluetoothGattDescriptor> RDKDescriptors = new ArrayList<BluetoothGattDescriptor>();

   //Find all Report characteristics
   for (BluetoothGattCharacteristic characteristic : allCharacteristics) {

   if (characteristic.getUuid().equals(UUIDDatabase.UUID_REPORT)) {

  RDKCharacteristics.add(characteristic);

  }

  }

   //Find all Report descriptors
   for (BluetoothGattCharacteristic rdkcharacteristic : RDKCharacteristics) {

  List<BluetoothGattDescriptor> descriptors = rdkcharacteristic.

  getDescriptors();

   for (BluetoothGattDescriptor descriptor : descriptors) {

  RDKDescriptors.add(descriptor);

  }

  }

   /**
  * Wait for all descriptors to receive
  */
   if (RDKDescriptors.size() == RDKCharacteristics.size() * 2) {

   for (int pos = 0, descPos = 0; descPos < RDKCharacteristics.size(); pos++, descPos++) {

  BluetoothGattCharacteristic rdkCharacteristic = RDKCharacteristics.get(descPos);

   //Mapping the characteristic and descriptors
   Logger.e("Pos-->" + pos);

  Logger.e("Pos+1-->" + (pos + 1));

  BluetoothGattDescriptor clientdescriptor = RDKDescriptors.get(pos);

  BluetoothGattDescriptor reportdescriptor = RDKDescriptors.get(pos + 1);

   if (!rdkCharacteristic.getDescriptors().contains(clientdescriptor)) {

  rdkCharacteristic.addDescriptor(clientdescriptor);

  }

   if (!rdkCharacteristic.getDescriptors().contains(reportdescriptor)) {

  rdkCharacteristic.addDescriptor(reportdescriptor);

  }

  pos++;

  }

  }

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   mGattServiceData.add(currentServiceData);

  } else {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   mGattServiceData.add(currentServiceData);

  }

  } else {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   mGattServiceData.add(currentServiceData);

  }

  }

   mApplication.setGattServiceMasterData(mGattServiceMasterData);

   if (mGattServiceData.size() > 0) {

  updateWithNewFragment();

  } else {

  dismissProgressDialog();

  showNoServiceDiscoveredInfo();

  }

  }

}

The carousal view uses the adapter to set the fragments. The adapter uses the ArrayList of HashMaps<String, BluetoothGattService>. In the above code we comment out the code where the GattDB service is added to this ArrayList.

This way only the services other than GattDB get displayed as you wanted.

Regards,
Dheeraj

View solution in original post

0 Likes
Reply
2 Replies
DheerajK_81
Moderator
Moderator

The reason you see the error is because when you remove the view, you haven't notified the adapter about this change and hence results in a null pointer exception as shown below:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference

Also, this assumes the position and in case there are more services or no services, the app will crash. Better than hardcoding this way, you could modify the function "prepareData" in "ServiceDiscoveryFragment.java" in the following way (lines: 43, 44, 47 and 111):

private void prepareData(List<BluetoothGattService> gattServices) {

   boolean mFindMeSet = false;

   boolean mProximitySet = false;

   boolean mGattSet = false;

   if (gattServices == null)

   return;

   // Clear all array list before entering values.
   mGattServiceData.clear();

   mGattServiceFindMeData.clear();

   mGattServiceMasterData.clear();

   // Loops through available GATT Services.
   for (BluetoothGattService gattService : gattServices) {

  HashMap<String, BluetoothGattService> currentServiceData = new HashMap<String, BluetoothGattService>();

  UUID uuid = gattService.getUuid();

   // Optimization code for FindMe Profile
   if (uuid.equals(UUIDDatabase.UUID_IMMEDIATE_ALERT_SERVICE)) {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   if (!mGattServiceFindMeData.contains(currentServiceData)) {

   mGattServiceFindMeData.add(currentServiceData);

  }

   if (!mFindMeSet) {

  mFindMeSet = true;

   mGattServiceData.add(currentServiceData);

  }

  }

   // Optimization code for Proximity Profile
   else if (uuid.equals(UUIDDatabase.UUID_LINK_LOSS_SERVICE)

  || uuid.equals(UUIDDatabase.UUID_TRANSMISSION_POWER_SERVICE)) {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   if (!mGattServiceProximityData.contains(currentServiceData)) {

   mGattServiceProximityData.add(currentServiceData);

  }

   if (!mProximitySet) {

  mProximitySet = true;

   mGattServiceData.add(currentServiceData);

  }

  }// Optimization code for GATTDB
   else if (uuid.equals(UUIDDatabase.UUID_GENERIC_ACCESS_SERVICE)

  || uuid.equals(UUIDDatabase.UUID_GENERIC_ATTRIBUTE_SERVICE)) {

   //currentServiceData.put(LIST_UUID, gattService);
  //mGattDbServiceData.add(currentServiceData);
   if (!mGattSet) {

  mGattSet = true;

   //mGattServiceData.add(currentServiceData);
   }

  } //Optimization code for HID
   else if (uuid.equals(UUIDDatabase.UUID_HID_SERVICE)) {

   /**
  * Special handling for KITKAT devices
  */
   if (android.os.Build.VERSION.SDK_INT < 21) {

  Logger.e("Kitkat RDK device found");

  List<BluetoothGattCharacteristic> allCharacteristics = gattService.getCharacteristics();

  List<BluetoothGattCharacteristic> RDKCharacteristics = new ArrayList<BluetoothGattCharacteristic>();

  List<BluetoothGattDescriptor> RDKDescriptors = new ArrayList<BluetoothGattDescriptor>();

   //Find all Report characteristics
   for (BluetoothGattCharacteristic characteristic : allCharacteristics) {

   if (characteristic.getUuid().equals(UUIDDatabase.UUID_REPORT)) {

  RDKCharacteristics.add(characteristic);

  }

  }

   //Find all Report descriptors
   for (BluetoothGattCharacteristic rdkcharacteristic : RDKCharacteristics) {

  List<BluetoothGattDescriptor> descriptors = rdkcharacteristic.

  getDescriptors();

   for (BluetoothGattDescriptor descriptor : descriptors) {

  RDKDescriptors.add(descriptor);

  }

  }

   /**
  * Wait for all descriptors to receive
  */
   if (RDKDescriptors.size() == RDKCharacteristics.size() * 2) {

   for (int pos = 0, descPos = 0; descPos < RDKCharacteristics.size(); pos++, descPos++) {

  BluetoothGattCharacteristic rdkCharacteristic = RDKCharacteristics.get(descPos);

   //Mapping the characteristic and descriptors
   Logger.e("Pos-->" + pos);

  Logger.e("Pos+1-->" + (pos + 1));

  BluetoothGattDescriptor clientdescriptor = RDKDescriptors.get(pos);

  BluetoothGattDescriptor reportdescriptor = RDKDescriptors.get(pos + 1);

   if (!rdkCharacteristic.getDescriptors().contains(clientdescriptor)) {

  rdkCharacteristic.addDescriptor(clientdescriptor);

  }

   if (!rdkCharacteristic.getDescriptors().contains(reportdescriptor)) {

  rdkCharacteristic.addDescriptor(reportdescriptor);

  }

  pos++;

  }

  }

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   mGattServiceData.add(currentServiceData);

  } else {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   mGattServiceData.add(currentServiceData);

  }

  } else {

  currentServiceData.put(LIST_UUID, gattService);

   mGattServiceMasterData.add(currentServiceData);

   mGattServiceData.add(currentServiceData);

  }

  }

   mApplication.setGattServiceMasterData(mGattServiceMasterData);

   if (mGattServiceData.size() > 0) {

  updateWithNewFragment();

  } else {

  dismissProgressDialog();

  showNoServiceDiscoveredInfo();

  }

  }

}

The carousal view uses the adapter to set the fragments. The adapter uses the ArrayList of HashMaps<String, BluetoothGattService>. In the above code we comment out the code where the GattDB service is added to this ArrayList.

This way only the services other than GattDB get displayed as you wanted.

Regards,
Dheeraj

View solution in original post

0 Likes
Reply
MaSa_2723681
New Contributor II

Thank you Dheeraj! I would have never figured that out.

It took me a few minutes to pick up the change on line 111, from

if (mGattDbServiceData.size() > 0) {

to

if (mGattServiceData.size() > 0) {

Thanks again! Works perfectly!

Mark