I am trying to list only bluetooth devices in iOS with a certain service, but it is not showing up. I am using a HiLetgo ESP-WROOM-32 ESP32 ESP-32S Development Board and Arduino. The Arduino code is BLE_notify under ESP32 BLE Arduino in the ESP32 Dev Module of the examples. It defines a service:
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
...create it:
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
...and advertises it:
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
The Swift iOS code also defines the service:
let serviceCBUUID = CBUUID(string: "4fafc201-1fb5-459e-8fcc-c5c9c331914b")
...and I scan for the service:
self.centralManager?.scanForPeripherals(withServices: [serviceCBUUID])
but it does not appear. When I put nil
for the withServices
. It picks it up. I print the services:
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if !peripherals.contains(peripheral) {
var pname: String = peripheral.name ?? "unnamed device"
if (pname.contains("Test Device")) {
print("Peripheral name: \(peripheral.name)")
print("Peripheral Discovered: \(peripheral)")
print ("Advertisement Data : \(advertisementData)")
print ("Services : \(peripheral.services)")
}
self.peripherals.append(peripheral)
self.peripheralNames.append((peripheral.name ?? "unnamed device") + " [\(peripheral.identifier)]")
}
}
...and services is nil:
Peripheral name: Optional("Test Device")
Peripheral Discovered: <CBPeripheral: 0x283019ad0, identifier = C7D8059E-42EE-A549-9AF9-C2DC93FBD712, name = Test Device, mtu = 0, state = disconnected>
Advertisement Data : ["kCBAdvDataRxSecondaryPHY": 0, "kCBAdvDataRxPrimaryPHY": 129, "kCBAdvDataTxPowerLevel": 3, "kCBAdvDataIsConnectable": 1, "kCBAdvDataTimestamp": 694870916.362642, "kCBAdvDataLocalName": Test Device]
Services : nil
I can connect to the peripheral and get the service.
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
print("didDiscoverServices")
guard let services = peripheral.services else {
return
}
print("services count: \(services.count)")
for service in services {
print("service: \(service.description)")
}
discoverCharacteristics(peripheral: peripheral)
}
and I see the value:
service: <CBService: 0x281378340, isPrimary = YES, UUID = 4FAFC201-1FB5-459E-8FCC-C5C9C331914B>
I assume the problem is in the Arduino end because CoreBluetooth is identifying the services as nil, but I'm not really sure. Has anybody gotten this to work?