Android : General questions to Bluetooth

on Monday, February 16, 2015


I am trying to implement an app which is able to send simple string messages to other devices. My app is running on Nexus 7 Tablet and it should be able to send simple String messages to my Nexus 4 or to my Microcontroller. For now i was able to do the following:



  • Discover nearby devices

  • Pair devices

  • Connect to my Nexus 4

  • Sending String messages to my Nexus 4


I used this BluetoothService, this BluetoothFragment to discover devices and this MainFragment.


I have some problems left now which are related to many people trying to use bluetooth in android apps (at least in my opinion). I was searching the web for different problems and realised, that there is no complete documentation or example on how to use bluetooth for different tasks. Even the bluetooth chat example from android is leaving many questions unanswered and i hope i can find the answers to my questions here.


1. Different UUID's on different Devices


As i mentioned i was able to connect from my Nexus 7 to my Nexus 4. For that i used this UUID:



private static final UUID MY_UUID = UUID.fromString("d0c722b0-7e15-11e1-b0c4-0800200c9a66");


I also need to connect from my Nexus 7 to my Microcontroller (not at the same time. always just one connection, not multiple) but the Microcontroller has a fix unchangeable UUID. So i somehow need to handle multiple (two) UUID's on my Nexus 7 (bluetooth server side). How can i do that?


2. Accept failed Error?


Everytime when i successfully connect from my nexus 7 to my nexus 4 i get the following error message:



02-16 12:11:37.839: E/BluetoothService(27605): accept() failed
02-16 12:11:37.839: E/BluetoothService(27605): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
02-16 12:11:37.839: E/BluetoothService(27605): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:512)
02-16 12:11:37.839: E/BluetoothService(27605): at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:489)
02-16 12:11:37.839: E/BluetoothService(27605): at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:399)
02-16 12:11:37.839: E/BluetoothService(27605): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:130)
02-16 12:11:37.839: E/BluetoothService(27605): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:116)


It appears everytime i connected successfully! the app is working fine after the error message and the sending of strings is also working, so why does that exception appear and how can i prevent it?


3. How to connect to another device while i am connected?


When i successfully connected to my nexus 4 and try to connect to another device i get the following error:



02-16 12:12:28.800: E/BluetoothService(27605): socket closed
02-16 12:12:28.800: E/BluetoothService(27605): java.io.IOException: socket closed
02-16 12:12:28.800: E/BluetoothService(27605): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:300)
02-16 12:12:28.800: E/BluetoothService(27605): at de.hof.drschneider.intelligentline.bluetooth.BluetoothService$ConnectThread.run(BluetoothService.java:381)


The app is crashing after that error. So i somehow need to check if im already connected and when im connected i need to close the connection and open it to the new device. How can i do that?


I hope someone can help me with my questions. I will deliver further informations if needed.


0 comments:

Post a Comment