我有一个芯片,在 LE 蓝牙上工作并传输它的 UUID。 我需要从 IOS 应用程序中发现它并获取它的 UUID。 我知道如何在两个 IOS 设备之间建立连接,但不知道如何与另一个芯片建立连接。
谢谢!
最佳答案
你应该看看 Apple 的 CoreBluetooth Temperature Example .
首先,您将使用 CBCentralManager 查找具有您要查找的 UUID 的可用蓝牙外围设备。这是一个需要委托(delegate)的漫长过程,我无法轻松地为您提供代码片段来执行此操作。它看起来像这样。
.h file will have these. Remember to add the CoreBluetooth Framework.
#import <CoreBluetooth/CoreBluetooth.h>
CBCentralManager * manager;
CBPeripheral * connected_peripheral;
(相应地更改您的 UUID):
NSArray * services=[NSArray arrayWithObjects:
[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"],
nil
];
[manager scanForPeripheralsWithServices:services options: [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]];
[manager connectPeripheral:peripheral options:nil];
从那里您知道您拥有正确的外围设备,但您仍然需要选择它并阻止 CBManager 继续扫描新设备。
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[manager stopScan];
NSArray *keys = [NSArray arrayWithObjects:
[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"],
nil];
NSArray *objects = [NSArray arrayWithObjects:
@"My UUID to find",
nil];
serviceNames = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[connected_peripheral setDelegate:self];
[connected_peripheral discoverServices:[serviceNames allKeys]];
}
现在您已经告诉您的外设去宣传它有哪些服务,您将有一个委托(delegate)来解析这些服务。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
CBService *bluetoothService;
for (bluetoothService in connected_peripheral.services) {
if([bluetoothService.UUID isEqual:[CBUUID UUIDWithString:@"0bd51666-e7cb-469b-8e4d-2742f1ba77cc"]])
{
NSLog(@"This is my bluetooth Service to Connect to");
}
}
我希望这个过程更容易解释。解决这个问题的最佳方法是下载 Apple 的 Temperature Example 并在您的 iPhone 或 iPad 上运行它(它不能在模拟器中运行)。即使您可能没有广播温度,它也会找到您的蓝牙 LE 设备并解析它广播的服务。在该项目的 LeDiscovery.m 文件中放置断点应该会向您展示从 iOS 应用发现蓝牙 LE 芯片所需的步骤。
希望这对您有所帮助!
关于ios - 如何从 LE 蓝牙芯片获取 UUID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763324/