Cysmart Android Source Code

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
LuDi_2288466
Level 2
Level 2
10 replies posted 5 replies posted 5 questions asked

Hi,

I am using CYBLE-212020-01 module which sends with notify some values over BLE from App and made a demo using the CySmart Android App found here:

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

I want to pass easily from GATT DB screen from the carousel view to directly the sceen Gatt Charateristic Notify Details.

How I can to do this easily?

Thanks in advance for any replies.

Lucia

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.

You can go back to the ProfileScanningFragment directly by editing the back button onClickListener function as follows in GattDetailsFragment.java file:

mBtnBack.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

  FragmentManager fragmentManager = getFragmentManager();

  ProfileScanningFragment profileScanningFragment = new ProfileScanningFragment();

  fragmentManager.beginTransaction()

  .replace(R.id.container, profileScanningFragment)

  .addToBackStack(null).commit();

  }

});

Please find the full file attached for reference. Hope this helps

Regards,

Dheeraj

View solution in original post

0 Likes
11 Replies
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Hello Lucia,

It can be done, but it is not going to be an easy task. It will require some rework especially because most of the CySmart Android Source Code uses Fragments​ and not Activities.

I can guide you on how to proceed. In Android Studio, navigate to this CommonFragments folder in the path: "CySmart_Android_1.2.0.156_Source_Code\src\com\cypress\cysmart\". The files of importance here are:

(1) ProfileScanningFragment.java  - Starts the BLE scanning, finds the nearby devices and loads the list of devices into the list adapter LeDeviceListAdapter

(2) ServiceDiscoveryFragment.java - Starts Service Discovery and loads it into the ArrayList mGattServiceData.

(3) ProfileControlFragment.java - Passes the discovered services into the Carousel View by setting the adapter i.e CarouselPagerAdapter.

(4) CarouselFragment.java - Has the OnClick function implemented, which says which Service Fragment to create based on the service you have clicked.

Now that you know what each of the files mean. Let me explain the flow. It goes this way:

ProfileScanningFragment --> ServiceDiscoveryFragment --> ProfileControlFragment --> CarouselPagerAdapter --> CarouselFragment --> OnClick --> GattServiceFragment.create()

What you want is this:

ProfileScanningFragment --> GattServiceFragment.create()

So basically, you will need to implement the process in between i.e (ServiceDiscoveryFragment --> ProfileControlFragment --> CarouselPagerAdapter --> CarouselFragment) inside the ProfileScanningFragment file. You need not do everything the other files do.

You can strip down the code to just discover the services and then call this:

if (mService.getUuid().equals(UUIDDatabase.UUID_GENERIC_ATTRIBUTE_SERVICE)

  || mService.getUuid().equals(UUIDDatabase.UUID_GENERIC_ACCESS_SERVICE)) {

  GattServicesFragment gattSericesFragment = GattServicesFragment.create();

  displayView(gattSericesFragment, getResources().getString(R.string.gatt_db));

}

But note that it will have not have any of the error handling methods which check for disconnection, connection, onResume etc.. but the above implementation will serve your purpose.

Hope this helps

Regards,

Dheeraj

Hi Dheeraj,

Thanks very much for your reply.

Sorry I didn't understand very well where I put the following code :

if (mService.getUuid().equals(UUIDDatabase.UUID_GENERIC_ATTRIBUTE_SERVICE)

  || mService.getUuid().equals(UUIDDatabase.UUID_GENERIC_ACCESS_SERVICE)) {

  GattServicesFragment gattSericesFragment = GattServicesFragment.create();

  displayView(gattSericesFragment, getResources().getString(R.string.gatt_db));

}

Can you explain me, please?

How I can do this in my code?

In addition, I have another question.

If I wanted to keep the following flow

ProfileScanningFragment --> ServiceDiscoveryFragment --> ProfileControlFragment --> CarouselPagerAdapter --> CarouselFragment --> OnClick --> GattServiceFragment.create()

but after GattServiceFragment.create() I would like to go directly to

the GattDetailsFragment.

How can I change the code to do this?

Regards,

Lucia

0 Likes
lock attach
Attachments are accessible only for community members.

Answers inline:

if (mService.getUuid().equals(UUIDDatabase.UUID_GENERIC_ATTRIBUTE_SERVICE)

  || mService.getUuid().equals(UUIDDatabase.UUID_GENERIC_ACCESS_SERVICE)) {

  GattServicesFragment gattSericesFragment = GattServicesFragment.create();

  displayView(gattSericesFragment, getResources().getString(R.string.gatt_db));

}

Can you explain me, please?

You need to put this code in the ProfileScanningFragment.java along with other code to discover the services etc. You will need to implement the required code in the files files in the following order: ProfileScanningFragment --> ServiceDiscoveryFragment --> ProfileControlFragment --> CarouselPagerAdapter --> CarouselFragment --> OnClick --> GattServiceFragment.create()

but after GattServiceFragment.create() I would like to go directly to

the GattDetailsFragment.

How can I change the code to do this?

This is easier to implement since we only have to move the code from two files into the ProfileFragmentControl.java file. Please find the modified file attached. Replace it in your project and you will see that when you click the device it no longer shows the carousel window. It directly goes to the GattDetails. You need not make edits in any other file.

