假設咱們要向藍牙發送0x1B9901這條數據html
Byte value[3]={0};
value[0]=0x1B;
value[1]=0x99;
value[2]=0x01;
NSData * data = [NSData dataWithBytes:&value length:sizeof(value)];
//發送數據
[self.peripheral writeValue:data forCharacteristic:self.write type:CBCharacteristicWriteWithoutResponse];
複製代碼
優勢:這種方法比較簡單,沒有進行轉換,直接一個字節一個字節的拼裝好發送出去。git
缺點:當發送數據比較長時會很麻煩,並且不易更改。github
- (NSData *)hexToBytes:(NSString *)str{
NSMutableData* data = [NSMutableData data];
int idx;
for (idx = 0; idx+2 <= str.length; idx+=2) {
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [str substringWithRange:range];
NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[data appendBytes:&intValue length:1];
}
return data;
}
//發送數據
[self.peripheral writeValue:[self hexToBytes:@"1B9901"] forCharacteristic:self.write type:CBCharacteristicWriteWithoutResponse];
複製代碼
接下來探討下發送的數據須要計算的狀況。 最經常使用的發送數據須要計算的場景是求__校驗和(CHECKSUM)__。這個根據硬件廠商來定,常見的求校驗和的規則有:objective-c
若是我要發送帶上校驗和的0x1B9901,方法就是:app
- (NSData *)getCheckSum:(NSString *)byteStr{
int length = (int)byteStr.length/2;
NSData *data = [self hexToBytes:byteStr];
Byte *bytes = (unsigned char *)[data bytes];
Byte sum = 0;
for (int i = 0; i<length; i++) {
sum += bytes[i];
}
int sumT = sum;
int at = 256 - sumT;
printf("校驗和:%d\n",at);
if (at == 256) {
at = 0;
}
NSString *str = [NSString stringWithFormat:@"%@%@",byteStr,[self ToHex:at]];
return [self hexToBytes:str];
}
//將十進制轉化爲十六進制
- (NSString *)ToHex:(int)tmpid{
NSString *nLetterValue;
NSString *str =@"";
int ttmpig;
for (int i = 0; i<9; i++) {
ttmpig=tmpid%16;
tmpid=tmpid/16;
switch (ttmpig){
case 10:
nLetterValue =@"A";break;
case 11:
nLetterValue =@"B";break;
case 12:
nLetterValue =@"C";break;
case 13:
nLetterValue =@"D";break;
case 14:
nLetterValue =@"E";break;
case 15:
nLetterValue =@"F";break;
default:
nLetterValue = [NSString stringWithFormat:@"%u",ttmpig];
}
str = [nLetterValue stringByAppendingString:str];
if (tmpid == 0) {
break;
}
}
//不夠一個字節湊0
if(str.length == 1){
return [NSString stringWithFormat:@"0%@",str];
}else{
return str;
}
}
//發送數據
NSData *data = [self getCheckSum:@"1B9901"];//data=<1b99014b>
[self.peripheral writeValue:data forCharacteristic:self.write type:CBCharacteristicWriteWithoutResponse];
複製代碼
這種是比較麻煩的,舉個栗子:在傳輸某條信息時,我想把時間放進去,不能用時間戳,還要節省空間,這樣就出現了一種新的方式存儲時間。 這裏再補充一些C語言知識:框架
存儲時間的條件是:函數
這樣直觀的解決辦法就是分別取出如今時間的年月日時分星期,先轉成2進制,再轉成16進制發出去。固然你這麼寫進去,讀的時候就要把16進制數據先轉成2進制再轉成10進制顯示。咱們就按這個簡單粗暴的思路來,準備工做以下:ui
//十進制轉二進制
- (NSString *)toBinarySystemWithDecimalSystem:(int)num length:(int)length
{
int remainder = 0; //餘數
int divisor = 0; //除數
NSString * prepare = @"";
while (true){
remainder = num%2;
divisor = num/2;
num = divisor;
prepare = [prepare stringByAppendingFormat:@"%d",remainder];
if (divisor == 0){
break;
}
}
//倒序輸出
NSString * result = @"";
for (int i = length -1; i >= 0; i --){
if (i <= prepare.length - 1) {
result = [result stringByAppendingFormat:@"%@", [prepare substringWithRange:NSMakeRange(i , 1)]];
}else{
result = [result stringByAppendingString:@"0"];
}
}
return result;
}
複製代碼
// 二進制轉十進制
- (NSString *)toDecimalWithBinary:(NSString *)binary{
int ll = 0 ;
int temp = 0 ;
for (int i = 0; i < binary.length; i ++){
temp = [[binary substringWithRange:NSMakeRange(i, 1)] intValue];
temp = temp * powf(2, binary.length - i - 1);
ll += temp;
}
NSString * result = [NSString stringWithFormat:@"%d",ll];
return result;
}
複製代碼
- (NSString *)getBinaryByhex:(NSString *)hex binary:(NSString *)binary{
NSMutableDictionary *hexDic = [[NSMutableDictionary alloc] init];
[hexDic = [[NSMutableDictionary alloc] initWithCapacity:16];
[hexDic setObject:@"0000" forKey:@"0"];
[hexDic setObject:@"0001" forKey:@"1"];
[hexDic setObject:@"0010" forKey:@"2"];
[hexDic setObject:@"0011" forKey:@"3"];
[hexDic setObject:@"0100" forKey:@"4"];
[hexDic setObject:@"0101" forKey:@"5"];
[hexDic setObject:@"0110" forKey:@"6"];
[hexDic setObject:@"0111" forKey:@"7"];
[hexDic setObject:@"1000" forKey:@"8"];
[hexDic setObject:@"1001" forKey:@"9"];
[hexDic setObject:@"1010" forKey:@"a"];
[hexDic setObject:@"1011" forKey:@"b"];
[hexDic setObject:@"1100" forKey:@"c"];
[hexDic setObject:@"1101" forKey:@"d"];
[hexDic setObject:@"1110" forKey:@"e"];
[hexDic setObject:@"1111" forKey:@"f"];
NSMutableString *binaryString=[[NSMutableString alloc] init];
if (hex.length) {
for (int i=0; i<[hex length]; i++) {
NSRange rage;
rage.length = 1;
rage.location = i;
NSString *key = [hex substringWithRange:rage];
[binaryString appendString:hexDic[key]];
}
}else{
for (int i=0; i<binary.length; i+=4) {
NSString *subStr = [binary substringWithRange:NSMakeRange(i, 4)];
int index = 0;
for (NSString *str in hexDic.allValues) {
index ++;
if ([subStr isEqualToString:str]) {
[binaryString appendString:hexDic.allKeys[index-1]];
break;
}
}
}
}
return binaryString;
}
複製代碼
有了這幾種轉換函數,完成上面的功能就容易多了,具體怎麼操做這裏就不寫一一出來了。但總感受怪怪的,這麼一個小功能怎麼要寫這麼一大堆代碼,固然還能夠用C語言的方法去解決。這裏主要是爲了展現iOS中數據如何轉換,C語言的實現方法這裏就不寫了,有興趣的同窗能夠研究下。編碼
int轉NSDataspa
- (NSData *) setId:(int)Id {
//用4個字節接收
Byte bytes[4];
bytes[0] = (Byte)(Id>>24);
bytes[1] = (Byte)(Id>>16);
bytes[2] = (Byte)(Id>>8);
bytes[3] = (Byte)(Id);
NSData *data = [NSData dataWithBytes:bytes length:4];
}
複製代碼
NSData轉int 接受到的數據0x00000a0122
//4字節表示的int
NSData *intData = [data subdataWithRange:NSMakeRange(2, 4)];
int value = CFSwapInt32BigToHost(*(int*)([intData bytes]));//655650
//2字節表示的int
NSData *intData = [data subdataWithRange:NSMakeRange(4, 2)];
int value = CFSwapInt16BigToHost(*(int*)([intData bytes]));//290
//1字節表示的int
char *bs = (unsigned char *)[[data subdataWithRange:NSMakeRange(5, 1) ] bytes];
int value = *bs;//34
複製代碼
//補充內容,由於沒有三個字節轉int的方法,這裏補充一個通用方法
- (unsigned)parseIntFromData:(NSData *)data{
NSString *dataDescription = [data description];
NSString *dataAsString = [dataDescription substringWithRange:NSMakeRange(1, [dataDescription length]-2)];
unsigned intData = 0;
NSScanner *scanner = [NSScanner scannerWithString:dataAsString];
[scanner scanHexInt:&intData];
return intData;
}
複製代碼
這兩個轉換在某些場景下使用頻率也是挺高的,藍牙裏面的數據轉換基本也就這麼多了,但願可以幫助你們。 更多關於字節編碼的問題,你們能夠點這裏:傳送門
基於CoreBluetooth4.0
框架的鏈接BLE4.0的Demo:你不點一下嗎