2017-06-01 67 views
2

我試圖讓iOS應用程序將通過OBDII(BLE)與手機連接到汽車。如何從NSData的(OBDII)取第三個字節

我可以獲得與模塊的連接並詢問有關RPM的問題,並將答案返回給零件。

第一 - 010C(數據用於獲取RPM的)

然後 - 41 0C 0A 98 \n \n

閱讀RPM的我只需要第三個和第四個字節(0A98)。我該怎麼做?從代碼

部分:

- (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); 
    } 
} 

回答

0

試試這個

NSData *sensorData = [characteristic value]; 
const uint8_t* bytes = (const uint8_t*)[sensorData bytes]; 
uint8_t third = bytes[3]; 
+0

當我試着使用的NSLog打印第三(@ 「新的字節%d」,第三);結果是「新的字節0」 – kajz3reczka