2012-12-07 47 views
1

我有一個芯片,在LE藍牙上工作併發送它的UUID。 我需要從iOS應用程序中發現它並獲取它的UUID。 我知道如何建立兩個IOS設備之間的連接,但不能與其他芯片建立連接。如何從LE藍牙芯片獲取UUID?

謝謝!

回答

1

你應該看看Apple的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"); 
     } 
} 

我希望這個過程更容易解​​釋。解決這個問題的最好方法是下載Apple的溫度範例並在iPhone或iPad上運行它(它在模擬器中不起作用)。即使您可能沒有播放溫度,它也會找到您的藍牙LE設備,並通過它正在播放的服務進行解析。在該項目的LeDiscovery.m文件中放置斷點應顯示從iOS應用程序中發現您的Bluetooth LE芯片所需的步驟。

希望這會有所幫助!