2013-10-17 144 views
1

我試圖從我的android發送字節到我的arduino,使它關閉/連接到它的led。當我打開我的android應用程序時,它立即轉到「無響應消息」並關閉。發佈LogCat以防萬一。使用Android + Arduino進行藍牙通信。 (僅將數據發送到Arduino)

這是Android代碼:

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; 
} 
} 

的logcat:

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 

回答

0

我看到兩個問題:

  • 分配btSocketbtSocketoutputstream訪問(你必須打開後訪問它);這是the NullPointerException
  • 原因,你應該封裝外onCreate()onClick()

希望這有助於在一個單獨的線程中的任何I/O操作。