【问题标题】:How to fix 'Install failed. Installation failed <a href='rerun'>Rerun</a>' error in Android?如何修复“安装失败。 Android 中的安装失败 <a href='rerun'>Rerun</a>' 错误?
【发布时间】:2019-07-24 13:49:57
【问题描述】:

我正在尝试通过 UART 电缆将设备(血氧计)连接到 Android 智能手机。所以我需要构建一个能够从这个设备读取数据的应用程序。为此,我使用的是 Android Things,但我无法让这个应用程序运行,因为我收到这个错误:“安装失败。 安装失败 重播”

很确定这个标签 "uses-library android:name="com.google.android.things" android:required="true"" 给我这个错误,没有它运行但崩溃。

主活动:

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>

构建.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 从血氧计读取数据。运行这段代码我得到了上面提到的错误,所以应用程序甚至没有在设备上打开。

【问题讨论】:

    标签: java android uart android-things


    【解决方案1】:

    您无法在手机上运行 Android Things 库。您的手机没有用于建立硬件连接的库挂钩。手机知道它没有运行应用程序所需的一切,应该拒绝安装。

    【讨论】:

    • 感谢您提供此信息。但是我应该在哪里运行呢?在我的手机上运行是不可能的吗?从设备读取数据到 Android 智能手机的替代方法是什么?
    • 你不能在你的手机上运行这个。您需要使用与 Android Things 兼容的设备,或使用其他可以通过无线协议或云与您的手机通信的硬件设备。
    • @ALEXANDRECHAGASVIIRAJUNIOR 如果您将设备连接到 Android 手机,请使用 Android USB API 访问该设备:developer.android.com/guide/topics/connectivity/usb
    猜你喜欢
    • 2019-07-06
    • 2013-03-31
    • 2019-03-14
    • 1970-01-01
    • 2018-08-12
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多