簡體   English   中英

BLE在iOS外設上的廣告數據

[英]BLE Advertising data on iOS peripheral

我將要開始開發一個iOS應用程序,它依賴於能夠通過Bluetooth LE廣告發送少量數據(因此iOS設備是外圍設備)。

在閱讀以下Apple文檔時,我偶然發現了以下內容:

也就是說,外圍設備管理器對象僅支持兩個鍵:CBAdvertisementDataLocalNameKey和CBAdvertisementDataServiceUUIDsKey。

這是否意味着我無法指定要發布的數據,並且實際上我只能使用設備名稱(常量)?

我的印象是,我可以自行決定廣告大約28個字節的數據。 如果事實證明無法發布自定義數據,我不想啟動一個大型項目。

答案是-可以! 如果您進一步閱讀文檔,您將知道該怎么做。

閱讀完文檔和文檔后,我建議您在此處逐行實施Central-Peripheral組合。

這是如何執行此操作的先睹為快(通過外圍設備觸發器在peripheral:didUpdateValueForCharacteristic:對應peripheral:didUpdateValueForCharacteristic:寫入數據外圍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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM