Error occured while trying to connect with bluetooth module HC-05 connected with arduino in android program -
i trying exchange data in between arduino , android phone via bluetooth module hc-05
, anenter image description hered 3-4 days unable identify cased of following error:
([jsr82] connect: connection not created (failed or aborted).) occurred while try connect paired bluetooth module using mac address.
i post mainactivity.java
, activity_main.xml
files , arduino
code too.
main activity java file :-
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 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 enabled run 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 file :-
<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 file:-
/* bluetooth control of robot via inputs 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 of arduino i/0s void setup() { pinmode(ledpin, output); pinmode(ledpin2,output); /* ------ define pins of outputs 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 cases per code in android app 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; } }
someone please me this. in advance.
you should avoid using while loop in main loop(). while not let program execute next instruction unless condition inside while satisfies. keep on waiting there. enter , infinite while loop , never come out. consider using if() loop same.
Comments
Post a Comment