android - 如何在我的 BroadcastReceiver 中收听 android 串口数据

标签 android serial-port android-usb

我做了一个串口通信系统的应用。但我需要这个。当接收到串行数据时,应用程序触发并打开屏幕。

我已经使用了 USB_DEVICE_ATTACHED,但我需要像“USB_DATA_RECEIVED”这样的操作。是否可以?

XXX 是我需要的 Action 。

<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>

最佳答案

对此没有默认操作。

在您的自定义接收器中,获取端口并使用它。

这就是它喜欢的样子;

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);
            }
        });

    } 
}

以及用于注册自定义接收器的代码

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

这是明显的部分

<receiver android:name=".receivers.CustomBroadcastReceiver">
        <intent-filter>
            <action android:name="arda.kaplan.android.action.broadcast" />
        </intent-filter>
    </receiver>

关于android - 如何在我的 BroadcastReceiver 中收听 android 串口数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39915080/

相关文章:

java - 如何调用 onMeasure 方法? - 安卓

java - 在 Android 上获取 Cognito 凭证

Android GraphView 项目因实时更新而卡住

java - Android Studio错误 "cannot resolve method"

c++ - 构建数据包的更好方法 - 逐字节?

c - Linux C 写串口(Arduino)并等待应答

java - 串行端口在 Windows 上写入和读取不起作用

android - Nougat Camera API 中的 USB 网络摄像头支持

android-usb - 通过 USB 安装无法正常工作 - 在 7.0 设备上

java - 抑制 UsbRequestJNI/ALOGD 日志消息