Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

obtain GATT response code in Android application

Hi All

On my NRF52 device I want to return my own error code after a mobile application (android) has written some data to my custom characteristic. For that I consider to use sd_ble_gatts_rw_authorize_reply() with codes range defined as BLE_GATT_STATUS_ATTERR_APP_BEGIN in ble_gatt.h SDK file. In the android application after few experiments I realized the result code is delivered in callback function with MSB truncated, so I corrected codes extraction as follow:

// on NRF side
void on_rw_authorize_request()
{
	ble_gatts_rw_authorize_reply_params_t reply;
	uint16_t my_result_code = process_message();
	if (my_result_code) reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_APP_BEGIN + my_result_code;
	sd_ble_gatts_rw_authorize_reply(p_gatts_evt->conn_handle, &reply);
}

// on android application side
static final int BLE_GATT_STATUS_ATTERR_APP_BEGIN = 0x80;
static final int BLE_GATT_STATUS_ATTERR_APP_END = 0x9f;
	
@Override public void onCharacteristicWrite(BluetoothGatt gatt,
                                          BluetoothGattCharacteristic characteristic,
                                          int status) {
	if (status != BluetoothGatt.GATT_SUCCESS && (status > BLE_GATT_STATUS_ATTERR_APP_END || status < BLE_GATT_STATUS_ATTERR_APP_BEGIN)) {
		// real bluetooth error
		return;
	}
	if (status > BLE_GATT_STATUS_ATTERR_APP_BEGIN)
		status -= BLE_GATT_STATUS_ATTERR_APP_BEGIN;
	// now status should be equal my_result_code
}

The problem that is these error codes coincide with those used by android system. At least one of important android code is 0x85 (produced every time I try to write data to just disconnected device):

// See https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/master/stack/include/gatt_api.h
static final int GATT_ERROR = 0x85;
static final int GATT_AUTH_FAIL = 0x89;

So my questions are:
1) is there some another way in android to get "true" GATT response code?
2) can I use another codes range in NRF side so it should be visible in android side? (If yes, what range? I need 10 values)

Thanks in advance!

Parents Reply Children
No Data
Related