i'm trying connect device code below... modified btchat source. seems go fine, device not responding commands, phone still says it's paired not connected, , device says it's not connected. makes me pretty sure it's indeed not connected, no errors happening.
could have wrong uuid?
the log says:
04-30 18:51:10.116: debug/bluetoothservice(1228): uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1 04-30 18:51:10.116: debug/bluetoothservice(1228): making callback 00001101-0000-1000-8000-00805f9b34fb result 1 04-30 18:51:10.131: verbose/bluetootheventredirector(31561): received android.bleutooth.device.action.uuid
and code printing log it's connected:
04-30 18:53:21.210: debug/btcomm(1044): connect to: k4500-620963 04-30 18:53:21.225: info/btcomm(1044): begin connectthread 04-30 18:53:26.272: debug/btcomm(1044): connected to: k4500-620963 04-30 18:53:26.272: debug/btcomm(1044): create connectedthread 04-30 18:53:26.280: debug/btcomm(1044): setstate() 0 -> 3 04-30 18:53:26.280: info/btcomm(1044): begin connectedthread
here whole code: p
ublic class btcom { // debugging private static final string tag = "btcomm"; private static final boolean d = true; private static final uuid uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); // member fields private final bluetoothadapter adapter; private final handler handler; private connectthread connectthread; private connectedthread connectedthread; private int state; private context context; private bluetoothdevice btdevice; // constants indicate current connection state public static final int state_none = 0; // we're doing nothing public static final int state_listen = 1; // listening incoming connections public static final int state_connecting = 2; // initiating outgoing connection public static final int state_connected = 3; // connected remote device public btcom(context context, handler handler) { adapter = bluetoothadapter.getdefaultadapter(); state = state_none; this.handler = handler; this.context = context; } private void checkbluetooth(){ // check if device supports bluetooth if (adapter == null) { toast.maketext(context, "error: device not support bluetooth communication" , toast.length_long).show(); return; } // make sure bluetooth enabled if (!adapter.isenabled()) { // if not, prompt user intent enablebtintent = new intent(bluetoothadapter.action_request_enable); context.startactivity(enablebtintent); } } public bluetoothdevice getbtdevice(){ set<bluetoothdevice> paireddevices = adapter.getbondeddevices(); bluetoothdevice founddevice = null; btdevice = null; // if there paired devices if (paireddevices.size() > 0) { // loop through paired devices (bluetoothdevice device : paireddevices) { // check if name fits pattern k4xxx-xxxxxx if (device.getname().matches("k4\\d\\d\\d-\\d\\d\\d\\d\\d\\d")){ founddevice = device; break; } } if (founddevice == null){ // if didn't find toast.maketext(context, "error: no paired btdevice device found, please pair btdevice device continue" , toast.length_long).show(); return null; } } return founddevice; // found btdevice! } /** * set current state of chat connection * @param state integer defining current connection state */ private synchronized void setstate(int state) { if (d) log.d(tag, "setstate() " + this.state + " -> " + state); this.state = state; } /** * start connectthread initiate connection remote device. * @param device bluetoothdevice connect */ public synchronized void connect(bluetoothdevice device) { log.d(tag, "connect to: " + device.getname()); // cancel thread attempting make connection if (connectthread != null) {connectthread.cancel(); connectthread = null;} // cancel thread running connection if (connectedthread != null) {connectedthread.cancel(); connectedthread = null;} // start thread connect given device connectthread = new connectthread(device); connectthread.start(); } /** * start connectedthread begin managing bluetooth connection * @param socket bluetoothsocket on connection made * @param device bluetoothdevice has been connected */ public synchronized void connected(bluetoothsocket socket, bluetoothdevice device) { log.d(tag, "connected to: " + device.getname()); // cancel thread completed connection if (connectthread != null) {connectthread.cancel(); connectthread = null;} // cancel thread running connection if (connectedthread != null) {connectedthread.cancel(); connectedthread = null;} // start thread manage connection , perform transmissions connectedthread = new connectedthread(socket); connectedthread.start(); setstate(state_connected); } /** * stop threads */ public synchronized void stop() { if (d) log.d(tag, "stop"); if (connectthread != null) { connectthread.cancel(); connectthread = null; } if (connectedthread != null) { connectedthread.cancel(); connectedthread = null; setstate(state_none); } } /** * write connectedthread in unsynchronized manner * @param out bytes write * @see connectedthread#write(byte[]) */ public void write(byte[] out) { // create temporary object connectedthread r; // synchronize copy of connectedthread synchronized (this) { if (state != state_connected) return; r = connectedthread; } // perform write unsynchronized r.write(out); } private class connectthread extends thread { private final bluetoothsocket socket; private final bluetoothdevice device; public connectthread(bluetoothdevice device) { this.device = device; bluetoothsocket tmp = null; // bluetoothsocket connection // given bluetoothdevice try { tmp = device.createrfcommsockettoservicerecord(uuid); } catch (ioexception e) { log.e(tag, "socket create failed", e); } socket = tmp; } public void run() { log.i(tag, "begin connectthread"); // make connection bluetoothsocket try { // blocking call , return on // successful connection or exception socket.connect(); } catch (ioexception e) { // close socket try { socket.close(); } catch (ioexception e2) { log.e(tag, "unable close() socket during connection failure", e2); } return; } // reset connectthread because we're done synchronized (btcom.this) { connectthread = null; } // start connected thread connected(socket, this.device); } public void cancel() { try { socket.close(); } catch (ioexception e) { log.e(tag, "close() of connect socket failed", e); } } } /** * thread runs during connection remote device. * handles incoming , outgoing transmissions. */ private class connectedthread extends thread { private final bluetoothsocket socket; private final inputstream instream; private final outputstream outstream; public connectedthread(bluetoothsocket socket) { log.d(tag, "create connectedthread"); this.socket = socket; inputstream tmpin = null; outputstream tmpout = null; // bluetoothsocket input , output streams try { tmpin = socket.getinputstream(); tmpout = socket.getoutputstream(); } catch (ioexception e) { log.e(tag, "temp sockets not created", e); } instream = tmpin; outstream = tmpout; } public void run() { log.i(tag, "begin connectedthread"); byte[] buffer = new byte[1024]; int bytes; // keep listening inputstream while connected while (true) { try { // read inputstream bytes = instream.read(buffer); // send obtained bytes ui activity handler.obtainmessage(kestreltest.message_read, bytes, -1, buffer) .sendtotarget(); } catch (ioexception e) { log.e(tag, "disconnected", e); // start service on restart listening mode break; } } } /** * write connected outstream. * @param buffer bytes write */ public void write(byte[] buffer) { try { outstream.write(buffer); log.i(tag, "sending " + buffer.length + " bytes"); log.i(tag, "sending: " + new string(buffer)); } catch (ioexception e) { log.e(tag, "exception during write", e); } } public void cancel() { try { socket.close(); } catch (ioexception e) { log.e(tag, "close() of connect socket failed", e); } } } }
i'm stumped why is't not connecting... ideas? thanks!
what device connecting to? perhaps device requires profiles supported host (android) system, a2dp, bip, bpp etc.
the bluetooth chat program creates basic client-server connection, , not require bluetooth profiles support.
for bluetooth profile support implemented on android, there project called “sybase-ianywhere-blue-sdk-for-android”, replaces android's version, , provides interfaces underlying bluetooth profiles , protocols. using this, printing on bluetooth using android phone possible using bpp profile provided sdk.
see links below more details: link 1: http://www.sybase.com/detail?id=1064424 link 2: http://www.sybase.com/products/allproductsa-z/mobiledevicesdks/bluetoothsdks
application profiles supported ianywhere blue sdk android said include:
handsfree (hfp) advanced audio distribution (a2dp) a/v remote control (avrcp) file transfer protocol (ftp) object push protocol (opp) sim access (sap) phone book access (pbap) message access profile (map) human interface (hid) basic printing (bpp) basic imaging (bip)
Comments
Post a Comment