2

I'm trying to make iOS app which will connect to a car with phone via OBDII (BLE).

I can get a connection with the module and ask about RPM's, and the answers are returned in parts.

First - 010C (Data used to get RPM's)

then - 41 0C 0A 98 \n \n

To read RPM's I need only the third and fourth byte (0A and 98). How can I do it?

Part of code from:

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F"]])
    {
        //NSLog(@"char %@", characteristic);

        NSData *sensorData = [characteristic value];
        uint8_t *bodyData = (uint16_t *)[sensorData bytes];

        NSLog(@"data bytes %s", bodyData);
    }
}
Dat Nguyen
  • 1,626
  • 22
  • 25

2 Answers2

0

Try this

NSData *sensorData = [characteristic value];
const uint8_t* bytes = (const uint8_t*)[sensorData bytes];
uint8_t third = bytes[3];
Bilal
  • 18,478
  • 8
  • 57
  • 72
0

The way to parse OBD2 answers is to convert the NSData to a string and then iterate over the nibbles to compute individual bytes.

Check Convert Hex to ASCII number on Objective-c for one way to do it.

Alternatively you might want to take a look at https://github.com/mickeyl/LTSupportAutomotive, which already does the heavy lifting.

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67