本文介绍了(Android工作室)无法获得arduino lightsensor数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

您好,我无法从Arduino 检索数据。我的代码有什么问题吗?谢谢!





Android Studio Code



Hello, I am unable to retrieve the data from Arduino. Is there anything wrong with my code? Thank You!


Android Studio Code

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代码



Arduino Code

<pre>#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(115200);  // The Bluetooth Mate defaults to 115200bps
//  bluetooth.print("$");  // Print three times individually
//  bluetooth.print("$");
//  bluetooth.print("$");  // Enter command mode
//  delay(100);  // Short delay, wait for the Mate to send back CMD
//  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
//  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

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





我的尝试:



大部分在线教程。



What I have tried:

Mostly all the tutorials online.

推荐答案







我尝试过:



主要是所有在线教程。



What I have tried:

Mostly all the tutorials online.


这篇关于(Android工作室)无法获得arduino lightsensor数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:47