我将Arduino配置为继续在TX端口上写东西,并通过Android Things Developer Preview 3连接到我的Raspberry Pi 3(我确定RX / TX电缆配置正确并且电压正常)。
我正在尝试使用Android Things库的PeripheralManagerService读取数据和写入数据的时间很长。
启动应用程序时出现错误:
04-11 16:26:13.665 163-163 /? I /外围设备:类型= 1400审核(0.0:44):AVC:拒绝{读写}名称=“ ttyAMA0” dev =“ tmpfs” ino = 1203 scontext = u:r:peripheralman:s0 tcontext = u:object_r:设备:s0 tclass = chr_file许可= 1
04-11 16:26:13.665 163-163 /? I /外围设备:类型= 1400审核(0.0:45):AVC:拒绝{打开} =“ / dev / ttyAMA0” dev =“ tmpfs” ino = 1203 scontext = u:r:peripheralman:s0 tcontext = u: object_r:设备:s0 tclass = chr_file允许= 1
04-11 16:26:13.665 163-163 /? I /外围设备:类型= 1400审核(0.0:46):AVC:拒绝{ioctl}用于路径=“ / dev / ttyAMA0” dev =“ tmpfs” ino = 1203 ioctlcmd = 5401 scontext = u:r:peripheralman:s0 tcontext = u:object_r:device:s0 tclass = chr_file允许= 1
我的代码:
public class MainActivity extends Activity {
private final int BUFFER_SIZE = 64;
private UartDevice rxtx;
public void configureUartFrame(UartDevice uart) throws IOException {
// Configure the UART port
uart.setBaudrate(9600);
}
public void writeUartData(UartDevice uart, String msg) throws IOException {
byte[] buffer = msg.getBytes();
int count = uart.write(buffer, buffer.length);
Log.d(TAG, "Wrote " + count + " bytes to peripheral");
}
public void readUartBuffer(UartDevice uart) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
int count;
while ((count = uart.read(buffer, buffer.length)) > 0) {
Log.d(TAG, "Read " + count + " bytes from peripheral");
Log.d("Message", new String(buffer));
}
}
@Override
protected void onStart() {
super.onStart();
try {
writeUartData(rxtx, "teste");
readUartBuffer(rxtx);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PeripheralManagerService manager = new PeripheralManagerService();
manager.getUartDeviceList();
List<String> portList = manager.getUartDeviceList();
if (portList.isEmpty()) {
Log.i(TAG, "No UART port available on this device:");
} else {
Log.i(TAG, "List of RXTX available ports: - " + portList);
}
try{
rxtx = manager.openUartDevice(portList.get(0));
configureUartFrame(rxtx);
}catch (IOException e){
e.printStackTrace();
}
}}
有人知道这会是什么吗?
最佳答案
我没有在您的代码中的任何地方注册UartDeviceCallback
。您如何轮询UART以获取新的传入数据?没有注册回调,您的应用将需要通过UartDevice.read()
定期轮询UART,以获取新的传入字节,我也看不到您的代码。似乎唯一被调用的地方是在onStart()
活动回调中。
回调是一种更有效的方法,所以我建议添加它。有关更多详细信息,请查看UART Peripheral I/O Guide。