首页 > 解决方案 > Android - 蓝牙 - 写入结束时崩溃

问题描述

我是 android 新手,我基本上是在两个设备之间发送一个简单的字节流。

似乎通过添加额外的活动(第二个布局上的确认按钮),我引入了一些潜在的复杂性,我无法确定传输后崩溃的原因。消息在输入流上传输,并在输出流上接收,但日志似乎表明管道损坏,尽管发生了传输。

我试图确保“确认页面”的 java 类从“主”继承 BT 功能。

我怀疑错误出现在 MainActivity 中,并且在从确认屏幕返回时发生此错误。

*发生的情况是我有一个新的活动,带有相应的按钮,以确认启动过程。一旦确认,BT应该发送一个开始信号,并返回主布局

这是我怀疑错误被本地化的地方。

MainActivity 类

public void OnStartClick(View view) {


    Intent GetStartConfirmation = new Intent(this, StartConfirm.class);

    final int res = 99; // can use as a signal for another time

    startActivityForResult(GetStartConfirmation, res);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    startPaintbot = resultCode;

    updateUi(resultCode);
}
private void updateUi(int res)
{
    if(res == 1) {
        //this should all be done on a result of '1'
        char data[] = {'s', 't', 'a', 'r', 't', 'u', 'p'};
        String str = new String(data);
        byte[] bytes = str.getBytes(Charset.defaultCharset());
        mBluetoothConnection.write(bytes);
    }
    else if (res == 0)
    {
        char data[] = {'s', 'u', 'p','p'};
        String str = new String(data);
        byte[] bytes = str.getBytes(Charset.defaultCharset());
        mBluetoothConnection.write(bytes);
    }
    else
    {
        char data[] = {'s', 'y', 'w','a'};
        String str = new String(data);
        byte[] bytes = str.getBytes(Charset.defaultCharset());
        mBluetoothConnection.write(bytes);
    }
}

开始确认类

public class StartConfirm extends MainActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.start_confirm_layout);

    Intent activityStartHome = getIntent();

}



public void OnStartConfirmation(View view) {
    // Vid 5, Derek
    Intent toStart = new Intent();

    // this is where the difficulties lie, the intents.

    setResult(1, toStart);

    finish(); // eventually will need to add something to these two intents
}

public void OnNoStartConfirmation(View view) {

    Intent DoNotStart = new Intent();

    setResult(0, DoNotStart);
    //Do not want canceled, indicates there was a failure on the button

    finish();
}
}

输出日志如下:

04-12 02:59:33.379 15665-15665/com.example.piotr.userinterface D/BluetoothConnectionServ: write: Write Called.
write: Writing to outputstream: startup

04-12 02:59:33.379 15665-15665/com.example.piotr.userinterface D/BluetoothConnectionServ: write: Write Called.
write: Writing to outputstream: startup
04-12 02:59:33.379 15665-15665/com.example.piotr.userinterface E/BluetoothConnectionServ: write: Error writing to output stream. Broken pipe
04-12 02:59:33.449 15665-15680/com.example.piotr.userinterface D/OpenGLRenderer: endAllActiveAnimators on 0x9f355d80 (RippleDrawable) with handle 0x9f42c370
04-12 02:59:33.459 15665-15665/com.example.piotr.userinterface I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@8eaf228 time:88717553
04-12 02:59:33.699 15665-15665/com.example.piotr.userinterface D/MainActivity: onDestroy: called.
04-12 02:59:33.699 15665-15665/com.example.piotr.userinterface D/AndroidRuntime: Shutting down VM
04-12 02:59:33.699 15665-15665/com.example.piotr.userinterface E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.piotr.userinterface, PID: 15665
java.lang.RuntimeException: Unable to destroy activity {com.example.piotr.userinterface/com.example.piotr.userinterface.StartConfirm}: java.lang.IllegalArgumentException: Receiver not registered: com.example.piotr.userinterface.MainActivity$1@dadacf9
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5061)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5084)
    at android.app.ActivityThread.access$1700(ActivityThread.java:221)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1853)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7224)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.example.piotr.userinterface.MainActivity$1@dadacf9
    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:878)
    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1283)
    at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:601)
    at com.example.piotr.userinterface.MainActivity.onDestroy(MainActivity.java:217)
    at android.app.Activity.performDestroy(Activity.java:7102)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1170)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:5039)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:5084) 
    at android.app.ActivityThread.access$1700(ActivityThread.java:221) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1853) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7224) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
04-12 02:59:36.049 15665-15665/com.example.piotr.userinterface I/Process: Sending signal. PID: 15665 SIG: 9

抱歉,如果这很恶心

https://github.com/PK-GH/PB/tree/PeteInWonderland/app/src/main/java/com/example/piotr/userinterface

标签: javaandroidbluetoothinputstream

解决方案


您接收数据的设备与发送设备断开连接,这就是出现管道损坏的原因。仅当连接的一侧断开时才出现,不知道哪个,但在这种情况下,我的东西是接收器,因为 java.lang.RuntimeException: Unable to destroy activity {com.example.piotr.userinterface/com.example.piotr.userinterface.StartConfirm}: java.lang.IllegalArgumentException: Receiver not registered: com.example.piotr.userinterface.MainActivity$1@dadacf9

我的建议检查连接之前是否已连接,如果是,请逐步调试并找到断开连接的位置


推荐阅读