android - Cant connect to device using bluetooth chat example -
i want create application connecting bluetooth obdii device. firstly wanted base application on bluetooth chat example , start build own app. downloaded bluetooth chat example. https://developer.android.com/samples/bluetoothchat/index.html . tried connect device obdii unable connect. tried connect other device gps bluetooth, fails. app play store
bluetooth terminal
connect 2 devices mentioned earlier without problem. talk them , receive text. should bluetooth example work?
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <!-- copyright 2014 android open source project licensed under apache license, version 2.0 (the "license"); may not use file except in compliance license. may obtain copy of license @ http://www.apache.org/licenses/license-2.0 unless required applicable law or agreed in writing, software distributed under license distributed on "as is" basis, without warranties or conditions of kind, either express or implied. see license specific language governing permissions , limitations under license. --> <manifest package="com.example.android.bluetoothchat" xmlns:android="http://schemas.android.com/apk/res/android" android:versioncode="1" android:versionname="1.0"> <!-- min/target sdk versions (<uses-sdk>) managed build.gradle --> <uses-permission android:name="android.permission.bluetooth_admin"/> <uses-permission android:name="android.permission.bluetooth"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:configchanges="orientation|keyboardhidden" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <activity android:name=".devicelistactivity" android:configchanges="orientation|keyboardhidden" android:label="@string/select_device" android:theme="@android:style/theme.holo.dialog"/> </application> </manifest> bluetoothservice
/* * copyright (c) 2014 android open source project * * licensed under apache license, version 2.0 (the "license"); * may not use file except in compliance license. * may obtain copy of license @ * * http://www.apache.org/licenses/license-2.0 * * unless required applicable law or agreed in writing, software * distributed under license distributed on "as is" basis, * without warranties or conditions of kind, either express or implied. * see license specific language governing permissions , * limitations under license. */ package com.example.android.bluetoothchat; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.bluetooth.bluetoothserversocket; import android.bluetooth.bluetoothsocket; import android.content.context; import android.os.bundle; import android.os.handler; import android.os.message; import com.example.android.common.logger.log; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.uuid; /** * class work setting , managing bluetooth * connections other devices. has thread listens * incoming connections, thread connecting device, , * thread performing data transmissions when connected. */ public class bluetoothchatservice { // debugging private static final string tag = "bluetoothchatservice"; // name sdp record when creating server socket private static final string name_secure = "bluetoothchatsecure"; private static final string name_insecure = "bluetoothchatinsecure"; // unique uuid application private static final uuid my_uuid_secure = uuid.fromstring("fa87c0d0-afac-11de-8a39-0800200c9a66"); private static final uuid my_uuid_insecure = uuid.fromstring("8ce255c0-200a-11e0-ac64-0800200c9a66"); // member fields private final bluetoothadapter madapter; private final handler mhandler; private acceptthread msecureacceptthread; private acceptthread minsecureacceptthread; private connectthread mconnectthread; private connectedthread mconnectedthread; private int mstate; // 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 /** * constructor. prepares new bluetoothchat session. * * @param context ui activity context * @param handler handler send messages ui activity */ public bluetoothchatservice(context context, handler handler) { madapter = bluetoothadapter.getdefaultadapter(); mstate = state_none; mhandler = handler; } /** * set current state of chat connection * * @param state integer defining current connection state */ private synchronized void setstate(int state) { log.d(tag, "setstate() " + mstate + " -> " + state); mstate = state; // give new state handler ui activity can update mhandler.obtainmessage(constants.message_state_change, state, -1).sendtotarget(); } /** * return current connection state. */ public synchronized int getstate() { return mstate; } /** * start chat service. start acceptthread begin * session in listening (server) mode. called activity onresume() */ public synchronized void start() { log.d(tag, "start"); // cancel thread attempting make connection if (mconnectthread != null) { mconnectthread.cancel(); mconnectthread = null; } // cancel thread running connection if (mconnectedthread != null) { mconnectedthread.cancel(); mconnectedthread = null; } setstate(state_listen); // start thread listen on bluetoothserversocket if (msecureacceptthread == null) { msecureacceptthread = new acceptthread(true); msecureacceptthread.start(); } if (minsecureacceptthread == null) { minsecureacceptthread = new acceptthread(false); minsecureacceptthread.start(); } } /** * start connectthread initiate connection remote device. * * @param device bluetoothdevice connect * @param secure socket security type - secure (true) , insecure (false) */ public synchronized void connect(bluetoothdevice device, boolean secure) { log.d(tag, "connect to: " + device); // cancel thread attempting make connection if (mstate == state_connecting) { if (mconnectthread != null) { mconnectthread.cancel(); mconnectthread = null; } } // cancel thread running connection if (mconnectedthread != null) { mconnectedthread.cancel(); mconnectedthread = null; } // start thread connect given device mconnectthread = new connectthread(device, secure); mconnectthread.start(); setstate(state_connecting); } /** * 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, final string sockettype) { log.d(tag, "connected, socket type:" + sockettype); // cancel thread completed connection if (mconnectthread != null) { mconnectthread.cancel(); mconnectthread = null; } // cancel thread running connection if (mconnectedthread != null) { mconnectedthread.cancel(); mconnectedthread = null; } // cancel accept thread because want connect 1 device if (msecureacceptthread != null) { msecureacceptthread.cancel(); msecureacceptthread = null; } if (minsecureacceptthread != null) { minsecureacceptthread.cancel(); minsecureacceptthread = null; } // start thread manage connection , perform transmissions mconnectedthread = new connectedthread(socket, sockettype); mconnectedthread.start(); // send name of connected device ui activity message msg = mhandler.obtainmessage(constants.message_device_name); bundle bundle = new bundle(); bundle.putstring(constants.device_name, device.getname()); msg.setdata(bundle); mhandler.sendmessage(msg); setstate(state_connected); } /** * stop threads */ public synchronized void stop() { log.d(tag, "stop"); if (mconnectthread != null) { mconnectthread.cancel(); mconnectthread = null; } if (mconnectedthread != null) { mconnectedthread.cancel(); mconnectedthread = null; } if (msecureacceptthread != null) { msecureacceptthread.cancel(); msecureacceptthread = null; } if (minsecureacceptthread != null) { minsecureacceptthread.cancel(); minsecureacceptthread = 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 (mstate != state_connected) return; r = mconnectedthread; } // perform write unsynchronized r.write(out); } /** * indicate connection attempt failed , notify ui activity. */ private void connectionfailed() { // send failure message activity message msg = mhandler.obtainmessage(constants.message_toast); bundle bundle = new bundle(); bundle.putstring(constants.toast, "unable connect device"); msg.setdata(bundle); mhandler.sendmessage(msg); // start service on restart listening mode bluetoothchatservice.this.start(); } /** * indicate connection lost , notify ui activity. */ private void connectionlost() { // send failure message activity message msg = mhandler.obtainmessage(constants.message_toast); bundle bundle = new bundle(); bundle.putstring(constants.toast, "device connection lost"); msg.setdata(bundle); mhandler.sendmessage(msg); // start service on restart listening mode bluetoothchatservice.this.start(); } /** * thread runs while listening incoming connections. behaves * server-side client. runs until connection accepted * (or until cancelled). */ private class acceptthread extends thread { // local server socket private final bluetoothserversocket mmserversocket; private string msockettype; public acceptthread(boolean secure) { bluetoothserversocket tmp = null; msockettype = secure ? "secure" : "insecure"; // create new listening server socket try { if (secure) { tmp = madapter.listenusingrfcommwithservicerecord(name_secure, my_uuid_secure); } else { tmp = madapter.listenusinginsecurerfcommwithservicerecord( name_insecure, my_uuid_insecure); } } catch (ioexception e) { log.e(tag, "socket type: " + msockettype + "listen() failed", e); } mmserversocket = tmp; } public void run() { log.d(tag, "socket type: " + msockettype + "begin macceptthread" + this); setname("acceptthread" + msockettype); bluetoothsocket socket = null; // listen server socket if we're not connected while (mstate != state_connected) { try { // blocking call , return on // successful connection or exception socket = mmserversocket.accept(); } catch (ioexception e) { log.e(tag, "socket type: " + msockettype + "accept() failed", e); break; } // if connection accepted if (socket != null) { synchronized (bluetoothchatservice.this) { switch (mstate) { case state_listen: case state_connecting: // situation normal. start connected thread. connected(socket, socket.getremotedevice(), msockettype); break; case state_none: case state_connected: // either not ready or connected. terminate new socket. try { socket.close(); } catch (ioexception e) { log.e(tag, "could not close unwanted socket", e); } break; } } } } log.i(tag, "end macceptthread, socket type: " + msockettype); } public void cancel() { log.d(tag, "socket type" + msockettype + "cancel " + this); try { mmserversocket.close(); } catch (ioexception e) { log.e(tag, "socket type" + msockettype + "close() of server failed", e); } } } /** * thread runs while attempting make outgoing connection * device. runs straight through; connection either * succeeds or fails. */ private class connectthread extends thread { private final bluetoothsocket mmsocket; private final bluetoothdevice mmdevice; private string msockettype; public connectthread(bluetoothdevice device, boolean secure) { mmdevice = device; bluetoothsocket tmp = null; msockettype = secure ? "secure" : "insecure"; // bluetoothsocket connection // given bluetoothdevice try { if (secure) { tmp = device.createrfcommsockettoservicerecord( my_uuid_secure); } else { tmp = device.createinsecurerfcommsockettoservicerecord( my_uuid_insecure); } } catch (ioexception e) { log.e(tag, "socket type: " + msockettype + "create() failed", e); } mmsocket = tmp; } public void run() { log.i(tag, "begin mconnectthread sockettype:" + msockettype); setname("connectthread" + msockettype); // cancel discovery because slow down connection madapter.canceldiscovery(); // make connection bluetoothsocket try { // blocking call , return on // successful connection or exception mmsocket.connect(); } catch (ioexception e) { // close socket try { mmsocket.close(); } catch (ioexception e2) { log.e(tag, "unable close() " + msockettype + " socket during connection failure", e2); } connectionfailed(); return; } // reset connectthread because we're done synchronized (bluetoothchatservice.this) { mconnectthread = null; } // start connected thread connected(mmsocket, mmdevice, msockettype); } public void cancel() { try { mmsocket.close(); } catch (ioexception e) { log.e(tag, "close() of connect " + msockettype + " socket failed", e); } } } /** * thread runs during connection remote device. * handles incoming , outgoing transmissions. */ private class connectedthread extends thread { private final bluetoothsocket mmsocket; private final inputstream mminstream; private final outputstream mmoutstream; public connectedthread(bluetoothsocket socket, string sockettype) { log.d(tag, "create connectedthread: " + sockettype); mmsocket = 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); } mminstream = tmpin; mmoutstream = tmpout; } public void run() { log.i(tag, "begin mconnectedthread"); byte[] buffer = new byte[1024]; int bytes; // keep listening inputstream while connected while (true) { try { // read inputstream bytes = mminstream.read(buffer); // send obtained bytes ui activity mhandler.obtainmessage(constants.message_read, bytes, -1, buffer) .sendtotarget(); } catch (ioexception e) { log.e(tag, "disconnected", e); connectionlost(); // start service on restart listening mode bluetoothchatservice.this.start(); break; } } } /** * write connected outstream. * * @param buffer bytes write */ public void write(byte[] buffer) { try { mmoutstream.write(buffer); // share sent message ui activity mhandler.obtainmessage(constants.message_write, -1, -1, buffer) .sendtotarget(); } catch (ioexception e) { log.e(tag, "exception during write", e); } } public void cancel() { try { mmsocket.close(); } catch (ioexception e) { log.e(tag, "close() of connect socket failed", e); } } } } logcat
07-07 17:46:52.463 12696-12696/com.example.android.bluetoothchat d/dalvikvm﹕ late-enabling checkjni 07-07 17:46:52.783 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ motionrecognitionmanager 07-07 17:46:52.833 12696-12696/com.example.android.bluetoothchat i/mainactivity﹕ ready 07-07 17:46:52.833 12696-12696/com.example.android.bluetoothchat d/bluetoothchatservice﹕ start 07-07 17:46:52.833 12696-12696/com.example.android.bluetoothchat d/bluetoothchatservice﹕ setstate() 0 -> 1 07-07 17:46:52.933 12696-12696/com.example.android.bluetoothchat w/bluetoothadapter﹕ getbluetoothservice() called no bluetoothmanagercallback 07-07 17:46:52.943 12696-12711/com.example.android.bluetoothchat d/bluetoothchatservice﹕ socket type: securebegin macceptthreadthread[thread-28376,5,main] 07-07 17:46:52.943 12696-12696/com.example.android.bluetoothchat w/bluetoothadapter﹕ getbluetoothservice() called no bluetoothmanagercallback 07-07 17:46:52.943 12696-12712/com.example.android.bluetoothchat d/bluetoothchatservice﹕ socket type: insecurebegin macceptthreadthread[thread-28377,5,main] 07-07 17:46:53.023 12696-12696/com.example.android.bluetoothchat d/libegl﹕ loaded /system/lib/egl/libegl_mrvl.so 07-07 17:46:53.033 12696-12696/com.example.android.bluetoothchat d/libegl﹕ loaded /system/lib/egl/libglesv1_cm_mrvl.so 07-07 17:46:53.084 12696-12696/com.example.android.bluetoothchat d/libegl﹕ loaded /system/lib/egl/libglesv2_mrvl.so 07-07 17:46:53.114 12696-12696/com.example.android.bluetoothchat d/gc﹕ <tid=12696> oes20 ===> gc version : gc ver rls_pxa988_kk44_gc13.25 07-07 17:46:53.144 12696-12696/com.example.android.bluetoothchat d/openglrenderer﹕ enabling debug mode 0 07-07 17:47:01.552 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.562 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.572 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ motionrecognitionmanager 07-07 17:47:01.572 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.582 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.592 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.592 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.622 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:01.632 12696-12696/com.example.android.bluetoothchat w/resourcetype﹕ invalid package identifier when getting bag resource number 0xffffffff 07-07 17:47:02.803 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ ondetachedfromwindow 07-07 17:47:02.823 12696-12696/com.example.android.bluetoothchat e/viewrootimpl﹕ senduseractionevent() mview == null 07-07 17:47:02.833 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ setprogressdrawable drawableheight = 21 07-07 17:47:02.873 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ motionrecognitionmanager 07-07 17:47:02.883 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ motionrecognitionmanager 07-07 17:47:02.983 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: left = 0 07-07 17:47:02.983 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: top = 0 07-07 17:47:02.983 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: right = 21 07-07 17:47:02.983 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: bottom = 21 07-07 17:47:04.525 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: left = 0 07-07 17:47:04.525 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: top = 0 07-07 17:47:04.525 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: right = 615 07-07 17:47:04.525 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: bottom = 21 07-07 17:47:04.525 12696-12696/com.example.android.bluetoothchat d/progressbar﹕ updatedrawablebounds: mprogressdrawable.setbounds() 07-07 17:47:04.535 12696-12696/com.example.android.bluetoothchat d/bluetoothchatservice﹕ connect to: 00:15:4b:10:9c:86 07-07 17:47:04.545 12696-12696/com.example.android.bluetoothchat d/bluetoothchatservice﹕ setstate() 1 -> 2 07-07 17:47:04.545 12696-12908/com.example.android.bluetoothchat i/bluetoothchatservice﹕ begin mconnectthread sockettype:insecure 07-07 17:47:04.545 12696-12908/com.example.android.bluetoothchat d/bluetoothutils﹕ issocketallowedbysecuritypolicy start : device null 07-07 17:47:04.545 12696-12908/com.example.android.bluetoothchat d/bluetoothsocket﹕ globalconfig.globalconfig_bt_it_policy_feature = true 07-07 17:47:04.545 12696-12908/com.example.android.bluetoothchat w/bluetoothadapter﹕ getbluetoothservice() called no bluetoothmanagercallback 07-07 17:47:04.565 12696-12908/com.example.android.bluetoothchat d/bluetoothsocket﹕ connect(), socketstate: init, mpfd: {parcelfiledescriptor: filedescriptor[56]} 07-07 17:47:04.605 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ ondetachedfromwindow 07-07 17:47:04.605 12696-12696/com.example.android.bluetoothchat d/abslistview﹕ ondetachedfromwindow 07-07 17:47:06.797 12696-12908/com.example.android.bluetoothchat d/bluetoothchatservice﹕ start 07-07 17:47:06.797 12696-12908/com.example.android.bluetoothchat d/bluetoothchatservice﹕ setstate() 2 -> 1
i found problem. unique id application wrong. found uuid on https://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/ way don't understand why uuid works other don't if explain thats grateful.
// unique uuid application private static final uuid my_uuid_secure = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); private static final uuid my_uuid_insecure = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");
Comments
Post a Comment