react-native-obd-retriver
Version:
A React Native hook library to manage Bluetooth Low Energy connections and communication with ELM327 OBD-II adapters.
116 lines • 4.14 kB
TypeScript
import type { ServiceMode } from './types';
import type { SendCommandFunction } from '../utils/types';
/**
* Retrieves the Vehicle Identification Number (VIN) from the vehicle
*
* The VINRetriever class specializes in retrieving the 17-character VIN
* from vehicle ECUs using OBD Mode 09 PID 02. This class handles:
*
* - Adapter configuration for optimal VIN retrieval
* - Protocol detection and adjustment
* - Multi-frame response handling
* - Flow control on CAN-based protocols
* - Parsing and validation of VIN data
* - Automatic retries with different settings
*
* The VIN is a crucial vehicle identifier containing encoded information about:
* - Manufacturer/make (first 3 characters)
* - Vehicle attributes (positions 4-8)
* - Check digit validation (position 9)
* - Model year (position 10)
* - Plant code (position 11)
* - Production sequence number (last 6 digits)
*
* This class is standalone and includes its own adapter configuration,
* protocol detection, and enhanced flow control handling logic. It's designed
* to work reliably across different vehicle makes, models, and OBD protocols.
*
* @example
* ```typescript
* // Create a VIN retriever instance
* const vinRetriever = new VINRetriever(sendCommand);
*
* // Retrieve the vehicle's VIN
* const vin = await vinRetriever.retrieveVIN();
*
* if (vin) {
* console.log(`Vehicle VIN: ${vin}`); // e.g. "1HGCM82633A123456"
* console.log(`Manufacturer: ${vin.substring(0,3)}`); // e.g. "1HG" (Honda)
* console.log(`Model Year: ${decodeModelYear(vin.charAt(9))}`); // e.g. "2003"
* } else {
* console.error("Unable to retrieve VIN");
* }
* ```
*/
export declare class VINRetriever {
static SERVICE_MODE: ServiceMode;
private static readonly DATA_TIMEOUT;
private static readonly COMMAND_TIMEOUT;
private readonly sendCommand;
private readonly mode;
private isCan;
private protocolNumber;
private protocolType;
private headerFormat;
private ecuResponseHeader;
private protocolState;
private isHeaderEnabled;
constructor(sendCommand: SendCommandFunction);
/**
* Helper method to create a delay.
*/
private delay;
/**
* Configure adapter specifically for VIN retrieval.
* Includes reset, basic settings, protocol detection, ECU header detection, and specific config.
*/
private _configureAdapterForVIN;
/**
* (NEW) Attempts to detect the primary ECU response header by sending 0100.
* Stores the result in this.ecuResponseHeader.
*/
private _detectEcuResponseHeader;
/**
* Determine the active protocol by querying the adapter (ATDPN).
* Updates internal state variables.
*/
private _detectProtocol;
/**
* Updates internal protocol state based on the ELM protocol number.
*/
private _updateProtocolInfo;
/**
* Applies protocol-specific configurations, including default CAN Flow Control using detected header if available.
*/
private _configureForProtocol;
/**
* Enhanced method to send commands with timing appropriate for the detected protocol.
*/
private _sendCommandWithTiming;
/**
* Check if a response string indicates an ELM or OBD error.
* Uses imported isResponseError helper, treats null response as error.
*/
private isErrorResponse;
/**
* Tries different CAN flow control configurations to optimize communication.
* Now includes Block Size testing and uses detected ECU header.
*/
private _tryOptimizeFlowControl;
/**
* Sends the VIN request command and verifies/processes the response.
* Handles potential flow control issues for CAN. Returns raw response string on success.
*/
private _sendVINRequestAndProcess;
/**
* Retrieves and parses the VIN.
* Orchestrates configuration, command sending, retries, and parsing.
*/
retrieveVIN(): Promise<string | null>;
/**
* Resets the internal state of the retriever.
*/
resetState(): void;
getServiceMode(): ServiceMode;
}
//# sourceMappingURL=VINRetriever.d.ts.map