为什么无法在Android上从串行端口打开/写入? [英] Why can't I open/write from a serial port on Android?

查看:466
本文介绍了为什么无法在Android上从串行端口打开/写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Android应用程序,该应用程序在Android 4.4 Kitkat设备上的自定义内核上运行,该设备使用Android串行端口API(

I have written an Android app which runs on a custom kernel on Android 4.4 Kitkat device, which uses the Android Serial Port API (https://code.google.com/p/android-serialport-api/) in order to open the serial port "/dev/ttyACM0" which is the port associated with my serial device. The port has the proper "666" permissions (crw-rw-rw), and the app itself even has the WRITE_EXTERNAL_STORAGE" permission.

但是,即使权限显示正确,当我的应用尝试使用该库打开所述串行端口时,它也会失败.在我的Java代码中,如果尝试执行"device.canWrite()",则返回false,而"device.canRead()"则返回true.我觉得这是更大的权限问题的征兆.

However, even though the permissions appear correct, when my app tries to open said serial port using the library, it fails. In my java code, if I try to do a "device.canWrite()" it returns false, whereas a "device.canRead()" returns true. I feel like this is a symptom of a greater permissions problem.

如果我使用与串行端口api/library捆绑在一起的示例apk,则会遇到相同的问题,即应用程序在device.canWrite()处失败,并引发SecurityException.

If I use the sample apk bundled with the serial port api/library, I get the same issue, where the app fails at the device.canWrite() and throws a SecurityException.

当在最初为其开发应用程序的自定义内核ICS设备上运行时,完全相同的代码可以成功运行并打开.但是,这两个内核都是由同一位开发人员编写的,至少从表面上看,对端口的权限似乎是相同的.

The exact same code DOES successfully run and open when ran on a custom kernel ICS device, for which the app was originally developed for. However, the kernels for both were written by the same developer, and at least on a surface glance, the permissions on the port seem to be the same.

这可能是我可以在应用程序代码方面解决的问题吗?我尝试了其他一些串行端口库(USBSerial3.0,usb-serial-for-android-master),但似乎遇到了相同的问题.

Is it likely this is an issue that I can resolve on the application code side? I have tried some other serial port libraries (USBSerial3.0, usb-serial-for-android-master) and I seem to encounter the same issue.

不管问题出在哪里,任何人都对问题可能有什么想法吗?更改内核,应用程序和api都在表中.谢谢.

Regardless of where the issue may lie, would anyone have any ideas of what the problem may be? Making changes to the kernel, app, and api are all on the table. Thank you.

推荐答案

Gabe,Google在Android 4.4中将SELinux模式从允许模式"更改为强制模式",默认情况下,应用程序没有(SELinux)权限来访问串行端口.以下是解决此问题的几种方法: (1).如果您的设备是root用户,则可以将SELinux模式从强制模式"切换为允许模式":"su setenforce 0".每次重新启动设备时,都需要运行命令.参见 http://www.xda-developers.com/easily -change-your-android-selinux-mode/ (2).如果您可以访问设备的内核框架文件,则可以 可以更改它们并授予对您的应用程序的串行端口访问权限.

Gabe, Google changed the SELinux mode from Permissive Mode to Enforce Mode in Android 4.4, by default apps do not have the (SELinux) permissions to access the serial ports. Here are a couple of ways to work around the issue: (1). If your device is rooted you can toggle the SELinux mode from Enforce Mode to Permissive Mode: "su setenforce 0". You will need to run the command every time you restart your device. see http://www.xda-developers.com/easily-change-your-android-selinux-mode/ (2). If you have access to the kernel framework files for the device you can change them and grant serial port access to your apps.

这篇关于为什么无法在Android上从串行端口打开/写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