我试图将字节从我的 android 发送到我的 arduino 以使其关闭/打开连接到它的 LED。当我打开我的 android 应用程序时,它会立即转到“未响应消息”并关闭。发布 LogCat 以防万一。
这是安卓代码:
package com.example.arduino;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button turnOn , turnOff;
BluetoothAdapter btAdapter;
OutputStream outStream;
BluetoothSocket btSocket;
byte one;
byte two;
BluetoothDevice btDevice;
String address = "40:98:4E:37:4E:51";
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
@SuppressWarnings("static-access")
@SuppressLint("ShowToast") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
turnOn = (Button) findViewById(R.id.On);
turnOff = (Button) findViewById(R.id.Off);
one = 0;
two = 1;
btAdapter = BluetoothAdapter.getDefaultAdapter();
try {
outStream = btSocket.getOutputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
btDevice = btAdapter.getRemoteDevice(address);
try {
btSocket = btDevice.createRfcommSocketToServiceRecord(uuid);
btSocket.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(btDevice.ACTION_ACL_CONNECTED != null){ //Suppressed.
Toast.makeText(getBaseContext(), "Connected!", Toast.LENGTH_SHORT).show(); //Suppressed.
}
turnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ledOn();
}
private void ledOn() {
// TODO Auto-generated method stub
try {
outStream.write(one);
outStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
turnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ledOff();
}
private void ledOff() {
// TODO Auto-generated method stub
try {
outStream.write(two);
outStream.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
日志猫:
10-17 23:32:05.269: W/dalvikvm(21770): threadid=1: thread exiting with uncaught exception (group=0x40c7ea08)
10-17 23:32:05.269: E/AndroidRuntime(21770): FATAL EXCEPTION: main
10-17 23:32:05.269: E/AndroidRuntime(21770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arduino/com.example.arduino.MainActivity}: java.lang.NullPointerException
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.os.Looper.loop(Looper.java:158)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.main(ActivityThread.java:5751)
10-17 23:32:05.269: E/AndroidRuntime(21770): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 23:32:05.269: E/AndroidRuntime(21770): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 23:32:05.269: E/AndroidRuntime(21770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-17 23:32:05.269: E/AndroidRuntime(21770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-17 23:32:05.269: E/AndroidRuntime(21770): at dalvik.system.NativeStart.main(Native Method)
10-17 23:32:05.269: E/AndroidRuntime(21770): Caused by: java.lang.NullPointerException
10-17 23:32:05.269: E/AndroidRuntime(21770): at com.example.arduino.MainActivity.onCreate(MainActivity.java:44)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.Activity.performCreate(Activity.java:5165)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
10-17 23:32:05.269: E/AndroidRuntime(21770): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
10-17 23:32:05.269: E/AndroidRuntime(21770): ... 11 more