Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.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
Java 蓝牙只发送一次传感器数据_Java_Android_Bluetooth_Arduino_Accelerometer - Fatal编程技术网

Java 蓝牙只发送一次传感器数据

Java 蓝牙只发送一次传感器数据,java,android,bluetooth,arduino,accelerometer,Java,Android,Bluetooth,Arduino,Accelerometer,所以在我的研究中,我必须将加速度计数据以恒定流的形式发送到arduino mega。我将模块通过串行总线连接到arduino。但是,当我运行代码时,它只运行一次。我试图将蓝牙连接部分的代码放在我的密码中,但它一直冻结设备。这是我的密码: package com.example.arduino_bluetooth2; //========================================================================================

所以在我的研究中,我必须将加速度计数据以恒定流的形式发送到arduino mega。我将模块通过串行总线连接到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();
                    }
                }
            }
        };
    };
}

从arduino查看此页面:
问题是,它只运行一次,因为它不是一个循环,直到设备关闭或被告知其他情况,才会一直持续。

我不确定您是否应该在广播接收器中创建并连接蓝牙插座。我在活动的onResume()中进行蓝牙连接管理

我还使用了一个线程来管理从arduino和设备之间的串行数据连接获取数据,它是派生出来的,并在后台连续运行。有一个write方法将我从活动调用的数据发送出去

    /* 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() + "...");     
          }
    }
然后,tread的run()方法负责返回数据

有关示例,请参见本文中的答案

祝你好运