我有一个BLE设备,它有两个模块
1-Blue Tooth(BlueCreation BC127A) 附加了2-MicroController,它有一些命令集,如LOGIN RESET VERSION等。
我想向那个与uC通信的BLE写命令。
根据我的规范/文档,数据包结构应该是:
当我在BLE上写完确的命令时,我得到错误:"值的长度无效。"
根据文档的数据包长度为17,因为我认为这是错误的,因为当我得到它sizeof(byte)/ sizeof(char);我得到4.我使用了两个值,但我无法获得积极的回应。
我需要帮助才能通过成功编写命令来克服这个问题。这是我与BlueTooth集成的第一个项目。
我的代码 在数据包结构之后,我的命令LOGIN变为:
NSString *text = @"02 00 0B 00 00 00 01 10 82 10 83 04 05 06 00 00 03"; const char* byte = [text cStringUsingEncoding:[NSString defaultCStringEncoding]];//(char)[text UTF8String]; length = sizeof(byte)/sizeof(char); //NSData *data = [NSData dataWithBytes:&byte length:length]; NSData *data = [self dataWithStringHex:text]; if (self.bleShield.activePeripheral.state == CBPeripheralStateConnected) { CBUUID *uuid_service = [CBUUID UUIDWithString:@"67D13B00-89B8-11E3-9DE5-0002A5D5C51B"]; CBUUID *uuid_char = [CBUUID UUIDWithString:@"892778A0-89B8-11E3-8821-0002A5D5C51B"]; CBService *service = [self findServiceFromUUID:serviceUUID p:p]; if (!service) { NSLog(@"Could not find service with UUID %@ on peripheral with UUID %@", [self CBUUIDToString:serviceUUID], p.identifier.UUIDString); return; } CBCharacteristic *characteristic = [self findCharacteristicFromUUID:characteristicUUID service:service]; if (!characteristic) { NSLog(@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@", [self CBUUIDToString:characteristicUUID], [self CBUUIDToString:serviceUUID], p.identifier.UUIDString); return; } [p writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; } -(CBService *) findServiceFromUUID:(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 } -(CBCharacteristic *) findCharacteristicFromUUID:(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 } - (NSData *)dataWithStringHex:(NSString *)string { NSString *cleanString; cleanString = [string stringByReplacingOccurrencesOfString:@"<" withString:@""]; cleanString = [cleanString stringByReplacingOccurrencesOfString:@">" withString:@""]; cleanString = [cleanString stringByReplacingOccurrencesOfString:@" " withString:@""]; NSInteger length = [cleanString length]; uint8_t buffer[length/2]; for (NSInteger i = 0; i < length; i+=2) { unsigned result = 0; NSScanner *scanner = [NSScanner scannerWithString:[cleanString substringWithRange:NSMakeRange(i, 2)]]; [scanner scanHexInt:&result]; buffer[i/2] = result; } return [[NSMutableData alloc] initWithBytes:&buffer length:length/2]; }
感谢。
答案 0 :(得分:0)
我们认为蓝牙型号尚未完成配置。
请检查BLE模块的虚拟机应用程序软件。 确认系统在接收数据时添加了响应函数。