對於我今年夏天的機器人考試,我正在構建一個機器人,我想通過藍牙與Android設備進行通信。爲此,我正在編寫自己的Android應用程序。我沒有問題發送數據,但我不能收到數據。我對接收數據的代碼,不工作,看起來是這樣的:在我的Android應用程序中通過藍牙接收數據的問題
new Thread(new Runnable() {
public void run(){
byte[] buffer = new byte[1024]; //Buffer for the incoming message
int bytes;
TextView afstandsTekst = (TextView)findViewById(R.id.afstandsText);
//Listen to the InputStream
while(true){
try {
if(mmInStream.available() != 0)
try {
bytes = mmInStream.read(buffer); //Read from the InputStream. This is where the app crashes.
}
catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
break;
}
afstandsTekst.setText(String.valueOf(bytes));
}
}
}).start();
應用崩潰的
bytes = mmInStream.read(buffer);
,我不明白爲什麼。我已經使用Android Developers上的藍牙頁面尋求幫助(http://developer.android.com/guide/topics/wireless/bluetooth.html#ManagingAConnection),並試圖像一樣和他們一樣,但是仍然崩潰,這就是爲什麼我現在嘗試了我上面粘貼的代碼。 另外,即使應用程序在正常運行時崩潰,當我選擇在Samsung Galaxy Nexus中的Eclipse中進行調試時,應用程序也不會崩潰,我不知道爲什麼。
這是我第一篇帖子到這個論壇,我非常希望有人在那裏會有答案。提前致謝!
編輯:下面是完整的代碼:
package skole.migogjesper.hospitalsseng;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothClass.Device;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HospitalssengActivity extends Activity {
private String address = "00:06:66:45:B8:DB";
private static final int REQUEST_ENABLE_BT = 3;
protected static final String EXTRA_DEVICE_ADDRESS = null;
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
public ArrayAdapter<String> mArrayAdapter;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //Indlæser bluetooth modulet i enheden.
private BluetoothDevice mmDevice = mBluetoothAdapter.getRemoteDevice(address);
private OutputStream mmOutStream;
private InputStream mmInStream;
private char stueDestination = '0';
private char scannerDestination = '1';
private char operationsstueDestination = '2';
private char krematorieDestination = '3';
private char xrayDestination = '4';
private char planlagtStueDestination = 'q';
private char planlagtScannerDestination = 'w';
private char planlagtOperationsstueDestination = 'e';
private char planlagtKrematorieDestination = 'r';
private char planlagtXrayDestination = 't';
private byte sendByte;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(mBluetoothAdapter == null){ //Tjekker om bluetooth er underst¯ttet.
Toast.makeText(HospitalssengActivity.this, "Enheden understøtter ikke bluetooth", Toast.LENGTH_LONG).show();
}
if(!mBluetoothAdapter.isEnabled()){ //Tjekker om bluetooth er tÊndt.
Intent enableBtIntent = new Intent (BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
new Thread(new Runnable() {
public void run(){
byte[] buffer = new byte[1024]; //Buffer til den indkommende besked
int bytes; //Bytes der kommer fra read()
TextView afstandsTekst = (TextView)findViewById(R.id.afstandsText);
//Lyt efter InputStream indtil der sker en exception
while(true){
try {
if(mmInStream.available() != 0)
try {
bytes = mmInStream.read(buffer); //Læs fra InputStream
}
catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
break;
}
//afstandsTekst.setText(String.valueOf(bytes));
}
}
}).start();
}
private BluetoothSocket mmSocket;
public void Connect(BluetoothDevice device){
BluetoothSocket tmp = null; //Dette er er et midlertidigt objekt, som senere bliver assigned til mmSocket.
mmDevice = device;
try { //Få en BluetoothSocket, som kan bruges til at forbinde med en BluetoothDevice
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(device, 1);
} catch(Exception e) {
Toast.makeText(HospitalssengActivity.this, "FEJL!", Toast.LENGTH_LONG).show();
}
mmSocket = tmp;
try{ //Forbind enheden gennem socket'en. Dette blokerer indtil den er succesfuld eller den fejler.
mmSocket.connect();
} catch (IOException connectException) {
try{mmSocket.close();} //Fejlet forbindelse. Luk socket'en og hop ud af metoden.
catch(IOException closeException) {}
return;
}
}
public void cancel(){ //Lukker en forbindelse og lukker socket'en.
try{mmSocket.close();}
catch(IOException e) {}
}
public void write(byte sendByte){
try {
mmOutStream = mmSocket.getOutputStream();
mmOutStream.write(sendByte);
Toast.makeText(HospitalssengActivity.this, "Sendt", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {}
}
public void scanKnap(View view){
Connect(mmDevice);
}
public void afbrydKnap(View view){
cancel();
}
public void stueKnap(View view){
CheckBox planlagtCheck = (CheckBox) findViewById(R.id.planlagtCheck);
if(!planlagtCheck.isChecked()){
sendByte = (byte) stueDestination;
write(sendByte);
}
if(planlagtCheck.isChecked()){
sendByte = (byte) planlagtStueDestination;
write(sendByte);
}
}
public void scannerKnap(View view){
CheckBox planlagtCheck = (CheckBox) findViewById(R.id.planlagtCheck);
if(!planlagtCheck.isChecked()){
sendByte = (byte) scannerDestination;
write(sendByte);
}
if(planlagtCheck.isChecked()){
sendByte = (byte) planlagtScannerDestination;
write(sendByte);
}
}
public void operationsstueKnap(View view){
CheckBox planlagtCheck = (CheckBox) findViewById(R.id.planlagtCheck);
if(!planlagtCheck.isChecked()){
sendByte = (byte) operationsstueDestination;
write(sendByte);
}
if(planlagtCheck.isChecked()){
sendByte = (byte) planlagtOperationsstueDestination;
write(sendByte);
}
}
public void krematorieKnap(View view){
CheckBox planlagtCheck = (CheckBox) findViewById(R.id.planlagtCheck);
if(!planlagtCheck.isChecked()){
sendByte = (byte) krematorieDestination;
write(sendByte);
}
if(planlagtCheck.isChecked()){
sendByte = (byte) planlagtKrematorieDestination;
write(sendByte);
}
}
public void xrayKnap(View view){
CheckBox planlagtCheck = (CheckBox) findViewById(R.id.planlagtCheck);
if(!planlagtCheck.isChecked()){
sendByte = (byte) xrayDestination;
write(sendByte);
}
if(planlagtCheck.isChecked()){
sendByte = (byte) planlagtXrayDestination;
write(sendByte);
}
}
}
發佈例外 – Ran 2012-04-17 13:42:37
嗨@Ran感謝您的回覆!Eclipse中LogCat窗口的輸出如下,在崩潰後: 04-17 16:29:20.559:W/dalvikvm(13854):threadid = 11:線程退出與未捕獲的異常(組= 0x40a341f8) 04 - 17 16:29:20.559:E/AndroidRuntime(13854):致命例外:線程 - 1635年 04 - 17日16:29:20。559:E/AndroidRuntime(13854):java.lang.NullPointerException 04-17 16:29:20.559:E/AndroidRuntime(13854):\t at skole.migogjesper.hospitalsseng.HospitalssengActivity $ 1.run(HospitalssengActivity.java:71) 04-17 16:29:20.559:E/AndroidRuntime(13854):\t at java.lang.Thread.run(Thread.java:856) – WuPtI 2012-04-17 14:32:04