我正在制作一个向乐高 NXT 发送一些字节的应用程序。我以前的应用程序使用相同的代码。当我运行它时,我在 LogCat 中得到了这个:
10-29 12:35:12.691: I/ENTRE(14494): ONCREATE
10-29 12:35:12.721: I/Excelente(14494): BT ACTIVADO
10-29 12:35:12.721: I/Excelente(14494): ENTRE A BT ACTIVAR
10-29 12:35:12.721: V/BluetoothSocket.cpp(14494): initSocketNative
10-29 12:35:12.721: V/BluetoothSocket.cpp(14494): ...fd 40 created (RFCOMM, lm = 26)
10-29 12:35:12.721: V/BluetoothSocket.cpp(14494): initSocketFromFdNative
10-29 12:35:12.751: D/BluetoothUtils(14494): isSocketAllowedBySecurityPolicy start : device null
10-29 12:35:13.442: V/BluetoothSocket.cpp(14494): connectNative
10-29 12:35:14.153: V/BluetoothSocket.cpp(14494): ...connect(40, RFCOMM) = 0 (errno 115)
10-29 12:35:14.163: I/CONECTE(14494): CONEXION EXITOSA
10-29 12:35:14.163: I/Excelente(14494): CONEXION EXITOSA
10-29 12:35:14.163: I/Exelente(14494): ENTRE A MOTOR
10-29 12:35:14.163: I/Excelente(14494): ENTRE A WRITE
10-29 12:35:14.163: D/AndroidRuntime(14494): Shutting down VM
10-29 12:35:14.163: W/dalvikvm(14494): threadid=1: thread exiting with uncaught exception (group=0x4114e2a0)
10-29 12:35:14.163: E/AndroidRuntime(14494): FATAL EXCEPTION: main
10-29 12:35:14.163: E/AndroidRuntime(14494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wordpress.sochidev.NXTControl/com.wordpress.sochidev.NXTControl.NXT}: java.lang.NullPointerException
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.ActivityThread.access$700(ActivityThread.java:134)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.os.Looper.loop(Looper.java:137)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.ActivityThread.main(ActivityThread.java:4867)
10-29 12:35:14.163: E/AndroidRuntime(14494): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 12:35:14.163: E/AndroidRuntime(14494): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 12:35:14.163: E/AndroidRuntime(14494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
10-29 12:35:14.163: E/AndroidRuntime(14494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
10-29 12:35:14.163: E/AndroidRuntime(14494): at dalvik.system.NativeStart.main(Native Method)
10-29 12:35:14.163: E/AndroidRuntime(14494): Caused by: java.lang.NullPointerException
10-29 12:35:14.163: E/AndroidRuntime(14494): at com.wordpress.sochidev.NXTControl.BT_comm.writeMessage(BT_comm.java:96)
10-29 12:35:14.163: E/AndroidRuntime(14494): at com.wordpress.sochidev.NXTControl.Motor.motor(Motor.java:41)
10-29 12:35:14.163: E/AndroidRuntime(14494): at com.wordpress.sochidev.NXTControl.NXT.onCreate(NXT.java:27)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.Activity.performCreate(Activity.java:5047)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-29 12:35:14.163: E/AndroidRuntime(14494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
10-29 12:35:14.163: E/AndroidRuntime(14494): ... 11 more
10-29 12:40:22.040: I/Process(14494): Sending signal. PID: 14494 SIG: 9
主要活动:
package com.wordpress.sochidev.NXTControl;
import com.wordpress.sochidev.NXTControl.BT_comm;
import com.wordpress.sochidev.NXTControl.Motor;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class NXT extends Activity {
boolean connection = false;
BT_comm BT = new BT_comm();
Motor MT = new Motor();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nxt);
Log.i("ENTRE", "ONCREATE");
BT.enableBT();
Log.i("Excelente", "BT ACTIVADO");
BT.connectToNXTs();
Log.i("Excelente", "CONEXION EXITOSA");
MT.motor(2,(byte) 0x64, false, true, false);
connection = true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nxt, menu);
return true;
}
}
电机类:
package com.wordpress.sochidev.NXTControl;
import android.util.Log;
public class Motor {
BT_comm BT = new BT_comm();
public void motor(int motor, byte power, boolean speedReg, boolean motorSync, boolean brake) {
Log.i("Exelente","ENTRE A MOTOR");
byte[] data = { (byte) 0x0c, (byte) 0x00, (byte) 0x80, (byte) 0x04, (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
switch (motor) {
case 0:
data[4] = (byte) 0xFF;
break;
case 1:
data[4] = (byte) 0x00;
break;
case 2:
data[4] = (byte) 0x01;
break;
case 3:
data[4] = (byte) 0x02;
break;
}
data[5] = power;
if (speedReg) {
data[7] = 0x01;
}
if (motorSync) {
data[7] = 0x02;
}
if (brake) {
data [6] = 0x02;
}
try {
BT.writeMessage (data);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.i("FAILED","SEND DATA");
}
}
}
BT_comm 类:
package com.wordpress.sochidev.NXTControl;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.UUID;
import com.wordpress.sochidev.NXTControl.NXT;
import com.wordpress.sochidev.NXTControl.Motor;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
public class BT_comm {
//Target NXTs for communication
final String nxt1 = "00:16:53:15:4D:66";
BluetoothAdapter localAdapter;
BluetoothSocket socket_nxt1;
boolean success=false;
BluetoothSocket mmSocket;
InputStream mmInStream;
OutputStream mmOutStream;
//Enables Bluetooth if not enabled
public void enableBT(){
localAdapter=BluetoothAdapter.getDefaultAdapter();
//If Bluetooth not enable then do it
if(localAdapter.isEnabled()==false){
localAdapter.enable();
while(!(localAdapter.isEnabled())){
}
}
}
//connect to both NXTs
public boolean connectToNXTs(){
//get the BluetoothDevice of the NXT
BluetoothDevice nxt_1 = localAdapter.getRemoteDevice(nxt1);
Log.i("Excelente", "ENTRE A BT ACTIVAR");
//try to connect to the nxt
try {
socket_nxt1 = nxt_1.createRfcommSocketToServiceRecord(UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB"));
socket_nxt1.connect();
success = true;
} catch (IOException e) {
Log.i("Bluetooth","Err: Device not found or cannot connect");
success=false;
}
if (success) {
Log.i("CONECTE","CONEXION EXITOSA");
}
return success;
}
public void writeMessage(byte[] msg) throws InterruptedException{
//BluetoothSocket connSock;
Log.i("Excelente","ENTRE A WRITE");
// connSock=socket_nxt1;
try {
// OutputStreamWriter out =new OutputStreamWriter(connSock.getOutputStream());
// mmOutStream.write(msg);
// out.flush();
OutputStream outputStream = socket_nxt1.getOutputStream();
Log.i("Excelente","Asigne socket");
outputStream.write(msg);
Log.i("Excelente","COMANDO EXITOSO");
Thread.sleep(100);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("PROBLEMA","ERROR DE DATOS");
}
}
public byte [] readMessage(){
byte[] buffer = new byte[64];
Log.i("Excelente","Entre a readmessage");
try {
InputStream inputstream = socket_nxt1.getInputStream();
inputstream.read(buffer);
//mmInStream.read(buffer);
Log.i("Excelente","Lei los datos");
for (int index = 0; index < buffer.length; index++){
Log.i("RECIBIDO:", String.format("0x%20x", buffer[index]));
}
Log.i("Excelente","Recibi datos y los escribi");
return buffer;
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("PROBLEMA","ERROR DE DATOS AL RECIBIR");
return null;
}
}
}
我想我是android dev的新手,所以我不知道错误是什么意思。有任何想法吗?
谢谢。