核心蓝牙:从外围端发布了一些字符,但只能从中央端接收制造商名称字符串

Core Bluetooth: published some character from peripheral side but only be able to receive Manufacturer Name String frrom central side

我有 2 个 iPhone,其中 1 个 运行 作为 ble central,另一个 运行 作为 ble 外围设备。 在我使用服务 uuid = '0000180a-0000-1000-8000-00805f9b34fb' 和特征 uuid = '0000180a-0000-1000-8000-00805f9b34fb' 从外围端发布一些特征后,我从中央端执行了 ble 扫描。 但是我从central端得到的是代表'Manufacturer Name String'和'Manufacturer Name String'的无用特征,无法接收到从peripheral端发布的特征。

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

BLE外设端源码:

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"]] }]; 

BLE Central端的源代码:

- (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.
}

有时我什至没有收到任何特征。 有任何想法吗? 谢谢

更新:我不确定这个@"0000180a-0000-1000-8000-00805f9b34fb"是否被现有的iPhone内置BLE服务保留,但是当我打印出服务时来自 BLE Central 的 uuid 显示为 'Device Information Service'.

for (CBService *service in peripheral.services) {
    [peripheral discoverCharacteristics:nil forService:service];
    NSLog(@"Service %@", service.UUID);
}

如果我将服务UUID和特征UUID改成官方iOSBTLE Transfer示例中使用的UUID,即使didDiscoverServices调用成功,服务也将是空的。

#define TRANSFER_SERVICE_UUID           @"E20A39F4-73F5-4BC4-A12F-17D1AD07A961"
#define TRANSFER_CHARACTERISTIC_UUID    @"08590F7E-DB05-467E-8757-72F6FAEB13D4"

如果您成功扫描外围设备,您的委托将被调用

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI {

这个advertisementData是广告数据所在的字典。

首先,试着找到这方面的资料。