1

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 :enter image description here

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.

Zeeshan
  • 4,194
  • 28
  • 32
  • 1
    Ok, show the code on how you read the value, giving us also it's value (as NSData, CBCharacteristic.value), and when you try to write it. – Larme Jul 03 '15 at 07:49
  • 1
    @Larme mate I added code. Characteristic with UUID that I added is Write type. – Zeeshan Jul 03 '15 at 09:11
  • Could you also show the code when your read the value and log the value? – Larme Jul 03 '15 at 09:12
  • 1
    My guess is that you should create the `NSData` like this: http://stackoverflow.com/a/26984127/1801544 – Larme Jul 03 '15 at 09:14
  • Yes, as @Larme said you need to create your NsData as a number of bytes, not characters. – Paulw11 Jul 03 '15 at 09:19
  • I tried adding NSData via method you sent @Larme it shows same error "The value's length is invalid." – Zeeshan Jul 03 '15 at 09:27
  • Show the value you read of this characteristic. – Larme Jul 03 '15 at 09:28
  • It take me back to delegate method : - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error – Zeeshan Jul 03 '15 at 09:30
  • Please update your question to show how you are now creating the data to send – Paulw11 Jul 03 '15 at 09:41
  • Using @Larme method length of Login is 17 which is right. – Zeeshan Jul 03 '15 at 09:49
  • Please, read the CBCharacteristic Value and show us what's in it. NSLog(@"Characteristic value: %@", [theCharacteristicWanted value]), in the appropriate method. – Larme Jul 03 '15 at 09:53
  • Service and Characteristics : <__NSArrayM 0x1751ea60>( CBCharacteristicPropertyWrite ) – Zeeshan Jul 03 '15 at 09:54

1 Answers1

0

It's considered that Bluetooth models has not completed configuration.

Please check the virtual machine application software for your BLE module. To confirm that the system has add the response function when receive data.

Dennis Mao
  • 54
  • 4