将传感器数据从Android蓝牙发送到arduino时出现问题

时间:2013-09-28 14:50:32

标签: android arduino

HIII,

我是arduino和android编程的新手。 我正在尝试将传感器数据发送到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);
}

}

当我试图运行时遇到问题,Emulator说遗憾的是应用程序停止了。

我的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

1 个答案:

答案 0 :(得分:0)

189行是mmOutputStream.write(x.getBytes()); sendData()

中的语句

204行是public void onSensorChanged(SensorEvent se)。