Black Lives Matter - Action and Equality.
0

BLE Service & Characteristic on Adafruit Bluefruit not visib
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

BLE Service & Characteristic on Adafruit Bluefruit not visib

by benberry on Sun Aug 25, 2019 11:30 pm

I'm trying to read and write data from an Adafruit Bluefruit Feather 32u4 using an Android app.

As a starting place, I'm using the health thermometer demo that creates a custom GATT service and characteristic, then writes to the characteristic once per second.

The relevant part of that code sample:

Code: Select all | TOGGLE FULL SIZE
/* Add the Heart Rate Service definition */
  /* Service ID should be 1 */
  Serial.println(F("Adding the Health Thermometer Service definition (UUID = 0x1809): "));
  htsServiceId = gatt.addService(0x1809);
  if (htsServiceId == 0) {
    error(F("Could not add Thermometer service"));
  }
 
  /* Add the Temperature Measurement characteristic which is composed of
   * 1 byte flags + 4 float */
  /* Chars ID for Measurement should be 1 */
  Serial.println(F("Adding the Temperature Measurement characteristic (UUID = 0x2A1C): "));
  htsMeasureCharId = gatt.addCharacteristic(0x2A1C, GATT_CHARS_PROPERTIES_INDICATE, 5, 5, BLE_DATATYPE_BYTEARRAY);
  if (htsMeasureCharId == 0) {
    error(F("Could not add Temperature characteristic"));
  }


Running the sample on my 32u4 Feather and watching the serial output, everything appears to be good. Adding an invocation of `AT+GATTLIST` to the code sample prints this to the Serial output:


Code: Select all | TOGGLE FULL SIZE
AT+GATTLIST

<- ID=01,UUID=0x1809
  ID=01,UUID=0x2A1C,PROPERTIES=0x20,MIN_LEN=5,MAX_LEN=5,DATATYPE=2,VALUE=00-00-00-00-00


So the service is being created, the characteristic is being created, and then the `loop()` of the sample goes on to write every second to that characteristic.

However, from my Android app, I just can't seem to get a handle to that GATT service or characteristic. I can scan for BLE devices just fine, find the Feather, connect to it, and run `discoverServices` and then once that's done, try to get the custom service being created, but it's not there. Relevant snippet of code:


Code: Select all | TOGGLE FULL SIZE
    fun connect(device: BluetoothDevice) {
        /** Step 1: Connect to the device **/
        device.connectGatt(context, true, gattCallback)
    }

    fun deviceConnected(gatt: BluetoothGatt) {
        /** Step 2: Store connection to GATT interface, discover services **/
        this.gatt = gatt
        gatt.discoverServices()
    }

    private val gattCallback = object : BluetoothGattCallback() {
        override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) {
            when (newState) {
                BluetoothGatt.STATE_CONNECTED -> {
                    gatt?.let {
                        deviceConnected(gatt)
                    }
                }
            }
        }

        override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) {
            /** Step 3: Do things with services **/

            Log.d(TAG, gatt?.getService(expandUuid(0x1809))?.uuid.toString())
            Log.d(TAG, gatt?.getService(expandUuid(0x180A))?.uuid.toString())
        }
    }


The log line to get the `0x180A` service prints the UUID of that service (the standard Device Information service), but the line to get our custom `0x1809` service just prints null.

The feather thinks it's creating the service, but Android just doesn't see it. I'm not sure how to troubleshoot this any further. Any suggestions would be helpful.

benberry
 
Posts: 2
Joined: Sun Aug 25, 2019 11:26 pm

Re: BLE Service & Characteristic on Adafruit Bluefruit not v

by benberry on Sun Sep 01, 2019 2:59 pm

This issue is still outstanding. Has the sample code for the 32u4 Bluefruit just been deprecated?

benberry
 
Posts: 2
Joined: Sun Aug 25, 2019 11:26 pm

Re: BLE Service & Characteristic on Adafruit Bluefruit not v

by paulfo on Sun Oct 20, 2019 1:52 pm

Hi I found the health thermometer code to be wrong when publishing the formatted temperature value. I used an mBed example's code replacing the use of IEEE11073float with:

/**
* @brief A very quick conversion between a float temperature and 11073-20601 FLOAT-Type.
* @param temperature The temperature as a float.
* @return The temperature in 11073-20601 FLOAT-Type format.
*/
uint32_t quick_ieee11073_from_float(float temperature) {
uint8_t exponent = 0xFE; //Exponent is -2
uint32_t mantissa = (uint32_t)(temperature * 100);
return (((uint32_t)exponent) << 24) | mantissa;
}

And using it in the script as:

//float2IEEE11073(double(temp), temp_measurement);
uint32_t temp_measurement = quick_ieee11073_from_float((double)temp);

//for (byte xx = 0; xx < 4; xx++) BLE_measurement[xx+1] = temp_measurement[xx];
memcpy(&BLE_measurement[1], &temp_measurement, sizeof(float));
gatt.setChar(htsMeasureCharId, BLE_measurement, sizeof(BLE_measurement));


I can see the characteristic and the correct values published via the Nordic Semiconductor Connect app on my Android phone.

The issue I have is the Web Bluetooth doesn't decode the characteristic properties correctly, in that the sample for displaying characteristics stays put with this output:

Requesting Bluetooth Device...
Connecting to GATT Server...
Getting Service...
Getting Characteristic...
> Characteristic UUID: 00002a1c-0000-1000-8000-00805f9b34fb
> Broadcast: false
> Read: false
> Write w/o response: false
> Write: false
> Notify: false
> Indicate: true
> Signed Write: false
> Queued Write: false
> Writable Auxiliaries: false

Even when I've added or just replaced the GATT_CHARS_PROPERTIES_INDICATE with GATT_CHARS_PROPERTIES_READ or indeed both with NOTIFY. The Nordic app shows these properties correctly. But Web Bluetooth doesn't and refuses to read a value from the service, saying NotSupported.

This doesn't directly help you, other than I can say the modified health_thermometer sketch on the 32u4 BlueFruit works against the Nordic app.

Cheers
Paul

paulfo
 
Posts: 9
Joined: Wed Oct 19, 2016 8:53 am

Please be positive and constructive with your questions and comments.