1

やぁ、

私はarduinoとandroidのプログラミングが初めてです。センサーデータをarduinoに送信しようとしています。つまり、電話を傾けた場合、Bluetooth経由で値を送信する必要があります。私のコードは

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

}

これを実行しようとしたときに問題が発生しました。エミュレーターは、残念ながらアプリケーションが停止したと言います。

私のlogcat情報は次のとおりです。

 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
4

1 に答える 1

0

189 行目は mmOutputStream.write(x.getBytes()); です。sendData() のステートメント

204 行は public void onSensorChanged(SensorEvent se) です。

于 2013-09-30T07:36:56.933 に答える