2012-04-26 20 views
18

मैं अपने एंड्रूनो में ब्लूटूथ के माध्यम से कनेक्ट करने के लिए अपना एंड्रॉइड ऐप प्राप्त कर सकता हूं। हालांकि उनके बीच कोई डेटा प्रसारित नहीं किया जा सकता है।एंड्रॉइड + आर्डिनो ब्लूटूथ डेटा ट्रांसफर

एचटीसी एंड्रॉयड v2.2, ब्लूटूथ दोस्त सोने मॉडम, Arduino मेगा (ATmega1280)

एंड्रॉयड जावा कोड:

package com.example.BluetoothExample; 

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.os.Handler; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.EditText; 
import android.widget.Button; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Set; 
import java.util.UUID; 

public class BluetoothExampleActivity extends Activity { 
    TextView myLabel; 
    EditText myTextbox; 
    BluetoothAdapter mBluetoothAdapter; 
    BluetoothSocket mmSocket; 
    BluetoothDevice mmDevice; 
    OutputStream mmOutputStream; 
    InputStream mmInputStream; 
    Thread workerThread; 
    byte[] readBuffer; 
    int readBufferPosition; 
    int counter; 
    volatile boolean stopWorker; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button openButton = (Button)findViewById(R.id.open); 
    Button sendButton = (Button)findViewById(R.id.send); 
    Button closeButton = (Button)findViewById(R.id.close); 
    myLabel = (TextView)findViewById(R.id.label); 
    myTextbox = (EditText)findViewById(R.id.entry); 

    //Open Button 
    openButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     try { 
      findBT(); 
      openBT(); 
     } 
     catch (IOException ex) { } 
     } 
    }); 

    //Send Button 
    sendButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     try { 
      sendData(); 
     } 
     catch (IOException ex) { 
      showMessage("SEND FAILED"); 
     } 
     } 
    }); 

    //Close button 
    closeButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     try { 
      closeBT(); 
     } 
     catch (IOException ex) { } 
     } 
    }); 
    } 

    void findBT() { 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if(mBluetoothAdapter == null) { 
     myLabel.setText("No bluetooth adapter available"); 
    } 

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

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    if(pairedDevices.size() > 0) { 
     for(BluetoothDevice device : pairedDevices) { 
     if(device.getName().equals("FireFly-108B")) { 
      mmDevice = device; 
      break; 
     } 
     } 
    } 
    myLabel.setText("Bluetooth Device Found"); 
    } 

    void openBT() throws IOException { 
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard //SerialPortService ID 
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);  
    mmSocket.connect(); 
    mmOutputStream = mmSocket.getOutputStream(); 
    mmInputStream = mmSocket.getInputStream(); 
    beginListenForData(); 
    myLabel.setText("Bluetooth Opened"); 
    } 

    void beginListenForData() { 
    final Handler handler = new Handler(); 
    final byte delimiter = 10; //This is the ASCII code for a newline character 

    stopWorker = false; 
    readBufferPosition = 0; 
    readBuffer = new byte[1024]; 
    workerThread = new Thread(new Runnable() { 
     public void run() { 
     while(!Thread.currentThread().isInterrupted() && !stopWorker) { 
      try { 
      int bytesAvailable = mmInputStream.available();    
      if(bytesAvailable > 0) { 
       byte[] packetBytes = new byte[bytesAvailable]; 
       mmInputStream.read(packetBytes); 
       for(int i=0;i<bytesAvailable;i++) { 
       byte b = packetBytes[i]; 
       if(b == delimiter) { 
        byte[] encodedBytes = new byte[readBufferPosition]; 
        System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length); 
        final String data = new String(encodedBytes, "US-ASCII"); 
        readBufferPosition = 0; 

        handler.post(new Runnable() { 
        public void run() { 
         myLabel.setText(data); 
        } 
        }); 
       } 
       else { 
        readBuffer[readBufferPosition++] = b; 
       } 
       } 
      } 
      } 
      catch (IOException ex) { 
      stopWorker = true; 
      } 
     } 
     } 
    }); 

    workerThread.start(); 
    } 

    void sendData() throws IOException { 
    String msg = myTextbox.getText().toString(); 
    msg += "\n"; 
    //mmOutputStream.write(msg.getBytes()); 
    mmOutputStream.write('A'); 
    myLabel.setText("Data Sent"); 
    } 

    void closeBT() throws IOException { 
    stopWorker = true; 
    mmOutputStream.close(); 
    mmInputStream.close(); 
    mmSocket.close(); 
    myLabel.setText("Bluetooth Closed"); 
    } 

    private void showMessage(String theMsg) { 
     Toast msg = Toast.makeText(getBaseContext(), 
       theMsg, (Toast.LENGTH_LONG)/160); 
     msg.show(); 
    } 
} 

