This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

As to Indicator Communication with Android app,

Hello,

I need your help, please review the following description and help to let me know the method to solve the issue,

thankful for your support in advance,

the symptom is ,

when using google app for ble communication with nrf51822, and trying to communicate as a indicator mode, 

I can't receive continuously after first touching in app in order to do indicate mode, after secondly touching in app, I can receive the data continuously, 

why ?

I update the log, please help to review it,

first touch, 

2018-11-19 16:26:59.344 13446-13446/com.example.android.bluetoothlegatt D/DeviceControlActivity: love PROPERTY_INDICATE
2018-11-19 16:26:59.347 13446-13446/com.example.android.bluetoothlegatt D/BluetoothLeService: love setCharacteristicIndication END
2018-11-19 16:26:59.347 13446-13446/com.example.android.bluetoothlegatt D/DeviceControlActivity: love PROPERTY_WRITE
2018-11-19 16:26:59.403 13446-13460/com.example.android.bluetoothlegatt D/BluetoothLeService: love onCharacteristicRead.
2018-11-19 16:26:59.404 13446-13460/com.example.android.bluetoothlegatt D/BluetoothLeService: love RAW_DATA format UINT8.
2018-11-19 16:26:59.417 13446-13460/com.example.android.bluetoothlegatt D/BluetoothLeService: love broadcastUpdate sendBroadcast execution

second touch, 

2018-11-19 16:27:13.713 13446-13446/com.example.android.bluetoothlegatt D/DeviceControlActivity: love PROPERTY_READ setCharacteristicIndication
2018-11-19 16:27:13.719 13446-13446/com.example.android.bluetoothlegatt D/BluetoothLeService: love setCharacteristicIndication END
2018-11-19 16:27:13.719 13446-13446/com.example.android.bluetoothlegatt D/DeviceControlActivity: love PROPERTY_INDICATE
2018-11-19 16:27:13.724 13446-13446/com.example.android.bluetoothlegatt D/BluetoothLeService: love setCharacteristicIndication END
2018-11-19 16:27:13.725 13446-13446/com.example.android.bluetoothlegatt D/DeviceControlActivity: love PROPERTY_WRITE
2018-11-19 16:27:13.802 13446-13499/com.example.android.bluetoothlegatt D/BluetoothLeService: love onCharacteristicChanged [B@3c4a623
2018-11-19 16:27:13.802 13446-13499/com.example.android.bluetoothlegatt D/BluetoothLeService: love RAW_DATA format UINT8.
2018-11-19 16:27:13.815 13446-13499/com.example.android.bluetoothlegatt D/BluetoothLeService: love broadcastUpdate sendBroadcast execution
2018-11-19 16:27:13.893 13446-13499/com.example.android.bluetoothlegatt D/BluetoothLeService: love onCharacteristicChanged [B@76b9720
2018-11-19 16:27:13.894 13446-13499/com.example.android.bluetoothlegatt D/BluetoothLeService: love RAW_DATA format UINT8.

thanks.

update the code, 

private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null)
{
Log.d(TAG, "love PROPERTY_READ setCharacteristicNotification");
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}

if (mIndicateCharacteristic != null)
{
Log.d(TAG, "love PROPERTY_READ setCharacteristicIndication");
mBluetoothLeService.setCharacteristicIndication(
mIndicateCharacteristic, false);
mIndicateCharacteristic = null;
}

mBluetoothLeService.readCharacteristic(characteristic);
}

// if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)
if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == BluetoothGattCharacteristic.PROPERTY_NOTIFY )
{
Log.d(TAG, "love PROPERTY_NOTIFY");
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, false);
}

// if ((charaProp | BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0)
if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) == BluetoothGattCharacteristic.PROPERTY_INDICATE )
{
Log.d(TAG, "love PROPERTY_INDICATE");
mIndicateCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicIndication(
characteristic, false);
}

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
Log.d(TAG, "love PROPERTY_WRITE");

if (UUID.fromString(SampleGattAttributes.THD_CONTROL_COMMAND).equals(characteristic.getUuid()))
{
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}

