4

I made an application with serial port comminication systems. But i need this. When serial data received, application triggers and opens the screen.

I already use USB_DEVICE_ATTACHED but i need something like that action "USB_DATA_RECEIVED". Is it possible?

XXX is a action that i need.

<receiver
        android:name=".receivers.SerialDataReceiver"
        android:enabled="true">

        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.XXX" />
        </intent-filter>
</receiver>
Can Can
  • 49
  • 1
  • 5

1 Answers1

1

There is no default action for this.

In your custom receiver, get the port and use this.

This is how it likes;

public class CustomBroadcastReceiver extends BroadcastReceiver {
public static final String BROADCAST = "arda.kaplan.android.action.broadcast";

@Override
public void onReceive(final Context context, Intent intent) {

        findDevices(context);
}




private void findDevices(final Context context) {

    UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

    final List<UsbSerialDriver> drivers = UsbSerialProber.getDefaultProber().findAllDrivers(usbManager);

    final List<UsbSerialPort> result = new ArrayList<>();

    for (final UsbSerialDriver driver : drivers) {

        final List<UsbSerialPort> ports = driver.getPorts();
        result.addAll(ports);
    }

    if (result.size() > 0) {

        UsbSerialPort usbSerialPort = result.get(0);

        UsbSerialDriver driver = usbSerialPort.getDriver();
        UsbDevice device = driver.getDevice();

        UsbDeviceConnection usbConnection = usbManager.openDevice(usbSerialPort.getDriver().getDevice());

        final UsbSerialDevice usbSerialDevice = UsbSerialDevice.createUsbSerialDevice(device, usbConnection);

        usbSerialDevice.open();
        usbSerialDevice.setBaudRate(9600);
        usbSerialDevice.setDataBits(UsbSerialInterface.DATA_BITS_8);
        usbSerialDevice.setStopBits(UsbSerialInterface.STOP_BITS_1);
        usbSerialDevice.setParity(UsbSerialInterface.PARITY_NONE);
        usbSerialDevice.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);


        usbSerialDevice.read(new UsbSerialInterface.UsbReadCallback() {

            @Override
            public void onReceivedData(final byte[] data) {

                Intent startAppIntent = new Intent(context, ComingFromBroadcastReceiverActivity.class);

                startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startAppIntent.putExtra(ComingFromBroadcastReceiverActivity.KEY, new String(data));

                context.startActivity(startAppIntent);
            }
        });

    } 
}

}

And this code for registering custom receiver

Intent intent = new Intent(CustomBroadcastReceiver.BROADCAST);
Bundle extras = new Bundle();
sendBroadcast(intent);

And this is manifest part

<receiver android:name=".receivers.CustomBroadcastReceiver">
        <intent-filter>
            <action android:name="arda.kaplan.android.action.broadcast" />
        </intent-filter>
    </receiver>
Arda Kaplan
  • 1,720
  • 1
  • 15
  • 23