java - Service discovery failed exception using Bluetooth on Android -
I am currently working on an Android application connecting via Bluetooth to a device and writing string commands and The need to get string reactions back. Currently I have to connect / read / write to TCP / IP on Wi-Fi and now trying to implement Bluetooth. But I am walking in some obstacles. I am trying to find some similar examples and have not got any luck. I'm using an Android Developer Resources example: Bluetooth chat as my main reference point.
My current code works .. Then it throws service discovery fail exception at the point of connection. I'm using the DeviceListActivity
class to select Discovery and to connect to the device. It gives an activity result and after that my Bluetooth class waits to handle it and then connects it. Below the code is nearly identical to the Bluetooth chat application.
Public Zero onActivityResult (integer requestCode, integer resultCode, intent data) {if (! M_BluetoothAdapter.isEnabled ()) {m_BluetoothAdapter.enable (); } Switch (requestCode) {Cases REQUEST_CONNECT_DEVICE: // DeviceListActivity returns to a device when it connects (resultCode == Activity.RESULT_OK) {// Go Tools MAC Address String Address = data.getExtras () .getString (DeviceListActivity.EXTRA_DEVICE_ADDRESS); // Get the Bluetooth device object Bluetooth device = m_BluetoothAdapter.getRemoteDevice (address); // Attempt to connect to the device Connect (device); } break; CASE REQUEST_ENABLE_BT: // Bluetooth enabled request returns (resultCode == Activity.RESULT_OK) and {// Bluetooth is now enabled, so set up a chat session above} {// User Bluetooth was not enabled or toast a mistake .make text (this, "Bluetooth is not enabled", Toast. LNNGHHORT). Show (); End(); }}}
This is my connect function:
Private static last UUID MY_UUID = UUID.fromString ("00001101-0000-1000-8000-00805F9B34FB "); Private Zero Connect (Bluetooth device device) {m_Device = device; BluetoothSocket tmp = null; // Get a bluetoothset for connection with the Bluetooth device {tmp = device.createRfcommSocketToServiceRecord (MY_UUID); } Hold (IOException e) {} m_Socket = tmp; M_BluetoothAdapter.cancelDiscovery (); Try {// This is a blocked call and will return only to a successful connection or an exception to m_Socket.connect (); } Hold (IOException e) {try {m_Socket.close (); } Hold (IOException e2) {} return; }}
Hopefully, whatever I am doing wrong is simple, but I'm afraid it will never be easy. This is my first time to develop bluetooth, and maybe I'm doing something wrong ... but I'm not sure why I found a failure in the search of the service.
You can add / find the phone manually on the device all the time ... it requires a passcode, but I do not think this is the problem that is happening to me.
Comments
Post a Comment