if (mIndicateCharacteristic != null) {
mBluetoothLeService.setCharacteristicIndication(
mIndicateCharacteristic, false);
mIndicateCharacteristic = null;
}

mBluetoothLeService.writeCharacteristic(characteristic);//love_1022
}
}
return true;
}
return false;
}
};

and the code of service is,

/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}

mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

if (UUID_THD_RAW_DATA_TRANSFER.equals(characteristic.getUuid()))
{
//love_0831 devzone.nordicsemi.com/.../android-enable-notification-client-side-cccd-uuid

final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}

public void setCharacteristicIndication(BluetoothGattCharacteristic characteristic,
boolean enabled) {//love_1022

if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}

mBluetoothGatt.setCharacteristicNotification(characteristic, true);

if (UUID_THD_RAW_DATA_TRANSFER.equals(characteristic.getUuid()))
{
//love_0831 devzone.nordicsemi.com/.../android-enable-notification-client-side-cccd-uuid

final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
Log.d(TAG, "love setCharacteristicIndication END");
}
}

public void writeCharacteristic(BluetoothGattCharacteristic characteristic){
// Toast.makeText(getApplicationContext(),"THD_CONTROL_COMMAND",Toast.LENGTH_SHORT).show();

if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "writeCharacteristic BluetoothAdapter not initialized");
return;
}

flag_count++;

if((flag_count % 2) == 1)
SampleGattAttributes.test_flag = 1;
else if((flag_count % 2) == 0)
SampleGattAttributes.test_flag = 2;
Toast.makeText(getApplicationContext(),SampleGattAttributes.test_flag+" value",Toast.LENGTH_SHORT).show();

final int Thdvalue;

if(SampleGattAttributes.test_flag == 1)
{
Log.d(TAG, "love THD_CONTROL_COMMAND 1 SET OK\r\n");
Thdvalue = 1;//START,
characteristic.setValue(Thdvalue,BluetoothGattCharacteristic.FORMAT_SINT8, 0);
mBluetoothGatt.writeCharacteristic(characteristic);
}
else if(SampleGattAttributes.test_flag == 2)
{
Log.d(TAG, "love THD_CONTROL_COMMAND 0 SET ==> OK\r\n");
Thdvalue = 0;//STOP,
flag_count = 0;
characteristic.setValue(Thdvalue,BluetoothGattCharacteristic.FORMAT_SINT8, 0);
mBluetoothGatt.writeCharacteristic(characteristic);

((DeviceControlActivity)DeviceControlActivity.mContext).saveExcel("");//love_1018
}

}

Parents
  • Hello ,

    Firstly thankful for your concern and rapid response,

    I need to explain the symptom for you, please refer below,

    1. the issue is app issue (android app), when using connect app of NRF, it is normally operated, I can receive data after first touching,

    but if using google ble example, and I modified to support indication case, it is not normally operated, I can receive data after secondly touching,

    so I wonder whether I can get the indication example or the method to solve it, 

    it is not related with sdk of nrf51822, and i updated my code including my modification, 

    touching means that we need to select service to operate indication service. 

    indicator ==> please refer the following link,

    devzone.nordicsemi.com/.../notification-indication-difference

    thankful for your support in advance,

Reply
  • Hello ,

    Firstly thankful for your concern and rapid response,

    I need to explain the symptom for you, please refer below,

    1. the issue is app issue (android app), when using connect app of NRF, it is normally operated, I can receive data after first touching,

    but if using google ble example, and I modified to support indication case, it is not normally operated, I can receive data after secondly touching,

    so I wonder whether I can get the indication example or the method to solve it, 

    it is not related with sdk of nrf51822, and i updated my code including my modification, 

    touching means that we need to select service to operate indication service. 

    indicator ==> please refer the following link,

    devzone.nordicsemi.com/.../notification-indication-difference

    thankful for your support in advance,

