Android - 蓝牙 - 写作

时间:2018-05-31 06:29:00

标签: java android bluetooth inputstream

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

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

我试图确保'确认页'的java类继承'main'的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);
    }
}

StartConfirm类

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

1 个答案:

答案 0 :(得分:0)

接收数据的设备会在设备发送时被删除,这就是为什么管道出现断线的原因。 只有当连接的一侧断开时才会出现但不知道哪一个但是在这种情况下我的东西是接收器因为 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

我的建议检查连接是否已连接之前,如果是,则逐步调试并找到解除连接的地方