iOS外围设备上的可复制广告数据
我即将开始开发一个iOS应用程序,它依赖于能够通过蓝牙LE广告发送少量数据,因此iOS设备是外围设备 在阅读以下内容时,我偶然发现: 也就是说,外设管理器只支持其中两个键 对象:CBAdvertisementDataLocalNameKey和 CBAdvertisementDataServiceUUIDsKey 这是否意味着我无法指定播发的数据是什么,并且本质上我仅限于设备名称常量iOS外围设备上的可复制广告数据,ios,bluetooth-lowenergy,Ios,Bluetooth Lowenergy,我即将开始开发一个iOS应用程序,它依赖于能够通过蓝牙LE广告发送少量数据,因此iOS设备是外围设备 在阅读以下内容时,我偶然发现: 也就是说,外设管理器只支持其中两个键 对象:CBAdvertisementDataLocalNameKey和 CBAdvertisementDataServiceUUIDsKey 这是否意味着我无法指定播发的数据是什么,并且本质上我仅限于设备名称常量 我的印象是,我能够自行决定发布大约28字节的数据。我不想启动一个大项目,如果它证明广告自定义数据是不可能的。答案是
我的印象是,我能够自行决定发布大约28字节的数据。我不想启动一个大项目,如果它证明广告自定义数据是不可能的。答案是-你可以!如果您进一步阅读文档,您将知道如何操作 一旦您阅读完本文和文档,我建议您逐行了解中央外围设备组合的实现 下面是如何通过外围触发器写入数据的一个小窍门外围: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;
}
}
}
答案是——你可以!如果您进一步阅读文档,您将知道如何操作 一旦您阅读完本文和文档,我建议您逐行了解中央外围设备组合的实现 下面是如何通过外围触发器写入数据的一个小窍门外围: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;
}
}
}
你的中心设备是什么?确保中央和外围设备中的UUID相同。它们将是相同的。但是我不清楚是否可以定制广告。文档中的什么地方?您的中心设备是什么?确保中央和外围设备中的UUID相同。它们将是相同的。但是,我不清楚是否可以定制广告。文档中的哪个位置?此代码通过特征发送数据,而不是作为广告的一部分。此代码通过特征发送数据,而不是作为广告的一部分