Children
  • Hi.

    I still find things a bit unclear, where did you find this code? Can you please link it?

    - Andreas

  • Hello ,

    So I see some errors in your code. You are using logical operator OR | instead of AND & in ifs. That may have some unwanted side effects.

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {  <- should be &

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) { <- the same

    I don't know if it helps, but that's something.

    Also, for performance, you may keep UUIDs as static final fields, not parse them from String every time.

  • Hi, ,

    Thankful for your information, I modified my code by your suggestion,

    please help to check whether it is right or not, 

    thankful for your support earnestly,

                            if ((charaProp & BluetoothGattCharacteristic.PROPERTY_READ) > 0) 
    //			if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_READ) == BluetoothGattCharacteristic.PROPERTY_READ )
    			{ 
                                // If there is an active notification on a characteristic, clear 
                                // it first so it doesn't update the data field on the user interface. 
                                if (mNotifyCharacteristic != null) 
                                { 
                                	Log.d(TAG, "love PROPERTY_READ setCharacteristicNotification");
                                    mBluetoothLeService.setCharacteristicNotification( 
                                            mNotifyCharacteristic, false); 
                                    mNotifyCharacteristic = null; 
                                }
                                
                                if (mIndicateCharacteristic != null) 
                                { 
                                	Log.d(TAG, "love PROPERTY_READ setCharacteristicIndication");
                                    mBluetoothLeService.setCharacteristicIndication( 
                                            mIndicateCharacteristic, false); 
                                    mIndicateCharacteristic = null; 
                                }
                                                            
                                mBluetoothLeService.readCharacteristic(characteristic); 
                            } 
                            
                            if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) 
    //                        if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) == BluetoothGattCharacteristic.PROPERTY_NOTIFY )
                            { 
    //                        	if (characteristic != null)
                            	{
    	                        	if (mNotifyCharacteristic == null)
    	                        	{
    		                            Log.d(TAG, "love PROPERTY_NOTIFY");
    		                            mNotifyCharacteristic = characteristic; 
    		                            mBluetoothLeService.setCharacteristicNotification(mNotifyCharacteristic, true); 
    		                            mBluetoothLeService.readCharacteristic(mNotifyCharacteristic);//love_1119
    		                        } 
    		                }
                            }
                             
                            if ((charaProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) 
    //			if ( (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) == BluetoothGattCharacteristic.PROPERTY_INDICATE )
    			{
    //				if (characteristic != null)
    				{
    					if (mIndicateCharacteristic == null)
    					{
    		                            Log.d(TAG, "love PROPERTY_INDICATE");                        	 
    		                            mIndicateCharacteristic = characteristic; 
    		                            mBluetoothLeService.setCharacteristicIndication(mIndicateCharacteristic, true);//love_1119 
    		                            mBluetoothLeService.readCharacteristic(mIndicateCharacteristic);//love_1119
    		                        } 
    		                }
                            }
    
    //love_1119 https://github.com/ghostyguo/BleUuidExplorer/blob/master/app/src/main/java/ghostysoft/bleuuidexplorer/CharacteristicReadWriteActivity.java
                                        
                            if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
                                Log.d(TAG, "love PROPERTY_WRITE");                        	 
    
    				if (UUID.fromString(SampleGattAttributes.THD_CONTROL_COMMAND).equals(characteristic.getUuid()))
    				{
    //	                            if (mNotifyCharacteristic != null) { 
    //	                                mBluetoothLeService.setCharacteristicNotification( 
    //	                                        mNotifyCharacteristic, false); 
    //	                                mNotifyCharacteristic = null; 
    //	                            } 
    //
    //	                            if (mIndicateCharacteristic != null) { 
    //	                                mBluetoothLeService.setCharacteristicIndication( 
    //	                                        mIndicateCharacteristic, false); 
    //	                                mIndicateCharacteristic = null; 
    //	                            } 
    	                            	                            
    				      	mBluetoothLeService.writeCharacteristic(characteristic);//love_1022  
    				}
                            } 
                            
                            
                            
    		final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
    		UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    		
    		

  • Operators look ok. Did you test it? Any change?

    Btw, we recommend using our BLE Library (https://github.com/NordicSemiconductor/Android-BLE-Library/), which handles all those things for you, giving a nice, easy to read API.

Related