image.png
<br />如上圖所示,藍牙接收到的信息中間部分變成了...,這是由于推送的 deviceToken 獲取到的格式發(fā)生變化<br />官方回復(fù) : <br />由于這種轉(zhuǎn)換快捷方式不再適用于您的目的散吵,您將需要解碼您自己接收的數(shù)據(jù)對象。<br />根據(jù)使用的語言蟆肆,可以使用類似于下面的代碼將數(shù)據(jù)轉(zhuǎn)換為字符串矾睦。
// Swift code snippet
let dataString = receivedData.map { String(format: "%02x", $0) }.joined()
//Objective C code snippet
const unsigned *dataBytes = [receivedData bytes];
dataString = [NSString stringWithFormat:@"<%08x %08x %08x %08x %08x %08x %08x %08x>",
ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
ntohl(tokenBytes[6]), ntohl(tokenBytes[7])]; … 這取決于你的數(shù)據(jù)有多長.
其實數(shù)據(jù)的長度通過之前的方式能夠得到,更改后的整體解析代碼如下:
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(nullable NSError *)error
{
NSString *dataStr = @"";
NSString *orStr = characteristic.value.description;
if ([characteristic.UUID.UUIDStringSafe isEqualToString:@"8888"]&& orStr)
{
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 13.0) {
const unsigned *dataBytes = [characteristic.value bytes];
NSString *str = [[orStr substringWithRange:NSMakeRange(1, orStr.length - 2)] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSUInteger datalength = [[[[[str componentsSeparatedByString:@","] objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1] intValue];
NSUInteger length = orStr.length;
NSLog(@"length = %lu",(unsigned long)length);
for(int i = 0;i<length;i++)
{
dataStr = [dataStr stringByAppendingString:[NSString stringWithFormat:@"%08x",
ntohl(dataBytes[i])]];
}
dataStr = [dataStr substringToIndex:datalength*2];
}
else
{
NSString *str = [orStr substringWithRange:NSMakeRange(1, orStr.length - 2)];
dataStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
}
NSLog(@"dataStr = %@",dataStr);
if(dataStr && ![dataStr isEqualToString:@""] && ![dataStr hasPrefix: @"8888"])
{
NSDictionary* userInfo = @{@"KeyBleNotifyString":dataStr};
[[NSNotificationCenter defaultCenter] postNotificationName:kSGBleManagerNotificationBleNotifyData
object:nil
userInfo:userInfo];
}
}
}
參考 :<br />iOS 13適配總結(jié)