2013-10-17 3 views
1

내 안드로이드에서 내 arduino로 바이트를 보내려는 중이므로 연결되어있는 LED가 켜지거나 꺼집니다. 내 안드로이드 응용 프로그램을 열면 immediatly "응답하지 않는 메시지"로 이동하고 종료됩니다.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; 
} 
} 

로그 캣 :

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 
+1

게시하여 로그 캣을. 대부분 nullpointer 예외가 있거나 Bluetooth 권한을 요청하지 않은 것 같습니다. – 323go

+0

편집 : 게시 된 LogCat. – Scale

답변

2

이 초기화되기 전에 당신은 btSocket 변수를 사용하고 있습니다. 당신의에서 onCreate 방법에 이 넣어보십시오 :이 후

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(); 
} 
+0

그것은 일했다! 이제 앱이 시작됩니다. 감사합니다. – Scale

+0

Diego의 대답을 upvote하고 동의하는 것을 잊지 마십시오! – 323go

+0

@ DiegoSuárez 나는 비슷한 질문을 게시했습니다. http://stackoverflow.com/questions/23250806/trouble-connecting-android-with-bluetooth 저를 도울 수 있다고 생각하십니까? – user1300788