我有一个芯片,工作在蓝牙和传输它的UUID。我需要从IOS应用程序中发现它,并获取它的UUID。我知道如何在两个IOS设备之间建立连接,但不知道如何与另一个芯片建立连接。
谢谢!
发布于 2012-12-27 00:34:29
你应该看看苹果的CoreBluetooth Temperature Example。
首先,您将使用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");
}
}我希望这个过程更容易解释。解决这个问题的最好方法是下载苹果的温度示例,并在你的iPhone或iPad上运行它(它在模拟器中不起作用)。即使你可能没有广播温度,它也会找到你的蓝牙LE设备,并解析它正在广播的服务。在该项目的LeDiscovery.m文件中放置断点应该会向您显示从iOS应用程序中发现蓝牙LE芯片所需的步骤。
希望这能有所帮助!
https://stackoverflow.com/questions/13763324
复制相似问题