#pragma mark - 向蓝牙发送数据
- (void)sendMessageToBle{
//加密数据
int index = arc4random() % 20;
NSString *doorcode = self.doorArray[index];
NSString *dateStr1 = [NSDate stringFromDate:[NSDate date] withFormat:@"yyyyMMdd"];
NSString *dateStr2 = [NSDate stringFromDate:[NSDate date] withFormat:@"MMddyyyy"];
NSString *dateStr = [NSString stringWithFormat:@"%@%@",dateStr1,dateStr2];
NSString *strA1 = @"OW15LjrCaY2bfcMl";
NSString *strB1 = @"aWuK48JRDPePsYXE";
Byte *A = [self strToByte:dateStr];
Byte *A1 = [self strToByte:strA1];
Byte *B = [self strToByte:doorcode];
Byte *B1 = [self strToByte:strB1];
Byte bleData[16] = {0};
for (int i = 0; i < strA1.length; i ++) {
Byte left = (Byte)(A[i] & A1[i]);
Byte right = (Byte)(B[i] | B1[i]);
bleData[i] = (Byte)(left ^ right);
}
Byte bleMD5[16] ={0};
CC_MD5(bleData, 16, bleMD5);
Byte final[15] = {0};
final[0] = 15;
for (int i = 0; i < 12; i ++) {
final[i + 1] = bleMD5[i];
}
final[13] = 13;
final[14] = 10;
NSData *finalData = [NSData dataWithBytes:final length:15];
[self writeValue:SERVICE_UUID characteristicUUID:CHAR_UUID p:self.myPeripheral data:finalData];
}
-(void) writeValue:(int)serviceUUID characteristicUUID:(int)characteristicUUID p:(CBPeripheral *)p data:(NSData *)data {
//这里对服务值以及特征值做了一下处理,按照硬件的识别方式转换一下
UInt16 s = [self swap:serviceUUID];
UInt16 c = [self swap:characteristicUUID];
NSData *sd = [[NSData alloc] initWithBytes:(char *)&s length:2];
NSData *cd = [[NSData alloc] initWithBytes:(char *)&c length:2];
CBUUID *su = [CBUUID UUIDWithData:sd];
CBUUID *cu = [CBUUID UUIDWithData:cd];
CBService *service = [self findServiceFromUUIDEx:su p:p];
if (!service) {
printf("Could not find service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:su],[self UUIDToString:(__bridge CFUUIDRef )p.identifier]);
return;
}
CBCharacteristic *characteristic = [self findCharacteristicFromUUIDEx:cu service:service];
if (!characteristic) {
printf("Could not find characteristic with UUID %s on service with UUID %s on peripheral with UUID %s\r\n",[self CBUUIDToString:cu],[self CBUUIDToString:su],[self UUIDToString:(__bridge CFUUIDRef )p.identifier]);
return;
}
[p setNotifyValue:YES forCharacteristic:characteristic];
//写入数据时选择有返回数据/无返回数据
if(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse)
{
[p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse];
}else
{
[p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
}
-(UInt16) swap:(UInt16)s {
UInt16 temp = s << 8;
temp |= (s >> 8);
return temp;
}
-(CBService *) findServiceFromUUIDEx:(CBUUID *)UUID p:(CBPeripheral *)p {
for(int i = 0; i < p.services.count; i++) {
CBService *s = [p.services objectAtIndex:i];
if ([self compareCBUUID:s.UUID UUID2:UUID]) return s;
}
return nil; //Service not found on this peripheral
}
-(int) compareCBUUID:(CBUUID *) UUID1 UUID2:(CBUUID *)UUID2 {
char b1[16];
char b2[16];
[UUID1.data getBytes:b1];
[UUID2.data getBytes:b2];
if (memcmp(b1, b2, UUID1.data.length) == 0)return 1;
else return 0;
}
-(const char *) CBUUIDToString:(CBUUID *) UUID {
return [[UUID.data description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
}
-(const char *) UUIDToString:(CFUUIDRef)UUID {
if (!UUID) return "NULL";
CFStringRef s = CFUUIDCreateString(NULL, UUID);
return CFStringGetCStringPtr(s, 0);
}
-(CBCharacteristic *) findCharacteristicFromUUIDEx:(CBUUID *)UUID service:(CBService*)service {
for(int i=0; i < service.characteristics.count; i++) {
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
if ([self compareCBUUID:c.UUID UUID2:UUID]) return c;
}
return nil; //Characteristic not found on this service
}
//字符串转byte
- (Byte *)strToByte:(NSString *)strBefor{
Byte *bt = (Byte *)malloc(16);
for (int i =0; i < strBefor.length; i++) {
int strInt = [strBefor characterAtIndex:i];
Byte b = (Byte) ((0xff & strInt) );//( Byte) 0xff&iByte;
bt[i] = b;
}
return bt;
}