4
votes

I want to connect an Android phone and an Arduino Mega 2560 with bluetooth (JY-MCU) to open or close LED. Here's my Arduino code:

#include <SoftwareSerial.h>   

#define arduinoRx 2
#define arduinoTx 3 

int gelen_veri;
int LedCikis = 8;

SoftwareSerial bluetooth(arduinoRx,arduinoTx);    

void setup()
{
    bluetooth.begin(9600);
}

void loop()
{
    if(bluetooth.available()>0)   
    {
        gelen_veri=bluetooth.read();    
        switch(gelen_veri)
        {
            case 'A' :
                digitalWrite(LedCikis,HIGH);
                break;
            case 'K' :
                digitalWrite(LedCikis,LOW);
                break;
            default:
                break;
        }
    }
}

In addition I have Android code:

onlight.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        try {
            // String msg = "A\n";
            // mmOutputStream.write(msg.getBytes()); // transmitter nesnemize 'A' karakterini ilettik.
            mmOutputStream.write('A');
        } catch (IOException ex) {
            Log.e("hata", ex.getMessage());
        }
    }
});

offlight.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        try {
             mmOutputStream.write('K'); // aynı şekilde transmitter nesnemize 'K' karakterini ilettik.
         } catch (IOException ex) {}
     }
 });
}

When I debug my Android code everything is normal. But it doesn't work. Help me please.

4

4 Answers

2
votes

Do you know blueArduıno? You can try and test your program & bluetooth devıce to understand where is the problem.

1
votes
void findDevice() { 
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBluetooth, 0);
 }

final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); //daha önceden eşleşmiş cihazların listesi alındı

if (pairedDevices.size() > 0) {
    for (BluetoothDevice device : pairedDevices) {
        if (device.getName().equals("HC-06")) // JY MCU ; bizim bluetooth modulumuzun default ismi.
        {
            mmDevice = device; // JY-MCU bizim mmDevice nesnesimiz oldu .
            break;
        }
    }
  myLabel.setText("Bluetooth Device Found");
 }
}

And

void connectBT() throws IOException {
try {
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice("20:13:05:06:54:98");
        // Benim bluetooth modulumun MAC adresi.


    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); 
    // Standard UUID. Çok büyük ihtimalle sizinde alacağınız modulün UUID numarası aynı olacaktır


    mmSocket = device.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();
 } catch (IOException e) {
     Log.d("BLUETOOTH_CLIENT", e.getMessage());
    }
 }

they are my connection methods. As debug result of android is normal, i thought that arduino code has any problem or my bluetooth device. How can i understand where is the problem???

0
votes

If you are using your phone did you use any bluetooth api ? Anyway you can try the following

Download Bluetooth chat source which is widely available

https://www.google.com.sg/search?q=bluetooth+chat+&oq=bluetooth+chat+&aqs=chrome..69i57j0l3.2172j0&sourceid=chrome&ie=UTF-8#q=bluetooth+chat+source

OR use bluetooth SPP from android market

Install and test by sending characters from the send message to test for connectivity .

Subsequently you can read through the example and get a feel of using the bluetooth api .

0
votes

You can do so by using bluetoth spp from android market or google bluetooth sample code (bluetoothchat) to test whether e issues is with android code or arduino.