Click here to Skip to main content
15,037,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 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.
Posted
Updated 25-Nov-18 4:06am
v2
Comments
Afzaal Ahmad Zeeshan 25-Nov-18 9:55am
   
And the problem with this is?
dansica 25-Nov-18 10:04am
   
I cant='t receive arduino data
David Crow 26-Nov-18 13:51pm
   
Have you considered moving this to the actual Android forum, rather than the "quick answers" section?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900