2015-10-20 68 views
0

我即將開始開發一款iOS應用程序,它依賴於能夠通過藍牙LE廣告發送小塊數據(因此iOS設備是外設)。BLE iOS外圍設備上的廣告數據

閱讀中,我偶然發現了以下以下Apple documentation

這就是說,只有兩個按鍵都支持外圍經理 對象:CBAdvertisementDataLocalNameKey和 CBAdvertisementDataServiceUUIDsKey。

這是否意味着我無法指定通告什麼數據,並且我基本上只限於設備名稱(常量)?

我的印象是,我可以根據自己的判斷,廣告大約28個字節的數據。如果廣告定製數據不可行,我不想開始一個重大項目。

+0

什麼是你的中央設備?確保中央和外圍設備的UUID相同。 –

+0

他們將是一樣的。不過,我不清楚自定義廣告是否可行。 –

+0

在文檔中的位置? –

回答

0

答案是 - 你可以!如果你通過文檔進一步閱讀,你會知道如何去做。

一旦你讀完thisthis文檔,我會建議你通過中央外設組合here逐行執行。

下面是如何(通過中央對手外圍觸發peripheral:didUpdateValueForCharacteristic:寫入數據)做先睹爲快:

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic { 

    _dataToSend = [_textView.text dataUsingEncoding:NSUTF8StringEncoding]; 

    _sendDataIndex = 0; 

    [self sendData]; 
} 

- (void)sendData { 

    static BOOL sendingEOM = NO; 

    // end of message? 
    if (sendingEOM) { 
     BOOL didSend = [self.peripheralManager updateValue:[@"EOM" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil]; 

     if (didSend) { 
      // It did, so mark it as sent 
      sendingEOM = NO; 
     } 
     // didn't send, so we'll exit and wait for peripheralManagerIsReadyToUpdateSubscribers to call sendData again 
     return; 
    } 

    // We're sending data 
    // Is there any left to send? 
    if (self.sendDataIndex >= self.dataToSend.length) { 
     // No data left. Do nothing 
     return; 
    } 

    // There's data left, so send until the callback fails, or we're done. 
    BOOL didSend = YES; 

    while (didSend) { 
     // Work out how big it should be 
     NSInteger amountToSend = self.dataToSend.length - self.sendDataIndex; 

     // Can't be longer than 20 bytes 
     if (amountToSend > NOTIFY_MTU) amountToSend = NOTIFY_MTU; 

     // Copy out the data we want 
     NSData *chunk = [NSData dataWithBytes:self.dataToSend.bytes+self.sendDataIndex length:amountToSend]; 

     didSend = [self.peripheralManager updateValue:chunk forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil]; 

     // If it didn't work, drop out and wait for the callback 
     if (!didSend) { 
      return; 
     } 

     NSString *stringFromData = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding]; 
     NSLog(@"Sent: %@", stringFromData); 

     // It did send, so update our index 
     self.sendDataIndex += amountToSend; 

     // Was it the last one? 
     if (self.sendDataIndex >= self.dataToSend.length) { 

      // Set this so if the send fails, we'll send it next time 
      sendingEOM = YES; 

      BOOL eomSent = [self.peripheralManager updateValue:[@"EOM" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:self.transferCharacteristic onSubscribedCentrals:nil]; 

      if (eomSent) { 
       // It sent, we're all done 
       sendingEOM = NO; 
       NSLog(@"Sent: EOM"); 
      } 

      return; 
     } 
    } 
} 
+0

此代碼通過特徵發送數據,而不是廣告的一部分 – Paulw11