2014-10-12 50 views
1

基本上我有兩個應用程序,一個用於掃描藍牙設備,另一個用於廣告,我想要做的是獲取所有iOS設備的列表,在我的鄰近範圍內在掃描iOS設備的應用程序上。didDiscoverPeripheral在廣告另一個iOS設備後沒有被調用

這是我到目前爲止的代碼:

Scanning.m:

// Scan for all available CoreBluetooth LE devices 
    NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)}; 
    NSArray *services = @[[CBUUID UUIDWithString:@"1CC024D6-E413-4B56-993C-831CAF033366"]]; 

    [self.centralManager scanForPeripheralsWithServices:services options:scanOptions]; 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI 
{ 
    NSLog(@"RSSI: %d", [RSSI intValue]); 

} 

// method called whenever the device state changes. 
- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
{ 
    // Determine the state of the peripheral 
    if ([central state] == CBCentralManagerStatePoweredOff) { 
     NSLog(@"CoreBluetooth BLE hardware is powered off"); 
    } 
    else if ([central state] == CBCentralManagerStatePoweredOn) { 
     NSLog(@"CoreBluetooth BLE hardware is powered on and ready"); 
    } 
    else if ([central state] == CBCentralManagerStateUnauthorized) { 
     NSLog(@"CoreBluetooth BLE state is unauthorized"); 
    } 
    else if ([central state] == CBCentralManagerStateUnknown) { 
     NSLog(@"CoreBluetooth BLE state is unknown"); 
    } 
    else if ([central state] == CBCentralManagerStateUnsupported) { 
     NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform"); 
    } 
} 

Advertising.m:

self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; 
    [self listenForRelays]; 


    /** Required protocol method. A full app should take care of all the possible states, 
* but we're just waiting for to know when the CBPeripheralManager is ready 
*/ 
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral 
{ 

    if (peripheral.state == CBPeripheralManagerStatePoweredOn) 
    { 

     // We're in CBPeripheralManagerStatePoweredOn state... 
     NSLog(@"self.peripheralManager powered on."); 

     // ... so build our service. 

    } 
} 


-(void) listenForRelays { 

    if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn) 
    { 

     NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral", 
              CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"1CC024D6-E413-4B56-993C-831CAF033322"]]}; 

     // Start advertising over BLE 
     [self.peripheralManager startAdvertising:advertisingData]; 
    } 

} 

當我運行在不同的iPhone在不同的應用程序, 什麼都沒發生 !

這不會崩潰,但didDiscoverPeripheral不會被調用:(請幫助我,是的,我已經通過文件走了,但還是沒有好!((

歡呼

+1

直到您處於開機狀態之後才能開始掃描 - 在您的掃描應用程序中使用類似於您廣告應用程序中的代碼 – Paulw11 2014-10-12 00:29:32

+0

@ Paulw11 OMGGGGG !!!!非常感謝,你是一位天才!在過去的3個小時裏已經在這上面了,救命啊! – ipalibowhyte 2014-10-12 02:21:41

回答

4

不能啓動您的掃描,直到您處於開機狀態 - 在您的掃描應用程序中使用類似於您的廣告應用程序中的代碼。

在iOS 7中,在啓動之前開始掃描,在控制檯上發出警告,但它仍然有效。在iOS 8中它不起作用。

相關問題