How to connect an Android phone and Ardunio with Bluetooth

I want to connect an Android phone and an Arduino Mega 2560 with bluetooth (JY-MCU) to open or close the LED. Here is 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 an 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 fine. But that will not work. Help me please.

+4
source share
4 answers

Do you know blueArduıno? You can try and test your program and bluetooth devyce to see where the problem is.

+2
source
 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()); } } 

these are my connection methods. Since the android debugging result is normal, I thought the arduino code has any problems or my bluetooth device. How can I understand where the problem is?

+1
source

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

Download a widely available Bluetooth chat source

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 the Android market.

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

Subsequently, you can read the example and get an idea of ​​using bluetooth api.

0
source

You can do this using bluetoth spp from the Android market or Google bluetooth sample code (bluetoothchat) to check if there are problems with Android or arduino code.

0
source

Source: https://habr.com/ru/post/1500257/


All Articles