You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
4.8 KiB
178 lines
4.8 KiB
1 year ago
|
//
|
||
|
// BluetoothTool.m
|
||
|
// BluetoothDemo
|
||
|
//
|
||
|
// Created by mac on 17/3/16.
|
||
|
// Copyright © 2017年 . All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "BluetoothTool.h"
|
||
|
#import "BLEConst.h"
|
||
|
#import "Bluetooth.h"
|
||
|
|
||
|
@implementation BluetoothTool
|
||
|
|
||
|
|
||
|
/**
|
||
|
CRC校验数据是否正确
|
||
|
*/
|
||
|
+ (BOOL)checkCRCWithData:(NSMutableData *)data {
|
||
|
// 取出CRC
|
||
|
// int crc = [BluetoothTool intFromHexSrting:[NSString stringWithFormat:@"%02x%02x",((Byte*)([data bytes]))[4],((Byte*)([data bytes]))[5]]];
|
||
|
Byte *bytes = (Byte *)[data bytes];
|
||
|
// 利用位移管道运算转整数 modify by fangtao 2018.12.01
|
||
|
int crc = (bytes[4] & 0xff) << 8 | (bytes[5] & 0xff);
|
||
|
// L2层数据
|
||
|
NSData *contentData = [data subdataWithRange:NSMakeRange(8, data.length - 8)];
|
||
|
// 重新根据contentData计算CRC
|
||
|
int dataCrc = 0xff;
|
||
|
int contentLength = (int)contentData.length;
|
||
|
Byte *contentBytes = (Byte *)[contentData bytes];
|
||
|
for (int i = 0; i <contentLength ; i++) {
|
||
|
dataCrc^= contentBytes[i] & 0xFF;
|
||
|
// NSLog(@"dataCrc == ++ %d",dataCrc);
|
||
|
for (int j = 0; j < 8; j++) {
|
||
|
if((dataCrc & 1) !=0){
|
||
|
dataCrc >>=1;
|
||
|
dataCrc^=0xB8;
|
||
|
}else{
|
||
|
dataCrc>>=1;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// 如果相等则正确
|
||
|
if (crc == dataCrc) {
|
||
|
return YES;
|
||
|
}
|
||
|
return NO;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 16进制字符串转10进制数字
|
||
|
*
|
||
|
* @param hexString 16进制字符串
|
||
|
*
|
||
|
* @return 十进制数据
|
||
|
*/
|
||
|
+ (int)intFromHexSrting:(NSString *)hexString {
|
||
|
|
||
|
NSLog(@"%@",hexString);
|
||
|
if (!hexString) {
|
||
|
return 0;
|
||
|
}
|
||
|
#warning 陈廷峰此处有几率闪退,出现请告知
|
||
|
NSString * temp10 = [NSString stringWithFormat:@"%lu",strtoul([hexString UTF8String],0,16)];
|
||
|
return [temp10 intValue];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
10进制转16进制字符串
|
||
|
|
||
|
@param hexNumber 10进制
|
||
|
|
||
|
@return 十六进制字符串
|
||
|
*/
|
||
|
+ (NSString *)stringWithHexNumber:(int)hexNumber {
|
||
|
// 固件升级情况下处理一个字节超过255的情况
|
||
|
if (hexNumber > 255) {
|
||
|
hexNumber = 0;
|
||
|
}
|
||
|
char hexChar[6];
|
||
|
sprintf(hexChar, "%02x", (int)hexNumber);
|
||
|
|
||
|
NSString *hexString = [NSString stringWithCString:hexChar encoding:NSUTF8StringEncoding];
|
||
|
|
||
|
return hexString;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/**
|
||
|
构造L1层第二个字节
|
||
|
|
||
|
@param err 1:对的 0:错的
|
||
|
@param ack 1:回复 0:命令
|
||
|
@param vision 版本
|
||
|
*/
|
||
|
+ (Byte)stitchWithErr:(int)err Ack:(int)ack Vision:(int)vision {
|
||
|
|
||
|
int a = 0 * pow(2, 7) + 0 * pow(2, 6) + err * pow(2, 5) + ack * pow(2, 4) + vision;
|
||
|
|
||
|
return a;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
倒序mac地址
|
||
|
*/
|
||
|
+ (NSString *)getInvertedOrderMacWithData:(NSData *)data {
|
||
|
|
||
|
NSString *macTemp = data.description;
|
||
|
|
||
|
NSMutableString *mutableMac = [[NSMutableString alloc]init];;
|
||
|
if (macTemp) {
|
||
|
|
||
|
macTemp = [macTemp stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||
|
macTemp = [macTemp stringByReplacingOccurrencesOfString:@"<" withString:@""];
|
||
|
macTemp = [macTemp stringByReplacingOccurrencesOfString:@">" withString:@""];
|
||
|
macTemp = [macTemp substringFromIndex:4];
|
||
|
|
||
|
for (int i = 0; i<6; i++) {
|
||
|
NSString *str = [macTemp substringWithRange:NSMakeRange(macTemp.length-2-i*2, 2)];
|
||
|
if (i==5) {
|
||
|
[mutableMac appendString:[NSString stringWithFormat:@"%@",str]];
|
||
|
|
||
|
}
|
||
|
else{
|
||
|
[mutableMac appendString:[NSString stringWithFormat:@"%@:",str]];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return mutableMac;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
正序mac地址
|
||
|
*/
|
||
|
+ (NSString *)getPositiveSequenceMacWithData:(NSData *)data {
|
||
|
NSString *dataStr = [self hexStringFromData:data];
|
||
|
NSMutableString *mutableMac = [[NSMutableString alloc]init];;
|
||
|
if (dataStr) {
|
||
|
dataStr = [dataStr stringByReplacingOccurrencesOfString:@" " withString:@""];
|
||
|
dataStr = [dataStr stringByReplacingOccurrencesOfString:@"<" withString:@""];
|
||
|
dataStr = [dataStr stringByReplacingOccurrencesOfString:@">" withString:@""];
|
||
|
for (int i = 0; i < 6; i++) {
|
||
|
NSString *str = [dataStr substringWithRange:NSMakeRange(i*2, 2)];
|
||
|
if (i == 5) {
|
||
|
[mutableMac appendString:[NSString stringWithFormat:@"%@",str]];
|
||
|
}else {
|
||
|
[mutableMac appendString:[NSString stringWithFormat:@"%@:",str]];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return [mutableMac uppercaseString];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
Data转16进制字符串
|
||
|
*/
|
||
|
+ (NSString *)hexStringFromData:(NSData*)data {
|
||
|
if (![data isKindOfClass:[NSData class]]) {
|
||
|
return nil;
|
||
|
}
|
||
|
if (data == nil) {
|
||
|
return nil;
|
||
|
}
|
||
|
NSMutableString *hexString = [NSMutableString string];
|
||
|
const unsigned char *p = [data bytes];
|
||
|
for (int i=0; i < [data length]; i++) {
|
||
|
[hexString appendFormat:@"%02x", *p++];
|
||
|
}
|
||
|
return hexString;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
@end
|