Is it possible to connect Arduino project in android studio? I have here the Arduino Uno 3 and my GP2Y1014AU0F Compact Optical Dust Sensor. Im having a problem, I think the implementation ("com.github.mik3y:usb-serial-for-android:3.7.3") is not working? Because there's an error in my Fragment, the error is
Could not find com.hoho.android:usb-serial-for-android:3.3.0.
even though its in the implementation already.
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.github.anastr.speedviewlib.SpeedView;
import com.hoho.android.usbserial.driver.UsbSerialDriver;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import com.hoho.android.usbserial.driver.UsbSerialProber;
import java.io.IOException;
public class CurrentFragment extends Fragment {
private static final String ACTION_USB_PERMISSION = "com.example.USB_PERMISSION";
UsbManager usbManager;
UsbDevice device;
UsbSerialPort port;
SpeedView speedView;
TextView textView;
public CurrentFragment() {
// Required empty public constructor
}
public static CurrentFragment newInstance(String param1, String param2) {
CurrentFragment fragment = new CurrentFragment();
Bundle args = new Bundle();
args.putString("param1", param1);
args.putString("param2", param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
// Get parameters if needed
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_current, container, false);
speedView = view.findViewById(R.id.speedView);
textView = view.findViewById(R.id.textView);
Button acuteButton = view.findViewById(R.id.acute);
Button goodButton = view.findViewById(R.id.good);
Button unhealthButton = view.findViewById(R.id.unhealthy);
Button emergencyButton = view.findViewById(R.id.emergency);
Button veryunhealthyButton = view.findViewById(R.id.veryunhealthy);
Button fairButton = view.findViewById(R.id.fair);
acuteButton.setAlpha(0f);
goodButton.setAlpha(0f);
unhealthButton.setAlpha(0f);
emergencyButton.setAlpha(0f);
veryunhealthyButton.setAlpha(0f);
fairButton.setAlpha(0f);
acuteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new AcuteFragment());
}
});
goodButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new GoodFragment());
}
});
unhealthButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new UnhealthyFragment());
}
});
emergencyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new CurrentFragment());
}
});
veryunhealthyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new VeryUnhealthyFragment());
}
});
fairButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadFragment(new FairFragment());
}
});
usbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
PendingIntent permissionIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
getActivity().registerReceiver(usbReceiver, filter);
for (UsbDevice device : usbManager.getDeviceList().values()) {
usbManager.requestPermission(device, permissionIntent);
}
return view;
}
private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
UsbDeviceConnection connection = usbManager.openDevice(device);
UsbSerialDriver driver = UsbSerialProber.getDefaultProber().probeDevice(device);
if (driver != null) {
port = driver.getPorts().get(0);
try {
port.open(connection);
port.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
Log.d("USB", "Port opened and configured");
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
byte[] buffer = new byte[16];
int numBytesRead = port.read(buffer, 1000);
final String data = new String(buffer, 0, numBytesRead);
Log.d("USB", "Data read: " + data);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateUI(data);
}
});
} catch (IOException e) {
Log.e("USB", "IOException: " + e.getMessage());
e.printStackTrace();
break;
}
}
}
}).start();
} catch (IOException e) {
Log.e("USB", "Port open failed: " + e.getMessage());
e.printStackTrace();
}
} else {
Log.e("USB", "Driver is null");
}
}
}
}
}
}
};
private void updateUI(String data) {
try {
float dustDensity = Float.parseFloat(data.trim().split(" ")[2]);
speedView.speedTo(dustDensity);
textView.setText("Dust Density: " + dustDensity + " ug/m3");
if (dustDensity <= 25) {
speedView.setIndicatorColor(getResources().getColor(R.color.good));
} else if (dustDensity <= 35) {
speedView.setIndicatorColor(getResources().getColor(R.color.fair));
} else if (dustDensity <= 45) {
speedView.setIndicatorColor(getResources().getColor(R.color.unhealthy));
} else if (dustDensity <= 55) {
speedView.setIndicatorColor(getResources().getColor(R.color.very_unhealthy));
} else if (dustDensity <= 90) {
speedView.setIndicatorColor(getResources().getColor(R.color.acute_unhealthy));
} else {
speedView.setIndicatorColor(getResources().getColor(R.color.emergency));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(usbReceiver);
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
transaction.replace(R.id.framelayout1, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}