2013-03-06 65 views
1

因此,對於我的研究,我必須將加速計數據作爲恆定流發送到arduino mega。我已經通過串口將模塊連接到了arduino。但是,當我運行代碼時,它只運行一次。我試圖將代碼中的藍牙連接部分放在我的代碼的準確性更改部分,但它會凍結設備。這裏是我的代碼:從Arduino的藍牙只發送一次傳感器數據

package com.example.arduino_bluetooth2; 

//================================================================================================= 
//Imports 
//================================================================================================= 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Set; 
import java.util.UUID; 
import android.os.Bundle; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothServerSocket; 
import android.bluetooth.BluetoothSocket; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.view.Menu; 
import android.widget.TextView; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 

public class MainActivity extends Activity implements SensorEventListener { 

    // Setup necessary sensor objects 
    private Sensor acc; 
    private SensorManager sm; 
    private TextView t1; 
    private double value; 
    // Bluetooth Object 
    private BluetoothAdapter bAdapter; 
    private BluetoothDevice device; 
    private BluetoothSocket mmServerSocket; 
    private OutputStream btoutput; 
    private static final UUID SPP_UUID = UUID 
      .fromString("00001101-0000-1000-8000-00805F9B34FB"); 
    private static final int DISCOVERY_REQUEST = 1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     accelerometer_initialization(); 
     bluetooth_initialization(); 
    } 

    // Setsup the accelerometer object 
    private void accelerometer_initialization() { 
     sm = (SensorManager) getSystemService(SENSOR_SERVICE); 
     acc = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     sm.registerListener(this, acc, SensorManager.SENSOR_DELAY_NORMAL); 
    } 

    // Setup bluetooth object 
    private void bluetooth_initialization() { 
     bAdapter = BluetoothAdapter.getDefaultAdapter(); 
     startActivityForResult(new Intent(
       BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE), 
       DISCOVERY_REQUEST); 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     registerReceiver(mReceiver, filter); 
     bAdapter.startDiscovery(); 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     value = event.values[0]; 
    } 

    @Override 
    public void onAccuracyChanged(Sensor arg0, int arg1) { 
    } 

    final BroadcastReceiver mReceiver = new BroadcastReceiver() { 

     public void onReceive(Context context, Intent intent) { 
      if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) { 
       device = intent 
         .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       if (new String(device.getName()).equals("BT UART")) { 
        bAdapter.cancelDiscovery(); 

        try { 
         BluetoothSocket test = null; 
         test = device 
           .createInsecureRfcommSocketToServiceRecord(SPP_UUID); 
         mmServerSocket = test; 
         mmServerSocket.connect(); 
         String message = Double.toString(value); 
         byte[] send = message.getBytes(); 
         btoutput = mmServerSocket.getOutputStream(); 
         btoutput.write(send); 
         btoutput.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 
     }; 
    }; 
} 

回答

-1

看看這個頁面:http://arduino.cc/en/Reference/Loop 的問題是,因爲它不是在永遠繼續,直到設備被關閉或以其他方式講了一個循環,那就只一次。

0

我不確定你應該在廣播接收器中創建並連接藍牙套接字。我在活動的onResume()中進行藍牙連接管理。

此外,我使用一個線程來管理從Arduino和設備之間的串行數據連接獲取數據,它被衍生並在後臺連續運行。還有就是要向外發送數據,我從活動

/* Call this from the main activity to send data to the remote device */ 
    public void write(String message) { 
     System.out.println("...Data to send: " + message + "..."); 
     byte[] msgBuffer = message.getBytes(); 
     try { 
      mmOutStream.write(msgBuffer); 
     } catch (IOException e) { 
      System.out.println("...Error data send: " + e.getMessage() + "...");  
      } 
    } 

則胎面的run()方法調用寫入方法負責獲取數據的回

見我的回答了這一主題爲例 Error with receiving xml strings via bluetooth in Android

祝你好運!