Arduino कोड: नीचे मेरी सेटअप और कोड है

#include <SoftwareSerial.h> 

int bluetoothTx = 45; 
int bluetoothRx = 47; 

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); 

void setup() { 
    //pinMode(45, OUTPUT); 
    //pinMode(47, INPUT); 
    pinMode(53, OUTPUT); 
    //Setup usb serial connection to computer 
    Serial.begin(9600); 

    //Setup Bluetooth serial connection to android 
    bluetooth.begin(115200); 
    bluetooth.print("$$$"); 
    delay(100); 
    bluetooth.println("U,9600,N"); 
    bluetooth.begin(9600); 
} 

void loop() { 
    //Read from bluetooth and write to usb serial 
    if(bluetooth.available()) { 
    char toSend = (char)bluetooth.read(); 
    Serial.print(toSend); 
    flashLED(); 
    } 

    //Read from usb serial to bluetooth 
    if(Serial.available()) { 
    char toSend = (char)Serial.read(); 
    bluetooth.print(toSend); 
    flashLED(); 
    } 
} 

void flashLED() { 
    digitalWrite(53, HIGH); 
    delay(500); 
    digitalWrite(53, LOW); 
} 

मैंने बॉड दरों के लिए 115200 और 9600 का उपयोग करने का प्रयास किया है, और मैंने ब्लूटूथ आरएक्स और टीएक्स पिन को सेट करने का प्रयास किया है इनपुट/आउटपुट और आउटपुट/इनपुट। Arduino पीसी से सीरियल डेटा प्राप्त कर रहा है लेकिन इसे एंड्रॉइड को नहीं भेज सकता (मैं इसे FlashLED() विधि के कारण देख सकता हूं)।

एंड्रॉइड Arduino के लिए कोई भी डेटा नहीं भेज सकता है। हालांकि वे दोनों जुड़े हुए हैं क्योंकि मॉडेम पर हरी रोशनी चालू हो जाती है और बंद हो जाती है और जब मैं कनेक्शन बंद करता हूं तो लाल एलईडी चमकती है। SendData() विधि अपवाद नहीं फेंकती है क्योंकि अन्यथा दिखाएं मैसेज ("विफल भेजें"); प्रदर्शित होगी।

मैं अपने प्रकट .xml में इस राशि

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> 

किसी भी मदद की बहुत सराहना की जाएगी!

कोड से लिया:

http://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/

+0

आपका कोड ठीक काम कर रहा है चरणों का एक नज़र डालें: पहले जब आप सिग्नल स्वीकार कर लेते हैं तो बाहरी डिवाइस पर कनेक्शन अनुरोध भेजते हैं, यह वापस स्वीकार सिग्नल और कनेक्शन सफल भेज देगा लेकिन हम समय-समय पर आने वाले डेटा को प्राप्त करने में सक्षम नहीं हैं। बाद में सफल कनेक्शन के कुछ समय। तो क्या आप मुझे बता सकते हैं कि आवधिक आने वाले डेटा को किसी अन्य बाहरी डिवाइस से कैसे प्राप्त किया जाए। अग्रिम धन्यवाद ... –

