Integrating On-Board Diagnostics II (OBD-II) functionality into your Flutter application opens up a world of possibilities for automotive diagnostics, data monitoring, and vehicle performance analysis. This guide introduces the obd_connection_ii
Flutter plugin, a powerful tool for establishing seamless communication between your Flutter app and vehicle systems via OBD-II.
Getting Started with Flutter OBD2 Integration
To begin leveraging the obd_connection_ii
plugin in your Flutter project, you’ll need to add it as a dependency. The most direct method is by referencing the Git repository:
```yaml
dependencies:
flutter:
sdk: flutter
obd2_plugin:
git:
url: git://github.com/begaz/OBDII.git
ref: main
Alternatively, you can manually integrate the plugin by following these steps:
- Download the Plugin: Obtain the plugin files directly from the GitHub repository.
- Plugin Placement: Place the downloaded plugin folder within your main Flutter project directory.
- Extraction and Renaming: Extract the contents of the downloaded folder and rename it to
obd2_plugin
. - Dependency Declaration: Update your
pubspec.yaml
file to include the local path dependency:
```yaml
dependencies:
flutter:
sdk: flutter
obd2_plugin:
path: ../obd2_plugin/
After configuring the dependency in pubspec.yaml
, execute the following command in your terminal to fetch the plugin:
flutter pub get
This command ensures that the plugin is correctly added to your project and ready for use.
Utilizing the OBD2 Plugin in Your Flutter App
Once the plugin is integrated, you can start interacting with OBD-II data within your Flutter application.
Initializing the OBD2 Plugin
First, instantiate the Obd2Plugin
class to gain access to the plugin’s functionalities:
Obd2Plugin obd2 = Obd2Plugin();
Checking Bluetooth Status
Before establishing an OBD-II connection, it’s essential to verify Bluetooth status on the device. The following line of code prompts the user to enable Bluetooth if it’s currently disabled:
bool enabled = await obd2.enableBluetooth;
The enableBluetooth
method returns true
if Bluetooth is already enabled or successfully enabled upon user request.
Verifying Active Connection
To check if an active OBD-II connection already exists, use the hasConnection
method:
bool connected = await obd2.hasConnection;
This method returns true
if a connection is active, and false
otherwise.
Discovering Bluetooth Devices
The obd2_plugin
provides methods to discover Bluetooth devices for connection.
To retrieve a list of paired Bluetooth devices:
List<BluetoothDevice> devices = await obd2.getPairedDevices;
To scan for nearby Bluetooth devices:
List<BluetoothDevice> devices = await obd2.getNearbyDevices;
For a combined list of both paired and nearby devices:
List<BluetoothDevice> devices = await obd2.getNearbyPairedDevices;
Establishing a Connection to a Bluetooth Device
To connect to a specific Bluetooth device from the discovered list, use the getConnection
method:
obd2.getConnection(devices[index], (connection) {
print("connected to bluetooth device.");
}, (message) {
print("error in connecting: $message");
});
This method takes a BluetoothDevice
object, a success callback function (invoked upon successful connection), and an error callback function (invoked if connection fails).
Receiving Real-time OBD-II Data
To listen for and process real-time data streamed from the OBD-II adapter, use the setOnDataReceived
method:
obd2.setOnDataReceived((command, response, requestCode) {
print("$command => $response");
});
The setOnDataReceived
method should be initialized only once in your application. To check if it has already been initialized, use:
await obd2.isListenToDataInitialed;
Sending Commands and Configuring OBD-II Parameters
The obd2_plugin
allows sending custom commands and configuring OBD-II parameters using JSON format.
Configuring OBD-II with JSON
The configObdWithJSON
method facilitates sending configuration commands to the OBD-II adapter using a JSON string:
await obd2.configObdWithJSON(json, {int requestCode});
This method returns an integer value representing the recommended waiting time in milliseconds before sending subsequent commands. It is advisable to implement a delay using Future.delayed
as demonstrated below:
loading.start();
await Future.delayed(Duration(milliseconds: await obd2.configObdWithJSON(json)), () {
loading.stop();
});
JSON Configuration Examples
The plugin supports three primary methods for sending data, each utilizing JSON format strings: getParamsFromJSON
, getDTCFromJSON
, and configObdWithJSON
. Ensure your JSON strings adhere to the specified formats.
Configuration JSON Example
[
{ "command": "AT Z", "description": "", "status": true },
{ "command": "AT E0", "description": "", "status": true },
{ "command": "AT SP 0", "description": "", "status": true },
{ "command": "AT SH 81 10 F1", "description": "", "status": true },
{ "command": "AT H1", "description": "", "status": true },
{ "command": "AT S0", "description": "", "status": true },
{ "command": "AT M0", "description": "", "status": true },
{ "command": "AT AT 1", "description": "", "status": true },
{ "command": "01 00", "description": "", "status": true }
]
Parameter JSON Example
[
{ "PID": "AT RV", "length": 4, "title": "Battery Voltage", "unit": "V", "description": "<str>", "status": true },
{ "PID": "01 0C", "length": 2, "title": "Engine RPM", "unit": "RPM", "description": "<double>, (( [0] * 256) + [1] ) / 4", "status": true },
{ "PID": "01 0D", "length": 1, "title": "Speed", "unit": "Kh", "description": "<int>, [0]", "status": true },
{ "PID": "01 05", "length": 1, "title": "Engine Temp", "unit": "°C", "description": "<int>, [0] - 40", "status": true },
{ "PID": "01 0B", "length": 1, "title": "Manifold absolute pressure", "unit": "kPa", "description": "<int>, [0]", "status": true }
]
Diagnostic Trouble Codes (DTC) JSON Example
[
{ "id": 1, "created_at": "2021-12-05T16:33:18.965620Z", "command": "03", "response": "6", "status": true },
{ "id": 7, "created_at": "2021-12-05T16:35:01.516477Z", "command": "18 FF 00", "response": "", "status": true },
{ "id": 6, "created_at": "2021-12-05T16:34:51.417614Z", "command": "18 02 FF FF", "response": "", "status": true },
{ "id": 5, "created_at": "2021-12-05T16:34:23.837086Z", "command": "18 02 FF 00", "response": "", "status": true },
{ "id": 4, "created_at": "2021-12-05T16:34:12.496052Z", "command": "18 00 FF 00", "response": "", "status": true },
{ "id": 3, "created_at": "2021-12-05T16:33:38.323200Z", "command": "0A", "response": "6", "status": true },
{ "id": 2, "created_at": "2021-12-05T16:33:28.439547Z", "command": "07", "response": "6", "status": true },
{ "id": 34, "created_at": "2021-12-05T16:41:25.883408Z", "command": "17 FF 00", "response": "", "status": true },
{ "id": 35, "created_at": "2021-12-05T16:41:38.901888Z", "command": "13 FF 00", "response": "", "status": true },
{ "id": 36, "created_at": "2021-12-05T16:41:51.040962Z", "command": "19 02 AF", "response": "", "status": true },
{ "id": 37, "created_at": "2021-12-05T16:42:01.384228Z", "command": "19 02 AC", "response": "", "status": true },
{ "id": 38, "created_at": "2021-12-05T16:42:11.770741Z", "command": "19 02 8D", "response": "", "status": true },
{ "id": 39, "created_at": "2021-12-05T16:42:28.443368Z", "command": "19 02 23", "response": "", "status": true },
{ "id": 40, "created_at": "2021-12-05T16:42:39.200378Z", "command": "19 02 78", "response": "", "status": true },
{ "id": 41, "created_at": "2021-12-05T16:42:50.444404Z", "command": "19 02 08", "response": "", "status": true },
{ "id": 42, "created_at": "2021-12-05T16:43:00.466739Z", "command": "19 0F AC", "response": "", "status": true },
{ "id": 43, "created_at": "2021-12-05T16:43:10.645120Z", "command": "19 0F 8D", "response": "", "status": true },
{ "id": 44, "created_at": "2021-12-05T16:43:25.257023Z", "command": "19 0F 23", "response": "", "status": true },
{ "id": 45, "created_at": "2021-12-05T16:43:36.567099Z", "command": "19 D2 FF 00", "response": "", "status": true },
{ "id": 46, "created_at": "2021-12-05T17:15:56.352652Z", "command": "19 C2 FF 00", "response": "", "status": true },
{ "id": 47, "created_at": "2021-12-05T17:16:17.567797Z", "command": "19 FF FF 00", "response": "", "status": true }
]
After successfully configuring OBD-II parameters using configObdWithJSON
, you can access the real-time data received from the vehicle’s system within the setOnDataReceived
callback function.
Developed with ❤️ by Begaz.