gpt4 book ai didi

ios - iOS 外设上的 BLE 广告数据

转载 作者:行者123 更新时间:2023-12-01 17:06:01 26 4
gpt4 key购买 nike

我即将开始开发一个 iOS 应用程序,它依赖于能够通过蓝牙 LE 广告发送小块数据(因此 iOS 设备是外围设备)。

阅读以下内容 Apple documentation我偶然发现了以下内容:

That said, only two of the keys are supported for peripheral manager objects: CBAdvertisementDataLocalNameKey and CBAdvertisementDataServiceUUIDsKey.

这是否意味着我无法指定公布的数据,并且我基本上受限于设备名称(常量)?

我的印象是我能够自行决定宣传大约 28 字节的数据。如果事实证明广告自定义数据是不可能的,我不想开始一个大项目。

最佳答案

答案是——可以!如果您进一步阅读文档,您就会知道如何去做。

一旦你读完 thisthis文档,我建议您逐行执行 Central-Peripheral 组合 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;
}
}
}

关于ios - iOS 外设上的 BLE 广告数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com