1、如藍牙發送顏色變化數據 -- 根據硬件需要的各種數據的其中一種轉換方式,如下圖,硬件需要8個數據:10、20、30、40、50、60、70、80等
+(NSData *)colorFocusRGB:(NSMutableArray *)array{
Byte bytes[8];
bytes[0] = (Byte)([array[0] intValue]);
bytes[1] = (Byte)([array[1] intValue]);
bytes[2] = (Byte)([array[2] intValue]);
bytes[3] = (Byte)([array[3] intValue]);
bytes[4] = (Byte)([array[4] intValue]);
bytes[5] = (Byte)([array[5] intValue]);
bytes[6] = (Byte)([array[6] intValue]);
bytes[7] = (Byte)([array[7] intValue]);
NSData *data = [NSData dataWithBytes:bytes length:8];
return data;
}
2、藍牙發送顏色直接轉換 -- 硬件需要的數據只需要RGB和Alpha
+(NSData *)bleColorAlpha:(int)alpha andRed:(int)red andGreen:(int)green andBlue:(int)blue{
NSData * data = [[NSData alloc]init];
NSString * colorStr = [NSString stringWithFormat:@"ctl:%d:%d:%d:%d:",alpha,red,green,blue];
data = [colorStr dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"調節顏色的值:%@",data);
return data;
}
+(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];
}
NSLog(@"hexToBytes:data:%@",data);
return data;
}
4、將十進制轉換成十六進制,其中0表示成00
#pragma mark - 將十進制轉換爲16進制
+(NSString *)tenToHex:(long long 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;
}
}
5、發送指定的命令到硬件,如0XFF、0XAA等
+ (NSData *)testData
{
unsigned char Buffer[9];
Buffer[0] = 0x4A;
Buffer[1] = 0x53;
Buffer[2] = 0x02;
Buffer[3] = 0x23;
Buffer[4] = 0xAA;
Buffer[5] = 0xBB;
Buffer[6] = 0xCC;
Buffer[7] = 0x36;
Buffer[8] = 0x57;
return [NSData dataWithBytes:Buffer length:9];
}