I have a BLE device which has two modules
1-Blue Tooth (BlueCreation BC127A) 2-MicroController attached which has some set of commands, Like LOGIN RESET VERSION etc.
I want to write commands to that BLE which communicate with uC.
Following my specification/documentation the packet structure should be like :
When I write exact command on BLE I get error : "The value's length is invalid."
Its packet length according to document is 17 where as I think it is wrong because when I get it sizeof(byte)/sizeof(char); i get 4. I used both values but I was unable to get positive response.
I need help to overcome this issue by successfully writing command. This is my first project with BlueTooth integration.
My Code Following packet structure my command LOGIN becomes :
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]; }
Thanks.