I have 2 iPhones with 1 running as a ble central and the other one running as a ble peripheral. After I published some characteristics from the peripheral side with service uuid = '0000180a-0000-1000-8000-00805f9b34fb' and characteristic uuid = '0000180a-0000-1000-8000-00805f9b34fb', I performed a ble scan from central side. But what I got from central side are useless characteristics representing 'Manufacturer Name String' and 'Manufacturer Name String', not able to receive the characteristic published from peripheral side.
2017-10-24 07:20:18.899260+0800 App[275:9299] Available characteristic UUID Manufacturer Name String
2017-10-24 07:20:18.899762+0800 App[275:9299] Available characteristic UUID Model Number String
2017-10-24 07:20:18.927153+0800 App[275:9301] getUuidFromCharacteristic: return characteristic value = Apple Inc.
2017-10-24 07:20:18.958133+0800 App[275:9301] getUuidFromCharacteristic: return characteristic value = iPhone7,2
The source code from BLE peripheral side:
CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"00002A2C-0000-1000-8000-00805f9b34fb"]
properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyRead
value:[chaValue dataUsingEncoding:NSUTF8StringEncoding]
permissions:CBAttributePermissionsReadable];
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:[self.bleUuidObj getServiceUuid]]
primary:YES];
[self.peripheralManager addService:transferService];
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:@"0000180a-0000-1000-8000-00805f9b34fb"]] }];
Source code from BLE Central side:
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Peripheral Connected");
[self.centralManager stopScan];
// Make sure we get the discovery callbacks
peripheral.delegate = self;
// Search only for services that match our UUID
[peripheral discoverServices:@[[CBUUID UUIDWithString:@"0000180a-0000-1000-8000-00805f9b34fb"]]];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
[self cleanup];
return;
}
// Discover the characteristic we want...
// Loop through the newly filled peripheral.services array, just in case there's more than one.
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// Deal with errors (if any)
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
[self cleanup];
return;
}
NSLog(@"Service discovered successfully: %@", service.UUID);
if ([service.UUID isEqual:[CBUUID UUIDWithString:@"0000180a-0000-1000-8000-00805f9b34fb"]]) {
NSLog(@"The UUID of the discovered service is %@", @"0000180a-0000-1000-8000-00805f9b34fb");
// Again, we loop through the array, just in case.
if (service.characteristics == nil) {
NSLog(@"service.characteristics is nil");
}
NSLog(@"Size of the characteristics array %lu", [service.characteristics count]);
for (CBCharacteristic *characteristic in service.characteristics) {
NSLog(@"Available characteristic UUID %@", characteristic.UUID);
NSLog(@"Reading desc value from characteristic with UUID = %@", characteristic.UUID);
[peripheral readValueForCharacteristic:characteristic];
}
}
// Once this is complete, we just need to wait for the data to come in.
}
Some times I don't even receive any characteristic at all. Any ideas? Thanks
Update: I'm not sure if this @"0000180a-0000-1000-8000-00805f9b34fb" is reserved by existing iPhone built-in BLE service or not, but when I printed out the service uuid from BLE Central side it reads 'Device Information Service'.
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
NSLog(@"Service %@", service.UUID);
}
If I change the service UUID and characteristic UUID to the ones used in the official iOS BTLE Transfer sample, the service will be empty at all even if didDiscoverServices is called successfully.
iOS peripheral service still empty after discovering
#define TRANSFER_SERVICE_UUID @"E20A39F4-73F5-4BC4-A12F-17D1AD07A961"
#define TRANSFER_CHARACTERISTIC_UUID @"08590F7E-DB05-467E-8757-72F6FAEB13D4"
If you success to scan the peripherals, your delegate get called
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {
This advertisementData is dictionary where the advertisement data located.
First of all, try to find the data on this.