1

我有一个芯片,在 LE 蓝牙上工作并传输它的 UUID。我需要从 IOS 应用程序中发现它并获取它的 UUID。我知道如何在两个 IOS 设备之间建立连接,但不知道如何与另一个芯片建立连接。

谢谢!

4

1 回答 1

1

您应该查看 Apple 的CoreBluetooth 温度示例

首先,您将使用 CBCentralManager 查找具有您正在寻找的 UUID 的可用蓝牙外设。这是一个漫长的过程,需要委托,我不能轻易地给你代码片段来做到这一点。它看起来像这样。

.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]];

}

既然您已经告诉外围设备宣传它所拥有的服务,那么您将拥有一个用于解析这些服务的委托。

- (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 的温度示例并在你的 iPhone 或 iPad 上运行它(它在模拟器中不起作用)。即使您可能没有广播温度,它也会找到您的蓝牙 LE 设备并解析它正在广播的服务。在该项目的 LeDiscovery.m 文件中放置断点应该会显示从 iOS 应用程序发现蓝牙 LE 芯片所需的步骤。

希望这可以帮助!

于 2012-12-26T16:34:29.243 回答