[英]Core Bluetooth: published some character from peripheral side but only be able to receive Manufacturer Name String frrom central side
我有 2 部 iPhone,其中 1 部作为 ble 中心运行,另一部作为 ble 外围设备运行。 在我用 service uuid = '0000180a-0000-1000-8000-00805f9b34fb' 和特性 uuid = '0000180a-0000-1000-8000-00805f9b34fb'从外围端发布了一些特性之后,我从中央执行了扫描 ab34f 但是我从中央端得到的是代表“制造商名称字符串”和“制造商名称字符串”的无用特征,无法接收外围端发布的特征。
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 中心端的源代码:
- (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 时,它显示为“设备”信息服务”。
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
NSLog(@"Service %@", service.UUID);
}
如果我将服务 UUID 和特征 UUID 更改为官方 iOS BTLE Transfer 示例中使用的服务,即使 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 {
该adsData是广告数据所在的字典。
首先,试着找到这方面的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.