Ah yes, you were correct. I had the service UUID in there and no the characteristic. Oh well, not all is lost. I wrote some code to scan nearby BLE devices and then read all of the characteristics from a particular one. Maybe someone will find this useful.
// Set the MAC address of the BLE device we want to scan
#define BLE_PEER_ADDRESS {0x86, 0xB7, 0x62, 0xB7, 0x03, 0xF4}
// Turn on all logging
SerialLogHandler logHandler(LOG_LEVEL_ALL);
// Global variables
Vector<BleCharacteristic> ble_peer_all_chars;
BleCharacteristic ble_char;
char ble_char_uuid_string[40];
byte ble_char_value[250];
char ble_char_value_string[501];
BlePeerDevice ble_peer;
int ble_peer_found_at = -1;
const BleAddress ble_peer_address = BleAddress(BLE_PEER_ADDRESS);
BleScanResult ble_devices[30];
int ble_devices_seen = 0;
// Setup
void setup() {
// Start logging
Log.info("Starting...");
}
// Loop
void loop() {
// Start
Log.info("Scanning for nearby BLE devices...");
// Scan for nearby devices
ble_devices_seen = BLE.scan(ble_devices, 30);
Log.info(" - devices visible: %u", ble_devices_seen);
// List nearby devices
for (int x = 0; x < ble_devices_seen; x++) {
BleScanResult scanResult = ble_devices[x];
Log.info(" %s %idB", ble_devices[x].address.toString().c_str(), ble_devices[x].rssi);
}
// Look for the device we want to connect to.
for (int x = 0; x < ble_devices_seen; x++) {
if (strncmp(ble_devices[x].address.toString(), ble_peer_address.toString(), 17) == 0) {
ble_peer_found_at = x;
Log.info("Found matching device %s at position %i", ble_devices[ble_peer_found_at].address.toString().c_str(), ble_peer_found_at);
// Connect to this peer
Log.info(" - Connecting");
ble_peer = BLE.connect(ble_devices[ble_peer_found_at].address);
if (ble_peer.connected()) {
Log.info(" - Connection successful");
// Read all of the advertised characteristics
ble_peer_all_chars = ble_peer.discoverAllCharacteristics();
if (ble_peer_all_chars.size()) {
Log.info(" - Characteristics found: %u", ble_peer_all_chars.size());
// Read and display each characteristic
for (int y = 0; y < ble_peer_all_chars.size(); y++) {
// Display header for short characteristics
if (ble_peer_all_chars[y].UUID().type() == BleUuidType::SHORT) {
bin2hex(&ble_peer_all_chars[y].UUID().rawBytes()[0], &ble_char_uuid_string[0], 2, 1);
Log.info(" SHORT 0x%s", ble_char_uuid_string);
}
// Display header for long characteristics
if (ble_peer_all_chars[y].UUID().type() == BleUuidType::LONG) {
bin2hex(&ble_peer_all_chars[y].UUID().rawBytes()[12], &ble_char_uuid_string[0], 4, 1);
ble_char_uuid_string[8] = '-';
bin2hex(&ble_peer_all_chars[y].UUID().rawBytes()[10], &ble_char_uuid_string[9], 2, 1);
ble_char_uuid_string[13] = '-';
bin2hex(&ble_peer_all_chars[y].UUID().rawBytes()[8], &ble_char_uuid_string[14], 2, 1);
ble_char_uuid_string[18] = '-';
bin2hex(&ble_peer_all_chars[y].UUID().rawBytes()[6], &ble_char_uuid_string[19], 2, 1);
ble_char_uuid_string[23] = '-';
bin2hex(&ble_peer_all_chars[y].UUID().rawBytes()[0], &ble_char_uuid_string[24], 6, 1);
Log.info(" LONG %s", ble_char_uuid_string);
}
// Read the characteristic (only the first 20 bytes)
if (ble_peer.getCharacteristicByUUID(ble_char, ble_peer_all_chars[y].UUID())) {
ble_char.getValue(ble_char_value, 20);
bin2hex(ble_char_value, ble_char_value_string, 20, 0);
Log.info(" [ %s ]", ble_char_value_string);
} else {
Log.info(" [ Could not read ]");
}
}
}
// Disconnect from the peer
BLE.disconnect(ble_peer);
Log.info(" - Disconnected");
} else {
Log.info(" - Could not connect to the device");
}
}
}
// Let us know if a match was not found
if (ble_peer_found_at == -1) Log.info("No matching device was found");
// Wait 60 seconds and then try again
delay(60000);
}
// ==============================================================
// bin2hex
// ==============================================================
int bin2hex(const byte *input, char *output, int len, int reverse) {
// Check if we have data to convert
if (input == NULL || output == NULL || len == NULL) return 0;
// Step through each byte
for (int i = 0; i < len; i++) {
if (reverse == 1) {
output[i * 2] = "0123456789ABCDEF"[input[len - (i + 1)] >> 4];
output[i * 2 + 1] = "0123456789ABCDEF"[input[len - (i + 1)] & 0x0F];
} else {
output[i * 2] = "0123456789ABCDEF"[input[i] >> 4];
output[i * 2 + 1] = "0123456789ABCDEF"[input[i] & 0x0F];
}
}
output[len * 2] = '\0';
// Return
return 1;
}