BLE Android studio & BLE101 example

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

cross mob
DaLi_2621766
Level 1
Level 1

Hi all,

working on Android studio with BLE101 capsense and led example... Once the UUID are rewrite with the correct ones the application starts work. But wonder if is it possible to use 16bits UUID instead 128?...it is not possible to configure the BLE units with few Services and characteristics with 128bits...

When I changed to 16bits UUID, the application crashes...

Any idea? I am doing somthing wrong on JAVA?

Thanks,

DL.

// UUIDs for the service and characteristics that the custom CapSenseLED service uses
private final static String baseUUID = "00000000-0000-1000-8000-00805f9b34f";

//private final static String baseUUID = "AAA";
private final static String capsenseLedServiceUUID = baseUUID + "0";

//private final static String capsenseLedServiceUUID = "AAA0";
public final static String capsenseCharacteristicUUID = baseUUID + "2";

//public final static String capsenseCharacteristicUUID = "AAA1";
private final static String CccdUUID = "00002902-0000-1000-8000-00805f9b34fb";

//private final static String CccdUUID = "AAA2";

0 Likes
1 Solution
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Yes definitely you can use 16 bit UUIDs. Please note that UUID class used in Android requires a 128-bit value. The solution is to prepend the 16-bit value to the Base Bluetooth UUID​ (0000xxxx-0000-1000-8000-00805F9B34FB)

Here, the x is where the 16-bit value will be prepended to. Refer Service Discovery Protocol Specification to understand more about this in the Bluetooth Core Spec.

So, in essence, you create a function in this fashion:

public static final String baseUUIDSuffix = "0000-1000-8000-00805F9B34FB"

/* Converts 16bit UUIDs to 128-bit format */

public static UUID convertFrom16bit(string shortuuid16){

     return UUID.fromString("0000" + shortuuid16 + "-" + baseUUIDSuffix);

}

/* Converts 32-bit UUIDs to 128-bit format */

public static UUID convertFrom32bit(string shortuuid32){

     return UUID.fromString(shortuuid32 + "-" + baseUUIDSuffix);

}

/* Example usage */

private static capsenseLEDServiceUUID = "AAA0";

UUID CapsenseLEDService = convertFrom16bit(capsenseLEDServiceUUID);

Your application was crashing due to InvalidArgumentException because of the 16-bit UUID instead of required 128-bit. Please make the above changes and it will work.

Regards,

Dheeraj

View solution in original post

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

Yes definitely you can use 16 bit UUIDs. Please note that UUID class used in Android requires a 128-bit value. The solution is to prepend the 16-bit value to the Base Bluetooth UUID​ (0000xxxx-0000-1000-8000-00805F9B34FB)

Here, the x is where the 16-bit value will be prepended to. Refer Service Discovery Protocol Specification to understand more about this in the Bluetooth Core Spec.

So, in essence, you create a function in this fashion:

public static final String baseUUIDSuffix = "0000-1000-8000-00805F9B34FB"

/* Converts 16bit UUIDs to 128-bit format */

public static UUID convertFrom16bit(string shortuuid16){

     return UUID.fromString("0000" + shortuuid16 + "-" + baseUUIDSuffix);

}

/* Converts 32-bit UUIDs to 128-bit format */

public static UUID convertFrom32bit(string shortuuid32){

     return UUID.fromString(shortuuid32 + "-" + baseUUIDSuffix);

}

/* Example usage */

private static capsenseLEDServiceUUID = "AAA0";

UUID CapsenseLEDService = convertFrom16bit(capsenseLEDServiceUUID);

Your application was crashing due to InvalidArgumentException because of the 16-bit UUID instead of required 128-bit. Please make the above changes and it will work.

Regards,

Dheeraj

0 Likes

Thanks for your professional answer!!

unfortunately I am still suffering of some issues implementing your solution:

/* Example usage */

private static capsenseLEDServiceUUID = "AAA0";
UUID CapsenseLEDService = convertFrom16bit(capsenseLEDServiceUUID);

this use is assigning CapsenseLEDService  as UUID type(class)...and now I need to convert is back to string...

I don't know how to do it.

Thanks,

Daniel.

0 Likes

Hello Dheeraj

Trying resolve my issue with the 16bit instead 128

I used the function you suggested:

public static final String baseUUIDSuffix = "0000-1000-8000-00805F9B34FB";
/* Converts 16bit UUIDs to 128-bit format */
public static UUID convertFrom16bit(String shortuuid16) {
return UUID.fromString("0000" + shortuuid16 + baseUUIDSuffix );


1) first question here, Is a "-" sign missing before the baseUUIDsuffix ?

2) Is "0" missing after the baseUUIDsuffix?

using it.

If I used the following two lines, the AAP crashed:

UUID capsenseLedService = convertFrom16bit(baseUUID_ADD);
UUID[] capsenseLedServiceArray = {capsenseLedService};

When I use :

UUID capsenseLedService = UUID.fromString(capsenseLedServiceUUID);
/* Example usage */
//UUID capsenseLedService = convertFrom16bit(baseUUID_ADD);
UUID[] capsenseLedServiceArray = {capsenseLedService};

Its works ok.

I can't understand why.  the convert16bits function should return same like UUID.fromString(capsenseLedServiceUUID);

Do you have any thoughts that can help me?

Appreciate really appreciate your help!!

  1. Daniel.
0 Likes

Yes, sorry missed a "-" in the code. I have edited the previous interaction with the right one. There is no 0 missing, it is correct.

Not sure why it is crashing, can you tell me what baseUUID_ADD is? Please connect your phone to the PC via USB and then run the app in Android Studio. At the bottom, you will see the logs being generated in the LogCat window. Please share the logs when the app crashes.

Regards,

Dheeraj

0 Likes

Thanks for your help.

anyway, I fixed it using the following:

/* Converts 16bit UUIDs to 128-bit format */
public static String convertFrom16bit(String shortuuid16){
return "0000" + shortuuid16 + baseUUIDSuffix;
//return UUID.fromString("0000" + shortuuid16 + baseUUIDSuffix);
}

And then it started work.

Thanks very much for your help!!

Best regards,

  1. Daniel.
0 Likes

Hi,

I changed the function to :

/* Converts 16bit UUIDs to 128-bit format */

public static String convertFrom16bit(String shortuuid16){

return "0000" + shortuuid16 + baseUUIDSuffix;

//return UUID.fromString("0000" + shortuuid16 + baseUUIDSuffix);

}

And then it started work.

Thanks very much for your help!!

Best regards,

Daniel.

0 Likes