+0

हे हाय। मेरे पास Arduino डिवाइस है और यह मुझे मॉडबस आरटीयू डेटा भेजता है। मैं ब्लूटूथ पर उस डेटा को कैसे पढ़ सकता हूं? मैं ब्लूटूथ जोड़ी के साथ किया गया है। लेकिन जब मैं "mmInputStream.available()" लिखता हूं तो यह मुझे '0' मान देता है। कृपया जितनी जल्दी हो सके मेरी मदद करें। अग्रिम में धन्यवाद। – Lawrance

उत्तर

11

बस जो इस पेज में आए किसी और के लिए समस्या हल हो।

लगता है कि मेरे Arduino मुझे धारावाहिक संचार के लिए डिजिटल पिन का उपयोग कर पसंद नहीं है, मैं TX और RX का उपयोग करने के बजाय इस कोड http://jondontdoit.blogspot.com.au/2011/11/bluetooth-mate-tutorial.html से लिया साथ, यह भी लगता है कि 9600 115200.

/*********************** 
Bluetooth test program 
***********************/ 
//TODO 
//TEST THIS PROGRAM WITH ANDROID, 
//CHANGE PINS TO RX AND TX THO ON THE ARDUINO! 
//int counter = 0; 
int incomingByte; 

void setup() { 
    pinMode(53, OUTPUT); 
    Serial.begin(9600); 
} 

void loop() { 
    // see if there's incoming serial data: 
    if (Serial.available() > 0) { 
    // read the oldest byte in the serial buffer: 
    incomingByte = Serial.read(); 
    // if it's a capital R, reset the counter 
    if (incomingByte == 'g') { 
     digitalWrite(53, HIGH); 
     delay(500); 
     digitalWrite(53, LOW); 
     delay(500); 
     //Serial.println("RESET"); 
     //counter=0; 
    } 
    } 

    //Serial.println(counter); 
    //counter++; 

    //delay(250); 
} 
के बजाय एक अच्छा बॉड है
-1

मुझे लगता है कि यह ब्लूटूथ में कुछ गलती हो सकती है .. इसके ड्राइवरों को दोबारा स्थापित करना बेहतर है .. जैसा ऊपर दिया गया कोड सही दिखता है।

0

मैं केवल इस खंड बदलने के बाद इस चलाने के लिए प्राप्त करने में सक्षम था:

Set<BluetoothDevice> pairedDevices = BluetoothAdapter.getBondedDevices(); 
if(pairedDevices.size() > 0) 
    { 
     for(BluetoothDevice device : pairedDevices) 
     { 
      if(device.getName().startsWith("FireFly-")) 
      { 
       mmDevice = device; 
       Log.d("ArduinoBT", "findBT found device named " + mmDevice.getName()); 
       Log.d("ArduinoBT", "device address is " + mmDevice.getAddress()); 
       break; 
      } 
     } 
    } 
इस के साथ

:

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
mmDevice = mBluetoothAdapter.getRemoteDevice("00:06:66:46:5A:91"); 
if (pairedDevices.contains(mmDevice)) 
    { 
     statusText.setText("Bluetooth Device Found, address: " + mmDevice.getAddress()); 
     Log.d("ArduinoBT", "BT is paired"); 
    } 

जहां मैं अपने ब्लूटूथ डिवाइस का पता दर्ज किया है। मूल कोड डिवाइस को पाता है और सही पता देता है, लेकिन mmSocket.connect(); एक अपवाद उत्पन्न करता है "java.io.IOException: सेवा खोज विफल"

सुझाव?

+0