Let me know if you face any problems

Regards,
Dheeraj

0 Likes

Hi Dheeraj,

Thanks very much for your reply.

I modified the code with your file but  I saw this part of  code in the function onCreateView in ProfileControlFragment.java :

// Passing service details to GattCharacteristicsFragment and
// adding that fragment to the current view
Bundle bundle = new Bundle();

bundle.putString(Constants.GATTDB_SELECTED_SERVICE,

  selectedServiceName);

FragmentManager fragmentManager = getFragmentManager();

GattCharacteristicsFragment gattcharacteristicsfragment = new GattCharacteristicsFragment()

  .create();

gattcharacteristicsfragment.setArguments(bundle);

fragmentManager.beginTransaction()

  .add(R.id.container, gattcharacteristicsfragment)

  .addToBackStack(null).commit();

In this way I go directly to GattCharateristicsFragment but

I would like to go directly to the GattDetailsFragment.

/**
* Passing the characteristic details to GattDetailsFragment and
* adding that fragment to the view
*/
Bundle bundle = new Bundle();

bundle.putString(Constants.GATTDB_SELECTED_SERVICE,

   mGattServiceName);

bundle.putString(Constants.GATTDB_SELECTED_CHARACTERISTIC,

  characteristicsname);

FragmentManager fragmentManager = getFragmentManager();

GattDetailsFragment gattDetailsfragment = GattDetailsFragment.create();

gattDetailsfragment.setArguments(bundle);

fragmentManager.beginTransaction()

  .add(R.id.container, gattDetailsfragment)

  .addToBackStack(null).commit();

How can I change the code to do this?

Can you explain me, please?

Regards,

Lucia

0 Likes
lock attach
Attachments are accessible only for community members.

Hello Lucia,

Yes, you can go one level higher i.e directly into GattDetailsFragment too. Note that it won't work if you just call the fragment. You need to get the required characteristics and then go into the Fragment.

Please find the file attached which demonstrates how to do that. You can directly replace the ProfileControlFragment.java file with the attached file and it will work, no other changes required.

Let me know how it goes

Regards,
Dheeraj

0 Likes

Hi Dheeraj,

Thanks very much for your reply.

The CYBLE-212020-01 module is configured with one characteristic.

Is possible to skip the list of characteristics and then go directly to the GattDetailsFragment?

How can I change the code to do this?

Can you explain me, please?

Regards,

Lucia

0 Likes
lock attach
Attachments are accessible only for community members.

If you have a single characteristic, then yes you can directly to the Details portion of that characteristic by doing this:

mApplication.setBluetoothGattCharacteristic(mGattCharacteristics.get(0));

String characteristicuuid = mGattCharacteristics.get(0).getUuid().toString();

String characteristicsname = GattAttributes.lookupUUID(mGattCharacteristics.get(0).getUuid(),

  characteristicuuid);

You can provide the index as 0 to obtain the characteristic directly. Note that you are hardcoding here and if you have more than one characteristic you will need to implement the previous file I had attached.

Please find the file to go to Details fragment directly when only one characteristic is present.

Let me know how it goes

Regards,
Dheeraj

0 Likes

Hi Dheeraj,

Thanks very much for your reply. It's work now!

But when I pressed on back button in GattDettailsFragment, App show the the list of characteristics.

Is possible to skip the list of characteristics and then back directly to ProfileScanningFragment ?

How can I change the code to do this?

Can you explain me, please?

Regards,

Lucia

0 Likes
lock attach
Attachments are accessible only for community members.

You can go back to the ProfileScanningFragment directly by editing the back button onClickListener function as follows in GattDetailsFragment.java file:

mBtnBack.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

  FragmentManager fragmentManager = getFragmentManager();

  ProfileScanningFragment profileScanningFragment = new ProfileScanningFragment();

  fragmentManager.beginTransaction()

  .replace(R.id.container, profileScanningFragment)

  .addToBackStack(null).commit();

  }

});

Please find the full file attached for reference. Hope this helps

Regards,

Dheeraj

0 Likes

Hi Dheeraj,

Thanks for your quickly reply.

Now I have the following problem :

When I pressed on back button in GattDettailsFragment,

App show correctly the ProfileScanningFragment

but App is still connect with my device and then I don't see my device in list of scanning device.

How I can fix this?

Regards,

Lucia

0 Likes

Hello Lucia,

As I have mentioned previously, you need to implement all kinds of error handling when it comes to Bluetooth device disconnection, when the activity is resumed, when more characteristics are added, etc. The code I have given you is the crux of what you need to implement the solution as per your requirement. You need to build on top of that and make it fool-proof by looking at all the other fragment files and implementing the necessary functions.

To disconnect the device on the back button press, just add the BluetoothLeService.disconnect(); inside it as shown below:

mBtnBack.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

  BluetoothLeService.disconnect();

  FragmentManager fragmentManager = getFragmentManager();

  ProfileScanningFragment profileScanningFragment = new ProfileScanningFragment();

  fragmentManager.beginTransaction()

  .replace(R.id.container, profileScanningFragment)

  .addToBackStack(null).commit();

  }

});

Please try to implement the features you require by studying the files. If you face errors, please do let us know, we will be happy to help!

Regards,
Dheeraj

0 Likes