按下蓝牙arduino按钮后应用程序崩溃



我想编程一个可以通过蓝牙控制我的Arduino的应用程序,但每次我按下按钮,应用程序都会崩溃。我收到了下面的代码和错误代码,我希望有人能帮我。我对这个android应用程序编程完全陌生。

主要活动

package com.example.arduinofernsteuerung;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {
private final String DEVICE_ADDRESS = "98:d3:31:f5:a9:e5";
private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private BluetoothDevice device;
private BluetoothSocket socket;
private OutputStream outputStream;
Button Vorwaerts, Links, Rechts, Rueckwaerts;
String command;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Vorwaerts = (Button) findViewById(R.id.Vorwaerts);
Links = (Button) findViewById(R.id.Links);
Rechts = (Button) findViewById(R.id.Rechts);
Rueckwaerts = (Button) findViewById(R.id.Rueckwaerts);

Vorwaerts.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
command = "1";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
command = "10";
try
{
outputStream.write(command.getBytes());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return false;
}
});

Rueckwaerts.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
command = "2";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
command = "10";
try
{
outputStream.write(command.getBytes());
}
catch(IOException e)
{
}
}
return false;
}
});

Links.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
command = "3";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
command = "10";
try
{
outputStream.write(command.getBytes());
}
catch(IOException e)
{
}
}
return false;
}
});

Rechts.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
command = "4";
try
{
outputStream.write(command.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if(event.getAction() == MotionEvent.ACTION_UP)
{
command = "10";
try
{
outputStream.write(command.getBytes());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return false;
}
});
}

public boolean BTinit()
{
boolean found = false;
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null)
{
Toast.makeText(getApplicationContext(), "Device doesn't support bluetooth", Toast.LENGTH_SHORT).show();
}
if(!bluetoothAdapter.isEnabled())
{
Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableAdapter,0);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices();
if(bondedDevices.isEmpty())
{
Toast.makeText(getApplicationContext(), "Please pair the device first", Toast.LENGTH_SHORT).show();
}
else
{
for(BluetoothDevice iterator : bondedDevices)
{
if(iterator.getAddress().equals(DEVICE_ADDRESS))
{
device = iterator;
found = true;
break;
}
}
}
return found;
}
public boolean BTconnect()
{
boolean connected = true;
try
{
socket = device.createRfcommSocketToServiceRecord(PORT_UUID);
socket.connect();
Toast.makeText(getApplicationContext(),
"Connection to bluetooth device successful", Toast.LENGTH_LONG).show();
}
catch(IOException e)
{
e.printStackTrace();
connected = false;
}
if(connected)
{
try
{
outputStream = socket.getOutputStream();
}
catch(IOException e)
{
e.printStackTrace();
}
}
return connected;
}
@Override
protected void onStart()
{
super.onStart();
}
}

安卓清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arduinofernsteuerung">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<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>
</application>
</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
tools:context=".MainActivity">
<Button
android:id="@+id/Vorwaerts"
android:text="Vorwärts"
android:layout_height="70dp"
android:layout_width="120dp"
android:layout_alignParentTop="true"
android:layout_marginTop="300dp"
android:layout_toStartOf="@+id/Rechts"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/Links"
android:text="Links"
android:layout_height="70dp"
android:layout_width="120dp"
android:layout_below="@+id/Vorwaerts"
android:layout_marginTop="2dp"
android:layout_marginEnd="13dp"
android:layout_toStartOf="@+id/Vorwaerts" />
<Button
android:id="@+id/Rechts"
android:text="Rechts"
android:layout_height="70dp"
android:layout_width="120dp"
android:layout_above="@+id/Rueckwaerts"
android:layout_marginStart="13dp"
android:layout_marginBottom="3dp"
android:layout_toEndOf="@+id/Rueckwaerts" />
<Button
android:id="@+id/Rueckwaerts"
android:text="Rückwärts"
android:layout_width="120dp"
android:layout_height="70dp"
android:layout_below="@+id/Links"
android:layout_centerHorizontal="true"
android:layout_marginTop="-3dp" />

</RelativeLayout>

错误

2020-03-11 15:01:31.432 20330-20330/com.example.arduinofernsteuerung E/InputEventReceiver: Exception dispatching input event.
2020-03-11 15:01:31.432 20330-20330/com.example.arduinofernsteuerung E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
2020-03-11 15:01:31.438 20330-20330/com.example.arduinofernsteuerung E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
at com.example.arduinofernsteuerung.MainActivity$1.onTouch(MainActivity.java:54)
at android.view.View.dispatchTouchEvent(View.java:14372)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3492)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3492)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3492)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3492)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3492)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3863)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:3492)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:729)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1922)
at android.app.Activity.dispatchTouchEvent(Activity.java:4051)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:687)
at android.view.View.dispatchPointerEvent(View.java:14644)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6456)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6243)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5681)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5734)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5700)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5856)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5708)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5913)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5681)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5734)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5700)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5708)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5681)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8840)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8701)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8654)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8976)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:194)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:336)
at android.os.Looper.loop(Looper.java:197)
at android.app.ActivityThread.main(ActivityThread.java:7811)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
2020-03-11 15:01:31.438 20330-20330/com.example.arduinofernsteuerung D/AndroidRuntime: Shutting down VM
2020-03-11 15:01:31.441 20330-20330/com.example.arduinofernsteuerung E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.arduinofernsteuerung, PID: 20330
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
at com.example.arduinofernsteuerung.MainActivity$1.onTouch(MainActivity.java:54)
at android.view.View.dispatchTouchEvent(View.java:14372)

我现在写的是:public boolean write((,而不是:public boolean BTConnect((。我希望这就是你的意思,我仍然有同样的问题,但谢谢你的快速评论

您的代码中有一些奇怪的地方,您永远不会调用BTConnect((方法。这就是当您尝试对outputStream调用write((时,它为null的原因。我认为它可能会这样工作,试试看,让我们保持联系。

最新更新