我试图在连接到所需的外围设备后发现服务,但我只能根据UUID找到不存在的服务。我的意思是;作为一种检查手段,我也在使用名为“Light Blue”的应用程序商店应用程序。 Light Blue只是一个识别所有外围设备,服务和特征的BLE探险家。我可以使用Light Blue看到所需的服务和特性,所以我知道它们存在并且Light Blue识别的那些是正确的。
以下是我的代码以及我如何识别所有服务,包含的服务和特征:
- (void)printAllChar:(CBPeripheral*)p {
printf("**************************************\n");
printf("Listing all services and characteristics\n");
CBService * s, * inc;
CBCharacteristic * c;
//Discover all services on peripheral
[p discoverServices:nil];
printf("%d services discovered\n", p.services.count);
for(int i = 0; i < p.services.count; i++){
//Traverse all services
s = [p.services objectAtIndex:i];
printf("Service %d: %s\n", i+1, s.UUID);
[p discoverIncludedServices:nil forService:s];
printf(" Included Services Discovered: %d\n", s.includedServices.count);
//Traverse all included services of each service
for(int k = 0; k < s.includedServices.count; k++){
inc = [s.includedServices objectAtIndex:k];
printf(" %d: %s\n", k+1, inc.UUID]);
}
[p discoverCharacteristics:nil forService:s];
printf("%d Characteristics discovered\n", s.characteristics.count);
//Traverse all characteristic of each service
for(int j = 0; j < s.characteristics.count; j++){
c = [s.characteristics objectAtIndex:j];
printf("Characteristic %d: %s\n", j+1, c.UUID);
}
}
}
^^^我知道打印函数实际上不会运行,因为s.UUID不是字符串变量,我删除了所有转换代码以使其更具可读性。
我认为值得一提的是回调函数didDiscoverServices永远不会被调用。
以下是上述代码的示例输出:
**************************************
Listing all services and characteristics
3 services discovered
Service 1: 02000000-0200-0000-60D0-E03B02000000
Included Services Discovered: 0
0 Characteristics discovered
Service 2: 02000000-0200-0000-18C0-F03B00005444
Included Services Discovered: 0
0 Characteristics discovered
Service 3: 00000000-1000-0000-B4CC-E03B82130001
Included Services Discovered: 0
0 Characteristics discovered
这些服务都不是我所期望的。我连接了正确的外围设备,但这些服务对我来说完全不了解。
我应该找到的服务ID如下:
//Service UUID
#define BLE_DEVICE_SERVICE_UUID "713D0000-503E-4C75-BA94-3148F18D941E"
//Characteristic UUID
#define BLE_DEVICE_VENDOR_NAME_UUID "713D0001-503E-4C75-BA94-3148F18D941E"
#define BLE_DEVICE_RX_UUID "713D0002-503E-4C75-BA94-3148F18D941E"
#define BLE_DEVICE_TX_UUID "713D0003-503E-4C75-BA94-3148F18D941E"
#define BLE_DEVICE_RESET_RX_UUID "713D0004-503E-4C75-BA94-3148F18D941E"
#define BLE_DEVICE_LIB_VERSION_UUID "713D0005-503E-4C75-BA94-3148F18D941E"
有谁知道发生了什么事?
如果相关,我使用的是RedBearLabs BLEMini。
答案 0 :(得分:0)
你真的应该尝试在回调方法中这样做。你说他们没有被叫 - 你把你的班级设置为CBPeripheralDelegate
并将外围设备的代表设置为self
吗?
(另外,另外:Objective-C有一个foreach
循环比手动设置一个带计数器的for
循环更方便):
for(CBService *s in peripheral.services){
NSLog(@"%@",s.uuid); //print the service uuid to console
}