我正在尝试通过蓝牙模块HC-05
在arduino和android手机之间交换数据,从3-4天开始,我无法识别出以下错误:
([JSR82] connect: Connection is not created (failed or aborted).) occurred while try to connect with already paired bluetooth module using its MAC address.
我也发布了我的MainActivity.java
,activity_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 Code .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 :(得分:0)
您应该避免在主循环中使用while循环()。虽然不会让程序执行下一条指令,除非你的条件满足。它将继续在那里等待。它将进入并且无限循环并且永远不会出现。考虑使用 if()循环。