2015-12-27 49 views
0

我正在嘗試編寫一個玩具應用程序來加速BLE。我有一個我想掃描其服務的外圍設備(它是一個Tile)。但是,我不知道它提供的是什麼服務(並且我在產品的技術規範中沒有看到這一點)。所以,我從藍牙規格中獲得了list of services,並試圖掃描所有這些規格。我只是假設這份清單是詳盡的,因爲它沒有說這種或那種方式。BLE掃描未知服務

這裏是我的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad];  

    // Scan for all available CoreBluetooth LE devices 
    _services = @[[CBUUID UUIDWithString:@"1811"], 
        [CBUUID UUIDWithString:@"1815"], 
        [CBUUID UUIDWithString:@"180F"], 
        [CBUUID UUIDWithString:@"1810"], 
        [CBUUID UUIDWithString:@"181B"], 
        [CBUUID UUIDWithString:@"181E"], 
        [CBUUID UUIDWithString:@"181F"], 
        [CBUUID UUIDWithString:@"1805"], 
        [CBUUID UUIDWithString:@"1818"], 
        [CBUUID UUIDWithString:@"1816"], 
        [CBUUID UUIDWithString:@"180A"], 
        [CBUUID UUIDWithString:@"181A"], 
        [CBUUID UUIDWithString:@"1800"], 
        [CBUUID UUIDWithString:@"1801"], 
        [CBUUID UUIDWithString:@"1808"], 
        [CBUUID UUIDWithString:@"1809"], 
        [CBUUID UUIDWithString:@"180D"], 
        [CBUUID UUIDWithString:@"1823"], 
        [CBUUID UUIDWithString:@"1812"], 
        [CBUUID UUIDWithString:@"1802"], 
        [CBUUID UUIDWithString:@"1821"], 
        [CBUUID UUIDWithString:@"1820"], 
        [CBUUID UUIDWithString:@"1803"], 
        [CBUUID UUIDWithString:@"1819"], 
        [CBUUID UUIDWithString:@"1807"], 
        [CBUUID UUIDWithString:@"1825"], 
        [CBUUID UUIDWithString:@"180E"], 
        [CBUUID UUIDWithString:@"1822"], 
        [CBUUID UUIDWithString:@"1806"], 
        [CBUUID UUIDWithString:@"1814"], 
        [CBUUID UUIDWithString:@"1813"], 
        [CBUUID UUIDWithString:@"1824"], 
        [CBUUID UUIDWithString:@"1804"], 
        [CBUUID UUIDWithString:@"181C"], 
        [CBUUID UUIDWithString:@"181D"]]; 

    CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 
    [centralManager scanForPeripheralsWithServices:_services options:nil]; 
    self.centralManager = centralManager; 
} 

- (IBAction)scanForDevices:(UIButton *)sender { 
    [_centralManager scanForPeripheralsWithServices:_services options:nil]; 
} 

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. 
// This contains most of the information there is to know about a BLE peripheral. 
- (void)centralManager:(CBCentralManager *)central 
didDiscoverPeripheral:(CBPeripheral *)peripheral 
    advertisementData:(NSDictionary *)advertisementData 
        RSSI:(NSNumber *)RSSI 
{ 
    NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey]; 
    if ([localName length] > 0) { 
     NSLog(@"Found the heart rate monitor: %@", localName); 
     [self.centralManager stopScan]; 
     _hRMPeripheral = peripheral; 
     peripheral.delegate = self; 
     [self.centralManager connectPeripheral:peripheral options:nil]; 
    } 
} 

// 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"); 
    } 
} 

中僅有的日誌輸出,我得到的是這樣的:

CoreBluetooth BLE硬件上電,並準備

否則,應用程序運行良好。沒有錯誤或警告。但它永遠找不到外圍設備。

我在做什麼錯?是否有我應該掃描的不同服務?

謝謝!

EDIT
使用下面的建議,我使用LightBlue應用掃描的外圍。現在我知道,有一個使用此UUID所通知的服務:

84100CBF-D622-8FF7-C8B8-300FDB52B92D

我改變了我的陣列看起來像這樣:

_services = @[[CBUUID UUIDWithString:@"84100CBF-D622-8FF7-C8B8-300FDB52B92D"]]; 

但仍然沒有得到日誌中的輸出。我也嘗試通過nil但仍然沒有。

+2

只需提供'nil'作爲服務來掃描並記錄您從App Store中接收或使用應用程序(如LightBlue)的內容。 – Paulw11

+0

謝謝。我以前曾嘗試使用'nil',但也沒有產生任何結果。 LightBlue是一個好主意。我現在知道廣告數據包的UUID,我把它插入到我的'NSArray'中。但是,仍然沒有日誌輸出。我的代碼在某處存在問題。 – Alex

回答

1

正如指出的Paulw,只是發送nil,而不是一個服務列表,你會得到在範圍內的所有外圍設備,無論服務(S),他們做廣告。

需要注意的是:

  • 這隻會在前臺工作。背景掃描需要你提供服務掃描
  • 在你的方法,它不可能列出所有服務。除了列出的簡短UUID服務(實際上是完整UUID的快捷方式)之外,設備可以通過任何「完整」UUID通告服務。有2^128個可能的值...
+0

謝謝你。我沒有意識到UUID實際上是服務號碼。我並不感到驚訝的是,有很多比藍牙網站上列出的更多。我嘗試了'nil',但什麼都沒有,所以我的代碼一定有問題。我還使用LightBlue來獲取廣告服務的確切UUID,並將其放入我的數組中。依然沒有。你看到代碼有什麼問題嗎?再次感謝! – Alex

+0

只有一次開始掃描'centralManagerDidUpdateState:'被調用且狀態爲'CBCentralManagerStatePoweredOn'。 – jcaron

+0

是的,我是,但現在我意識到,沒有改變按鈕中的掃描指令,以包含'nil'作爲參數(我只改變了'viewDidLoad')。一旦我改變了,它就出現了。非常感謝您的幫助! – Alex