Read an array of ADC readings as a bluetooth characteristic: ANDROID

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

cross mob
lock attach
Attachments are accessible only for community members.
JeCr_2235141
Level 2
Level 2
10 replies posted 5 replies posted 10 questions asked

Hello,

I have a question regarding PSOC4 BLE data transmission to Android App developed in Android Studio. 

I configure analog pin in PSOC Creator to connect with an ADC converter and then transmit this value to Android App.  For the initial single integer data, this works well, and I receive the data in the Android App and can plot very well.

The bandwidth is a bit low (~100Hz), so I increased the number of samples transmitted with each read request (increasing MTU to 512).  The analog in integer becomes an array, and I run a loop to fill up the array before updating the data transmission.  I have connected with Cypress CYSmart Dongle and I can see my data showing up on the console when I enable notifications.


After enabling notifications in Android, and trying to read the AnalogIn characteristic, the data is always displaying Null.  I try different offsets, but doesnt change.  I have read many forums on this, but I cannot seem to find the issue.

Enabling Bluetooth characteristic Notification in Android (Bluetooth Low Energy ) Not Working - Stac...

http://www.cypress.com/comment/340476

This is code in Android for enabling notifications to BLE.  The CAPSENSE in project still works, and seems to provide some data, however the array only returns null.

public boolean setCharacteristicNotification(BluetoothGattCharacteristic characteristic,boolean enable,BluetoothGattDescriptor descriptor)

{

   mBluetoothGatt.setCharacteristicNotification(characteristic, enable);

  descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});

   return mBluetoothGatt.writeDescriptor(descriptor); }//descriptor write operation successfully started?

Thanks for any help you can provide.

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

Hello,

Please find the attached project in which I'm able to send 509 bytes of data continuously when notification was enabled at client side.

The API CyBle_GattcExchangeMtuReq() is used by the GATT Client to send Maximum Transmitted Unit (GATT MTU) supported by the GATT Client. Default GATT MTU size as per Bluetooth 4.1 core specification is 23 bytes. If the GATT Client supports a size greater than the default, it has to invoke this function with the desired GATT MTU size. This function should only be initiated once during a connection.

Please note that for Some of the mobiles( with having Bluetooth version 4.0 or lower) will not send the MTU request to the peripheral devices. So these devices accepts the default MTU size of length 23 bytes only. Can you please check with mobile phones with having Bluetooth v4.1 or higher and also check with Android CySmart App.

Please let me know if this helps.

Thanks,

P Yugandhar.

View solution in original post

0 Likes
10 Replies
JeCr_2235141
Level 2
Level 2
10 replies posted 5 replies posted 10 questions asked

Some update.

I have noticed on some other threads that Android phones cannot have MTU > 23.   I have adjusted my android code and PSOC project and now it works fine.  At least instaed of a 100Hz Bandwidth, I am up to 2Khz seems.

But is there any way around this ?  It seems quite limiting for some applications and I am not sure why Iphone would be able to go up to 500MTU while Android sits at 23?  I would appreciate anyone's time with this specific question.

0 Likes

Hello,

Client will send the MTU request to the peripheral. CYBLE_EVT_GATTS_XCNHG_MTU_REQ event will be generated at the Peripheral device. The client and server exchange the maximum size of a packet that can be received using the Exchange MTU Request and Response PDUs. Both devices then use the minimum of these exchanged values for all further communication. Please refer the Day024_Throughput example project for more information on sending higher Throughput data.

Can you please let me know the Bluetooth version of the Android mobiles which you are using. Some of the mobiles( with having Bluetooth version 4.0) will not send the MTU request to the peripheral devices. So these devices accepts the default MTU size of length 23 bytes and these devices will not accept more than 23bytes MTU size.

Can you please resend the project. In the PSoC Creator, go to File ->Create Workspace Bundle -> Select minimal. Please attach the generated file.

Thanks,

P Yugandhar.

Hello,

Thanks for your comments.

I am using Nexus for development, which has bluetooth 4.0.

https://www.androidcentral.com/nexus-5-specs

I will update project here shortly.

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

Attached is the workspace bundle.  You will find I am working from cypress capsenseled making modifications as necessary.

I had taken a look at the throughput tutorial, but as I am working with an Android App receiving the data, I perhaps did not look as closely.  I will review.

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

Hello,

Please find the attached project in which I'm able to send 509 bytes of data continuously when notification was enabled at client side.

The API CyBle_GattcExchangeMtuReq() is used by the GATT Client to send Maximum Transmitted Unit (GATT MTU) supported by the GATT Client. Default GATT MTU size as per Bluetooth 4.1 core specification is 23 bytes. If the GATT Client supports a size greater than the default, it has to invoke this function with the desired GATT MTU size. This function should only be initiated once during a connection.

Please note that for Some of the mobiles( with having Bluetooth version 4.0 or lower) will not send the MTU request to the peripheral devices. So these devices accepts the default MTU size of length 23 bytes only. Can you please check with mobile phones with having Bluetooth v4.1 or higher and also check with Android CySmart App.

Please let me know if this helps.

Thanks,

P Yugandhar.

0 Likes

Hello,

This works.  Thanks for the assistance.  I was able to confirm with both CYSMART app as well as android device, although, the android app is having some implementation issues I am working on.

In the CySmart app, it appears that reading this characteristic yield a ~500 length array.  However, I do not see explicit negotiation of MTU in either PSOC Code or in CYSMART app. 

In Android App, after discovering services, I am using mBluetoothGatt.requestMtu(mtu) to request changing the MTU.  When I do so, the app has been crashing, thought I cannot find root cause yet.  Any feedback would be appreciated.

file.jpg

0 Likes

Hello,

Some additional Info.

In Android App, after discovering services, I am using mBluetoothGatt.requestMtu(mtu) to request changing the MTU. 

When I change values to 10 or numbers less than 20, it is OK, and broadcast reciever is recieving data.

When I change values above 23, no data is recieved.

When I change MTU back to say 20, the app no longer works either. 

When I disconnect power from the PSOC4, or push reset, and make connection again, the app is again working.

0 Likes

Please share the error seen in the logcat window when the app crashes in Android Studio. It will provide more information and help us debug further.

Regards,

Dheeraj

lock attach
Attachments are accessible only for community members.

Hello,

Sorry for delay.  I would really appreciate some help closing this ticket, and I think we are almost there.

I have verified PSOC Creator code works with CySmart laptop application & USB dongle.  MTU =500, chunks of data;

I have also verified on android CySmart App that  the MTU is negotiated and chunks of data are ~500 I believe.  Definitely > 20.

In android App however, some difficulty.  When I set MTU value to any number < 20, it seems to function.  I change the value using the command mBluetoothGatt.requestMtu(mtu), where MTU is some integer.

When I set the value > 20, 23 for example, I get an error.  The way I get the values from the characteristic, are I dynamically instantiate an array of length MTU, and I cycle through a for loop MTU times filling this array with

characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT8,ii).toString()

When I reach ii > 20, I get the error that it is null.

Perhaps there is a better way to do this?  But I am also not sure why the characteristic is returning a null value after request.

Thanks

0 Likes

I guess my question is what does Cypress do differently in CySmart app to handle MTU request?

0 Likes