2016-04-08 19 views
1

Android'e ve ben arduino herhangi okumaları alamadım Arduino kodu:gönderme veri Bluetooth Modülü HC-05 aracılığıyla bir Android uygulaması için arduino veri göndermek için çalışıyorlar

void setup() { 
    Serial.begin(9600); 

} 

void loop() 
{ 
    char c; 
if(Serial.available()) 
    { 
    c = Serial.read(); 
    Serial.print(c); 
    } 
} 

Android kodudur:

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.os.Handler; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Set; 
import java.util.UUID; 

public class MainActivity extends Activity { 
    // private final String DEVICE_NAME="MyBTBee"; 
    private final String DEVICE_ADDRESS="30:14:12:18:01:38"; 
    private final UUID PORT_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");//Serial Port Service ID 
    private BluetoothDevice device; 
    private BluetoothSocket socket; 
    private OutputStream outputStream; 
    private InputStream inputStream; 
    Button startButton, sendButton,clearButton,stopButton; 
    TextView textView; 
    EditText editText; 
    boolean deviceConnected=false; 
    Thread thread; 
    byte buffer[]; 
    int bufferPosition; 
    boolean stopThread; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     startButton = (Button) findViewById(R.id.buttonStart); 
     sendButton = (Button) findViewById(R.id.buttonSend); 
     clearButton = (Button) findViewById(R.id.buttonClear); 
     stopButton = (Button) findViewById(R.id.buttonStop); 
     editText = (EditText) findViewById(R.id.editText); 
     textView = (TextView) findViewById(R.id.textView); 
     setUiEnabled(false); 

    } 

    public void setUiEnabled(boolean bool) 
    { 
     startButton.setEnabled(!bool); 
     sendButton.setEnabled(bool); 
     stopButton.setEnabled(bool); 
     textView.setEnabled(bool); 

    } 

    public boolean BTinit() 
    { 
     boolean found=false; 
     BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); 
     if (bluetoothAdapter == null) { 
      Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show(); 
     } 
     if(!bluetoothAdapter.isEnabled()) 
     { 
      Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableAdapter, 0); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     Set<BluetoothDevice> bondedDevices = bluetoothAdapter.getBondedDevices(); 
     if(bondedDevices.isEmpty()) 
     { 
      Toast.makeText(getApplicationContext(),"Please Pair the Device first",Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 
      for (BluetoothDevice iterator : bondedDevices) 
      { 
       if(iterator.getAddress().equals(DEVICE_ADDRESS)) 
       { 
        device=iterator; 
        found=true; 
        break; 
       } 
      } 
     } 
     return found; 
    } 

    public boolean BTconnect() 
    { 
     boolean connected=true; 
     try { 
      socket = device.createRfcommSocketToServiceRecord(PORT_UUID); 
      socket.connect(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      connected=false; 
     } 
     if(connected) 
     { 
      try { 
       outputStream=socket.getOutputStream(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       inputStream=socket.getInputStream(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 


     return connected; 
    } 

    public void onClickStart(View view) { 
     if(BTinit()) 
     { 
      if(BTconnect()) 
      { 
       setUiEnabled(true); 
       deviceConnected=true; 
       beginListenForData(); 
       textView.append("\nConnection Opened!\n"); 
      } 

     } 
    } 

    void beginListenForData() 
    { 
     final Handler handler = new Handler(); 
     stopThread = false; 
     buffer = new byte[1024]; 
     Thread thread = new Thread(new Runnable() 
     { 
      public void run() 
      { 
       while(!Thread.currentThread().isInterrupted() && !stopThread) 
       { 
        try 
        { 
         int byteCount = inputStream.available(); 
         if(byteCount > 0) 
         { 
          byte[] rawBytes = new byte[byteCount]; 
          inputStream.read(rawBytes); 
          final String string=new String(rawBytes,"UTF-8"); 
          handler.post(new Runnable() { 
           public void run() 
           { 
            textView.append(string); 
           } 
          }); 

         } 
        } 
        catch (IOException ex) 
        { 
         stopThread = true; 
        } 
       } 
      } 
     }); 

     thread.start(); 
    } 

    public void onClickSend(View view) { 
     String string = editText.getText().toString(); 
     string.concat("\n"); 
     try { 
      outputStream.write(string.getBytes()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     textView.append("\nSent Data:"+string+"\n"); 

    } 

    public void onClickStop(View view) throws IOException { 
     stopThread = true; 
     outputStream.close(); 
     inputStream.close(); 
     socket.close(); 
     setUiEnabled(false); 
     deviceConnected=false; 
     textView.append("\nConnection Closed!\n"); 
    } 

    public void onClickClear(View view) { 
     textView.setText(""); 
    } 
} 

cevap

0

Ben şu anda gördüğünüz ilk sorunun Arduino kodu vardır: Sen olmuyorsun giriş ve çıkış için aynı Seriyi kullandığınız için mesajı Bluetooth üzerinden gönderme. Serial1 kullanarak Arduino Mega kullanıyorsanız bunu düzeltin (TX1 ve RX1'i kullanma). Bir Mega kullanmıyorsanız, yeni bir özel Seri oluşturmanıza izin veren SerialSoftware kitaplığını kullanmayı düşünmelisiniz.

Arduino kurulumunuzu, onu nasıl kurduğunuzu görmek için görmek güzel olurdu.

Size yardımcı olan herhangi bir soru sormaktan çekinmeyin.

+0

ya bunu çözdüm ve düzeltmelerimi yaptım ... şimdi güzel çalışıyor .... teşekkürler kardeşim –

+0

Sorun yok;). –

İlgili konular