2015-07-21 100 views
0

我想通過藍牙模塊HC-05交換在Arduino和Android手機之間的數據,anenter圖像說明hered從3-4天我是無法識別套管以下錯誤:嘗試連接藍牙模塊HC-05與Android程序中的arduino連接時出錯

([JSR82] connect: Connection is not created (failed or aborted).) occurred while try to connect with already paired bluetooth module using its MAC address.

我還張貼我的MainActivity.javaactivity_main.xml文件和arduino代碼了。

主要活動的java文件: -

package bluetooth.majorproject.bt; 



import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Set; 


public class MainActivity extends Activity { 
    //Button btnScan; 
    ListView lvDevices; 

    ArrayAdapter<String> listAdapter; 
    BluetoothAdapter adapter; 

    ArrayList<String> pairedDevices; 

    BroadcastReceiver receiver; 
    IntentFilter filter;; 

    Set<BluetoothDevice> devicesArray; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     adapter=BluetoothAdapter.getDefaultAdapter(); 
     if(adapter==null){ 
      Log.d("Bt","This device doesnot support bluetooth"); 
      finish(); 
     } 
     else if(!adapter.enable()){ 
      turnOnBt(); 
     } 

     init(); 
     getPairedDevices(); 
     startDiscovery(); 

    } 

    private void init() { 
     lvDevices=(ListView)findViewById(R.id.lvDevices); 
     // btnScan=(Button)findViewById(R.id.btnScan); 

     listAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,0); 
     lvDevices.setAdapter(listAdapter); 
     adapter=BluetoothAdapter.getDefaultAdapter(); 

     startDiscovery(); 
     pairedDevices=new ArrayList<String>(); 
     filter=new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     registerReceiver(receiver,filter); 
     receiver=new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action=intent.getAction(); 
       if(BluetoothDevice.ACTION_FOUND.equals(action)){ 
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 

        Log.d("Bt","Device Found :"+device.getName()); 
        String strTmp=""; 
        for(int a=0;a<pairedDevices.size();a++){ 
         if(device.getAddress().equals(pairedDevices.get(a))){ 
          strTmp="(Paired)"; 
          break; 
         } 

        } 
        //PA to writing 
        listAdapter.add(device.getName()+" "+device.getAddress()); 
        lvDevices.setAdapter(listAdapter); 


       }else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){ 
        Log.d("Bt", " Scanning Devices.. "); 

       }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){ 
        adapter.cancelDiscovery(); 
        if(!devicesArray.isEmpty()){ 
         lvDevices.setAdapter(listAdapter); 
        }else { 
         Log.d("Bt","Devices Not Found!!"); 
        } 
       }else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){ 
        if(adapter.getState() == adapter.STATE_OFF) { 
         Log.d("Bt","Bluetooth must be enabled to run this app.!!"); 
        } 

       } 
      } 
     }; 

     filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
     registerReceiver(receiver,filter); 
     filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     registerReceiver(receiver,filter); 
     filter=new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); 
     registerReceiver(receiver,filter); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     unregisterReceiver(receiver); 
    } 

    private void startDiscovery() { 
     if(adapter.isDiscovering()) 
      adapter.cancelDiscovery(); 
     adapter.startDiscovery(); 
    } 

    private void getPairedDevices() { 
     devicesArray=adapter.getBondedDevices(); 
     if(devicesArray.size()>0){ 
      for(BluetoothDevice device:devicesArray) { 
       pairedDevices.add(device.getName() + " " + device.getAddress()); 
       listAdapter.add(device.getName()+"\n"+device.getAddress()); 
      } 
     } 
     lvDevices.setAdapter(listAdapter); 

    } 

    private void turnOnBt() { 
     Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
     startActivity(intent); 
    } 
} 

activity_main xml文件: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
    android:orientation="vertical" 
    android:weightSum="1"> 


    <Button 
     android:id="@+id/turnBTOn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#000099" 
     android:text="Turn Bt On" 
     android:textColor="#ffffff" /> 

    <Button 
     android:id="@+id/Connect" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#83FF83" 
     android:enabled="false" 
     android:text="Connect" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btnLedOn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="#00ff00" 
     android:enabled="false" 
     android:text="Turn On Led" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btnLedOff" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:background="#FF0000" 
     android:enabled="false" 
     android:text="Turn Off Led" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/turnBTOff" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#000099" 
     android:text="Turn Bt Off" 
     android:textColor="#ffffff" /> 
</LinearLayout> 

Arduino的代碼.ino文件: -

/* Bluetooth control of Robot via inputs from Android phone via custom designed android app */ 

unsigned char myByte = ' '; 
int ledPin = 13; 
int ledPin2 = 12; 
boolean toglVal = HIGH; 
unsigned char flag = 0; 

// Prior set up of arduino i/0s 
void setup() 
{ 
    pinMode(ledPin, OUTPUT); 
    pinMode(ledPin2,OUTPUT); 
    /* ------ define the pins of outputs as per need 
    . 
    . 
    . 
    */ 

    digitalWrite(ledPin, LOW); 
    digitalWrite(ledPin2, LOW); 
    Serial.begin(9600); 
    Serial.flush(); 
} 

void loop() 
{ 
    Serial.flush(); 
    if (Serial.available()) 
    { 
    myByte = Serial.read(); 
    switch(myByte) 
    { 
    case '1': 
    flag = 1; 
    break; 
    case '0': 
    flag = 2; 
    break; 
    /* ------ define the cases as per the code in android app as per need 
    . 
    . 
    . 
    */ 

    default: 
    digitalWrite(ledPin,LOW); 
    } 
    } 
    while(flag == 1) 
    { 
    digitalWrite(ledPin,toglVal); 
    delay(1000); 
    digitalWrite(ledPin2,~toglVal); 
    delay(1000); 
    if (Serial.available()) break; 
    } 
    while(flag == 2) 
    { 
    digitalWrite(ledPin2,toglVal); 
    delay(500); 
    digitalWrite(ledPin,~toglVal); 
    delay(500); 
    if (Serial.available()) break; 
    } 
} 

有人請幫助我。提前致謝。

回答

0

您應該避免在主循環中使用while循環()。雖然不會讓程序執行下一條指令,除非您的條件滿足。它將繼續在那裏等待。它會進入無限循環,永遠不會出來。考慮使用if()循環爲相同。

+0

感謝您的回覆。 我也嘗試這個,但它不能解決我的問題。 –