(Android studios) не удалось получить данные arduino lightsensor
Здравствуйте, это я не в состоянии к извлечение данных от 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
<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); }
Что я уже пробовал:
В основном все учебники онлайн.
Afzaal Ahmad Zeeshan
И в чем тут проблема?
dansica
Я не могу='t получать данные arduino
David Crow
Подумывали ли вы о том, чтобы перенести это на настоящий форум Android, а не в раздел "Быстрые ответы"?