2013-09-28 5 views
1

습니다 고지 난 ARDUINO 안드로이드 프로그래밍 새롭다트러블

를 ARDUINO한다. 센서 데이터를 arduino로 보내려고합니다. 즉, 휴대 전화를 기울이면 블루투스를 통해 값을 전송해야합니다. 내 코드는 내가이를 실행하려고 할 때 내가 문제를 가지고

public class MainActivity extends Activity implements SensorEventListener 
{ 

BluetoothAdapter mBluetoothAdapter; 
BluetoothSocket mmSocket; 
BluetoothDevice mmDevice; 
OutputStream mmOutputStream; 
float gravity[]=new float[3]; 
float x2,y2,z2,y1; 
long actualtime,lastupdate=0; 
int i1=0; 
char move='o'; 
TextView t; 


SensorManager sm; 
Sensor s; 

@Override 
public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button openButton = (Button)findViewById(R.id.open); 
    Button closeButton = (Button)findViewById(R.id.close); 
    t=(TextView)findViewById(R.id.tV1); 
    sm=(SensorManager)getSystemService(SENSOR_SERVICE); 
    s=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 




    openButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      try 
      { 
       findBT(); 
       openBT(); 
      } 
      catch (IOException ex) { } 
     } 
    }); 

    //Close button 
    closeButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      try 
      { 
       closeBT(); 
      } 
      catch (IOException ex) { } 
     } 
    }); 
} 

void findBT() 
{ 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if(mBluetoothAdapter == null) 
    { 

    } 

    if(!mBluetoothAdapter.isEnabled()) 
    { 
     Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivityForResult(enableBluetooth, 0); 
    } 

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    if(pairedDevices.size() > 0) 
    { 
     for(BluetoothDevice device : pairedDevices) 
     { 
      if(device.getName().equals("RN42-5350")) 
      { 
       mmDevice = device; 
       break; 
      } 
     } 
    } 

} 

void openBT() throws IOException 
{ 
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID 
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);   
    mmSocket.connect(); 
    mmOutputStream = mmSocket.getOutputStream(); 





} 



void sendData(String x) throws IOException 
{ 


    mmOutputStream.write(x.getBytes()); 

} 

void closeBT() throws IOException 
{ 

    mmOutputStream.close(); 

    mmSocket.close(); 

} 

@Override 
public void onAccuracyChanged(Sensor arg0, int arg1) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onSensorChanged(SensorEvent se) 
{ 
    final float alpha = (float)0.8; 


     gravity[0] = alpha * gravity[0] + (1 - alpha) * se.values[0]; 
     gravity[1] = alpha * gravity[1] + (1 - alpha) * se.values[1]; 
     gravity[2] = alpha * gravity[2] + (1 - alpha) * se.values[2]; 


     x2= se.values[0] - gravity[0]; 
     y2= se.values[1] - gravity[1]; 
     z2= se.values[2] - gravity[2]; 


     actualtime=System.currentTimeMillis(); 

     if(actualtime-lastupdate>170) 
     { 
      if((y2-y1)>=2.5) 
      { 
       if(move=='a') 
       { 
        move='o'; 
       } 
       else 
       { 
        move='s'; 
       } 
      } 

      if((y1-y2)>=2.5) 
      { 
       if(move=='s') 
       { 
        move='o'; 
       } 
       else 
       { 
        move='a'; 
       } 
      } 
     lastupdate=actualtime; 
     y1=y2; 

     } 

     try { 
     sendData(Character.toString(move)); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

protected void onPause() 
{ 
    super.onPause(); 
    sm.unregisterListener(this); 
} 

protected void onResume() 
{ 
    super.onResume(); 
    sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL); 
} 

}

이다, 에뮬레이터는 불행하게도 응용 프로그램이 멈췄다 말한다.

내 로그 캣 정보는 다음과 같습니다

08-17 16:24:06.438: D/AndroidRuntime(15637): Shutting down VM 

08-17 16:24:06.438: W/dalvikvm(15637): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 

08-17 16:24:06.448: E/AndroidRuntime(15637): FATAL EXCEPTION: main 

08-17 16:24:06.448: E/AndroidRuntime(15637): java.lang.NullPointerException 

08-17 16:24:06.448: E/AndroidRuntime(15637): at com.example.newbt.MainActivity.sendData            (MainActivity.java:189) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at com.example.newbt.MainActivity.onSensorChanged(MainActivity.java:267) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at android.hardware.SystemSensorManager$ListenerDelegate$1.handleMessage(SystemSensorManager.java:204) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at android.os.Handler.dispatchMessage(Handler.java:99) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at android.os.Looper.loop(Looper.java:137) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at android.app.ActivityThread.main(ActivityThread.java:5041) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at java.lang.reflect.Method.invokeNative(Native Method) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at java.lang.reflect.Method.invoke(Method.java:511) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 

08-17 16:24:06.448: E/AndroidRuntime(15637): at dalvik.system.NativeStart.main(Native Method) 


i hope that the problem is at sendData(), kindly help me out of this problem 
+0

에뮬레이터에서 블루투스는 어떻게 작동합니까? – Metalhead1247

+0

나는 내 전화로 그것을 시도하고 동일한 메시지가있어. – wudpecker

+0

은 mainactivity의 189 및 204 라인입니까? – Metalhead1247

답변

0

189 선 mmOutputStream.write입니다 (x.getBytes()); sendData()에있는 문장

204 줄은 public void onSensorChanged (SensorEvent se)입니다.