I'm beginner new and i've done a lot of research on this subject but i couldn't find a helpful resource. I want to create an application on Android Studio that listens to Can-Bus line and runs only in the background without a user interface. I need to use J1939 library for this, but i don't know how to ensure communication. I would be glad if you help. Thank you.
CanBusService.java;
package com.example.j1939_android3;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class CanBusService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.d("CanBusService", "Service created");
// Initialize CAN bus communication here
// Add your CAN bus communication logic
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("CanBusService", "Service started");
// Start listening to the CAN bus
startListeningToCanBus();
return START_STICKY;
}
private void startListeningToCanBus() {
// Implement CAN bus listening logic here
// This method will run in the background
}
@Override
public IBinder onBind(Intent intent) {
// Return null because this service is not meant to be bound
return null;
}
}
CanBusHandler.java:
package com.example.j1939_android3;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class CanBusService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.d("CanBusService", "Service created");
// Initialize CAN bus communication here
// Add your CAN bus communication logic
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("CanBusService", "Service started");
// Start listening to the CAN bus
startListeningToCanBus();
return START_STICKY;
}
private void startListeningToCanBus() {
// Implement CAN bus listening logic here
// This method will run in the background
}
@Override
public IBinder onBind(Intent intent) {
// Return null because this service is not meant to be bound
return null;
}
}
CanBusApplication.java:
package com.example.j1939_android3;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class CanBusService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.d("CanBusService", "Service created");
// Initialize CAN bus communication here
// Add your CAN bus communication logic
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("CanBusService", "Service started");
// Start listening to the CAN bus
startListeningToCanBus();
return START_STICKY;
}
private void startListeningToCanBus() {
// Implement CAN bus listening logic here
// This method will run in the background
}
@Override
public IBinder onBind(Intent intent) {
// Return null because this service is not meant to be bound
return null;
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_DEVICE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.J1939_Android3"
tools:targetApi="31">
<activity android:name=".CanBusApplication"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".CanBusService"
android:exported="false">
</service>
</application>
</manifest>