<br />如上图所示,蓝牙接收到的信息中间部分变成了...,这是由于推送的 deviceToken 获取到的格式发生变化<br />官方回复 : <br />由于这种转换快捷方式不再适用于您的目的,您将需要解码您自己接收的数据对象。<br />根据使用的语言,可以使用类似于下面的代码将数据转换为字符串。
// 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])]; … 这取决于你的数据有多长.
其实数据的长度通过之前的方式能够得到,更改后的整体解析代码如下:
- (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适配总结