无法获取Arduino传感器数据

时间:2018-11-25 14:49:57

标签: java android arduino

我无法从Arduino检索数据。我的代码有什么问题吗?我尝试了许多不同的方法来从Arduino获取数据,但仍然无济于事。

Android Studio代码

package com.example.a17019501.bluetootharduino;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    //Layout//
    TextView adapterStatus;
    TextView lightValue;

    //Bluetooth//
    BluetoothAdapter mBluetoothAdapter;
    BluetoothDevice mDevice;
    Set <BluetoothDevice> pairedDevices;
    ConnectThread mConnectThread;
    ConnectedThread mConnectedThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapterStatus = findViewById(R.id.tvAdapterStatus);
        lightValue = findViewById(R.id.tvLightValue);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (mBluetoothAdapter == null) {
            adapterStatus.setText("Device does not support bluetooth");
        }
        else {
            adapterStatus.setText("Device support bluetooth");
        }
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 1);
        }
        pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                mDevice = device;
            }
        }
        mConnectThread = new ConnectThread(mDevice);
        mConnectThread.start();
        Log.i(TAG, "Connect to device: " + mDevice.getName());
    }
    public class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;
        private final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
        public ConnectThread(BluetoothDevice device) {
            BluetoothSocket tmp = null;
            mmDevice = device;
            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) { }
            mmSocket = tmp;
        }
        public void run() {
            mBluetoothAdapter.cancelDiscovery();
            try {
                mmSocket.connect();
            } catch (IOException connectException) {
                try {
                    mmSocket.close();
                } catch (IOException closeException) { }
                return;
            }
            mConnectedThread = new ConnectedThread(mmSocket);
            mConnectedThread.start();
        }
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) { }
        }
    }
    public class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInstream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.i(TAG, "Connected Thread");

            mmSocket = socket;
            InputStream tmpIn = null;

            try {
                tmpIn = mmSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mmInstream = tmpIn;
        }

        public void run() {
            byte[] buffer = new byte[1024];

            int bytes;

            while (true) {
                try {
                    bytes = mmInstream.read(buffer);
                    Log.i(TAG, "Buffer read");
                    String incomingMsg = new String(buffer, 0, bytes);
                    mHandler.obtainMessage(1, bytes, -1, buffer);
                    Log.i(TAG, "InputStream " + incomingMsg);
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.i(TAG, "Error reading input " + e.getMessage());
                }
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {

            }
        }
    }
    Handler mHandler = new Handler() {
        String s;

        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case 1:
                    s = (String) msg.obj;
                    if (s!=null) {
                        lightValue.setText(Integer.parseInt(s));
                    }
                    break;
            }
        }
    };
}`

Arduino代码

#include <SoftwareSerial.h>

const int LightSensor = A1; //LightSensor pin at A1

int bluetoothTx = 2; //BTTX at pin 2
int bluetoothRx = 3; //BTRX at pin 3
int sensorValue = 0;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup() {
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop() {
  sensorValue = analogRead(LightSensor);
  bluetooth.print(sensorValue);
  Serial.println(sensorValue);
  delay(2500);
}

0 个答案:

没有答案