यकीन नहीं क्षमा करें :( –

3

मुझे वही चीज़ मिल रही थी। मैं 'सेटिंग्स' -> 'वायरलेस और नेटवर्क' -> 'ब्लूटूथ सेटिंग्स' में गया और डिवाइस को जोड़ा। जब मैं वापस गया और अपना कोड फिर से चलाया, यह जुड़ा हुआ, कोई अपवाद नहीं। मैंने युग्मित उपकरणों को प्रदर्शित करने के लिए अपने यूआई में नियंत्रण डाला, मैं यह देखने जा रहा हूं कि मैं अपने यूआई से युग्मन उपकरणों को प्रबंधित करने के लिए कोड कर सकता हूं या नहीं।

0

शून्य करने के लिए किसी को भी इस पेज पाता है, लेकिन जैसा कि ऊपर का उपयोग कर एक हार्डकोडेड मैक पते अटक गई है, सेट मैक पते के लिए, और OnResume()

try{ 
File f = new File(Environment.getExternalStorageDirectory()+"/mac.txt"); 
FileInputStream fileIS = new FileInputStream(f); 
buf = new BufferedReader(new InputStreamReader(fileIS)); 
String readString = new String(); 
while((readString = buf.readLine())!= null){ 
address = readString; 
} 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e){ 
e.printStackTrace(); 
} 

भी इस कोड डालें, करने के लिए मत भूलना ग्रहण को आवश्यक पुस्तकालयों को शामिल करने की अनुमति दें, और अपने मैक पते को एसडी कार्ड की रूट पर mac.txt में रखें, फिर आप उपयोगकर्ताओं को अपने मैक पते के साथ एक टेक्स्ट फ़ाइल दे सकते हैं जबकि अभी भी ऐप को अनुकूलित करने के बिना बाजार से डाउनलोड करने की इजाजत दे रही है हर उदाहरण।

0

@ बैकवर्डडेव सिर्फ उत्सुकता के लिए, 45 और 46 पिन से कनेक्ट करने का प्रयास करें और इस सरल कोड का उपयोग करें। मैं इसका इस्तेमाल करता हूं और कोई समस्या नहीं है। आप Arduino सीरियल मॉनिटर से डेटा भेजने और वहां पढ़ने के लिए सक्षम हो जाएगा।

/* 
Pinout: 
45 --> BT module Tx 
46 --> BT module Rx 
*/ 
#include <SoftwareSerial.h> 

SoftwareSerial mySerial(45, 46); // RX, TX 

void setup() 
{ 
    // Open serial communications and wait for port to open: 
    Serial.begin(9600); 


    Serial.println("I am ready to send some stuff!"); 

    // set the data rate for the SoftwareSerial port 
    mySerial.begin(9600); 
} 

void loop() // run over and over 
{ 
    if (mySerial.available()) 
    Serial.write(mySerial.read()); 
    if (Serial.available()) 
    mySerial.write(Serial.read()); 
} 

इसके अलावा, आप Arduino के लिए क्या ब्लू टूथ शील्ड का उपयोग कर रहे हैं? कोर्ट-06?

संपादित

बस Mega2560 (1280 की जरूरत नहीं है) के साथ यह परीक्षण किया है और यह कोई समस्या नहीं के साथ काम करता है।

मुझे विश्वास है कि समस्या पिनआउट के साथ थी।

आपकी प्रतिक्रिया

0

के लिए प्रतीक्षा कर आप अभी भी एक जवाब के लिए देख रहे हैं, बदल रहा है सॉफ्टवेयर धारावाहिक पिन का प्रयास करें। यह आपके द्वारा उपयोग की जा रही लाइब्रेरी का एक प्रसिद्ध सीमा है।

मेगा समर्थन परिवर्तन पर सभी पिन नहीं हैं, इसलिए केवल निम्नलिखित का उपयोग आरएक्स: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, ए 8 (62), ए 9 के लिए किया जा सकता है (63), ए 10 (64), ए 11 (65), ए 12 (66), ए 13 (67), ए 14 (68), ए 15 (69)। Refs

उम्मीद है कि इससे मदद मिलती है।

संबंधित मुद्दे