我正在尝试通过UART电缆将设备(血氧仪)连接到Android智能手机。因此,我需要构建一个能够从该设备读取数据的应用程序。为此,我正在使用Android Things,但无法使该应用程序运行,因为我收到此错误:“安装失败。 安装失败 重新运行”
很确定,这个标签“ uses-library android:name =“ com.google.android.things” android:required =“ true”“使我收到此错误,但没有运行但崩溃了。
MainActivity:
public class MainActivity extends Activity {
String TAG = "Log";
private static final String UART_DEVICE_NAME = null;
private UartDevice mDevice;
private TextView tv;
private List<String> serial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = findViewById(R.id.serial);
serial = new ArrayList<>();
PeripheralManager manager = PeripheralManager.getInstance();
List<String> deviceList = manager.getUartDeviceList();
if (deviceList.isEmpty()) {
Log.i(TAG, "No UART port available on this device.");
} else {
Log.i(TAG, "List of available devices: " + deviceList);
}
// Attempt to access the UART device
try {
mDevice = manager.openUartDevice(deviceList.get(0));
} catch (IOException e) {
Log.w(TAG, "Unable to access UART device", e);
}
try {
setFlowControlEnabled(mDevice, false);
} catch (IOException e) {
e.printStackTrace();
}
try {
configureUartFrame(mDevice);
} catch (IOException e) {
e.printStackTrace();
}
try {
writeUartData(mDevice);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mDevice != null) {
try {
mDevice.close();
mDevice = null;
} catch (IOException e) {
Log.w(TAG, "Unable to close UART device", e);
}
}
}
public void configureUartFrame(UartDevice uart) throws IOException {
// Configure the UART port
uart.setBaudrate(57600);
uart.setDataSize(8);
uart.setParity(UartDevice.PARITY_NONE);
uart.setStopBits(1);
}
public void setFlowControlEnabled(UartDevice uart, boolean enable) throws
IOException {
uart.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_NONE);
}
public void writeUartData(UartDevice uart) throws IOException {
byte[] raw = new byte[]{(byte) 0xA5, (byte) 0x10, (byte) 0x02, (byte)
0x1A, (byte) 0x00, (byte) 0x00};
byte cs = (byte) ((raw[1] ^ raw[2] ^ raw[3] ^ raw[4]) & 0xFF);
raw[5] = cs;
int count = uart.write(raw, raw.length);
Log.d(TAG, "Wrote " + count + " bytes to peripheral");
readUartBuffer(uart);
}
public void readUartBuffer(UartDevice uart) throws IOException {
// Maximum amount of data to read at one time
final int maxCount = 9600;
byte[] buffer = new byte[maxCount];
int count;
while ((count = uart.read(buffer, buffer.length)) > 0) {
Log.d(TAG, "Read " + count + " bytes from peripheral");
serial.add(String.valueOf(buffer[count]) + " ");
}
StringBuilder data = new StringBuilder();
for (int i = 0; i < serial.size(); i++) {
data.append(serial.get(i));
}
tv.setText(data);
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oximetro">
<uses-permission
android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.things"
android:required="true"/>
</application>
</manifest>
build.Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.example.oximetro"
minSdkVersion 27
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.things:androidthings:+'
}
我非常感谢您提供的任何帮助,甚至可以替代我正在尝试做的事情,即使用Android智能手机通过UART从血氧仪读取数据。运行此代码,我得到上面提到的错误,因此该应用程序甚至无法在设备上打开。
答案 0 :(得分:0)
您无法在手机上运行Android Things库。您的电话没有用于连接硬件的库挂钩。知道它没有运行该应用程序所需的一切的手机,应拒绝安装。