Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android蓝牙向arduino发送传感器数据时出现故障_Android_Arduino - Fatal编程技术网

从android蓝牙向arduino发送传感器数据时出现故障

从android蓝牙向arduino发送传感器数据时出现故障,android,arduino,Android,Arduino,嗨 我不熟悉arduino和android编程。 我正在尝试将传感器数据发送到arduino,即如果我倾斜手机,它将通过蓝牙发送值。我的代码是 public class MainActivity extends Activity implements SensorEventListener { BluetoothAdapter mBluetoothAdapter; BluetoothSocket mmSocket; BluetoothDevice mmDevice; OutputStream

我不熟悉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);
}

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


204行在SensorChanged(传感器事件se)上为公共无效

Bluetooth在Emulator上如何工作?我在手机上试用了它,得到了相同的消息。mainactivity中的第189行和第204行是什么?
 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