How to detect arduino board through USB using android phone

Thread Starter

Don_Fila

Joined Nov 26, 2021
171
i was desingning an app that can communicate between android phone and arduino board. but whenever i connect the android device to the arduino the USB is detected and working but it says No arduino board detected. Below is the detection code, I really need help and any help from you will be highly appreciated.

C-like:
private void detectConnectedArduinoBoard() {

if (usbManager == null) {

Toast.makeText(this, "USB manager is not available", Toast.LENGTH_SHORT).show();

return;

}

HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();

if (deviceList.isEmpty()) {

Toast.makeText(this, "No USB devices detected", Toast.LENGTH_SHORT).show();

return;

}

for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) {

UsbDevice device = entry.getValue();

if (isArduinoUnoDevice(device)) {

// Display Arduino Uno board info

                String info = "Arduino Uno board detected\nManufacturer: " + device.getManufacturerName() +

"\nProduct Name: " + device.getProductName() +

"\nVendor ID: " + device.getVendorId() +

"\nProduct ID: " + device.getProductId();

Toast.makeText(this, info, Toast.LENGTH_LONG).show();

return;

} else if (isArduinoNanoDevice(device)) {

// Display Arduino Nano board info

                String info = "Arduino Nano board detected\nManufacturer: " + device.getManufacturerName() +

"\nProduct Name: " + device.getProductName() +

"\nVendor ID: " + device.getVendorId() +

"\nProduct ID: " + device.getProductId();

Toast.makeText(this, info, Toast.LENGTH_LONG).show();

return;

} else if (isUSB2SerialDevice(device)) {

// Display USB2.0 Serial device info

                String info = "USB2.0 Serial device detected\nManufacturer: " + device.getManufacturerName() +

"\nProduct Name: " + device.getProductName() +

"\nVendor ID: " + device.getVendorId() +

"\nProduct ID: " + device.getProductId();

Toast.makeText(this, info, Toast.LENGTH_LONG).show();

return;

}

}

Toast.makeText(this, "No Arduino board detected", Toast.LENGTH_SHORT).show();

}


// Method to check if the connected device is an Arduino Uno

    private boolean isArduinoUnoDevice(UsbDevice device) {

// Replace these vendor and product ID values with the actual IDs of the Arduino Uno

        return device.getVendorId() == 9025 && device.getProductId() == 67;

}


// Method to check if the connected device is an Arduino Nano

    private boolean isArduinoNanoDevice(UsbDevice device) {

// Replace these vendor and product ID values with the actual IDs of the Arduino Nano

        // Example IDs for Arduino Nano

        return device.getVendorId() == 1027 && device.getProductId() == 24577;

}


// Method to check if the connected device is a USB2.0 Serial device


    private boolean isUSB2SerialDevice(UsbDevice device) {

// Replace these vendor and product ID values with the actual IDs of your USB2.0 Serial device

        // Example IDs for USB2.0 Serial

        return device.getVendorId() == 1234 && device.getProductId() == 5678;

}


// Register BroadcastReceiver for USB device attachment and detachment events

    // Register BroadcastReceiver for USB device attachment and detachment events

    private void registerUSBReceiver() {

usbReceiver = new BroadcastReceiver() {

[USER=54416]@override[/USER]

            public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {

// USB device attached, trigger detection

                    detectConnectedArduinoBoard();

} // USB device detached, no action needed in this case

                // Device detachment might not require any action in this context


            }

};

IntentFilter filter = new IntentFilter();

filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);

filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);

registerReceiver(usbReceiver, filter);

}


// Unregister the USB BroadcastReceiver

    private void unregisterUSBReceiver() {

if (usbReceiver != null) {

unregisterReceiver(usbReceiver);

}

}

}
 
Last edited by a moderator:

Ya’akov

Joined Jan 27, 2019
9,229
// Method to check if the connected device is an Arduino Uno

private boolean isArduinoUnoDevice(UsbDevice device) {

// Replace these vendor and product ID values with the actual IDs of the Arduino Uno

return device.getVendorId() == 9025 && device.getProductId() == 67; }
In the code, detection is entirely dependent on the USB Vendor and Product IDs, and there are only tests for the Uno and the Nano—which apparently don’t even have the correct numbers for the IDs.

1. Have you replaced the examples with real data?
2. Are you using an Uno or a Nano for testing?
 

Thread Starter

Don_Fila

Joined Nov 26, 2021
171
In the code, detection is entirely dependent on the USB Vendor and Product IDs, and there are only tests for the Uno and the Nano—which apparently don’t even have the correct numbers for the IDs.

1. Have you replaced the examples with real data?
2. Are you using an Uno or a Nano for testing?
Since it isn't easy for me to get those ID's I got that online and think that is why it isn't working.

Yes I am using Arduino Nano
 
Top