views:

442

answers:

3

Howdy,

I'm currently working on an Android application that connects to a instrument via Bluetooth, and needs to write string commands and receive string responses back. Currently I have the connect/read/write working for TCP/IP over wi-fi and now trying to implement bluetooth. But am running into some roadblocks. I have been searching the web trying to find examples of something similar and haven't had any luck. I have been using the Android Dev Resource Example: Bluetooth Chat as my main reference point.

My current code seems to work.. then it throws a Service Discovery Failed exception at point of connection. I am using the DeviceListActivity class to do the discovery and selecting of the device I want to connect to. It returns anActivityResult and then my Bluetooth class waits for it handles that and then does the connect to it. The code beneath is almost identical to the Bluetooth Chat App.

public void onActivityResult(int requestCode, int resultCode, Intent data) {

if(!m_BluetoothAdapter.isEnabled())
{
m_BluetoothAdapter.enable();
}
     switch (requestCode) {
     case REQUEST_CONNECT_DEVICE:
         // When DeviceListActivity returns with a device to connect
         if (resultCode == Activity.RESULT_OK) {
             // Get the device MAC address
             String address = data.getExtras()
                                  .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
             // Get the BLuetoothDevice object
             BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
             // Attempt to connect to the device
             connect(device);
         }
         break;
             case REQUEST_ENABLE_BT:
         // When the request to enable Bluetooth returns
         if (resultCode == Activity.RESULT_OK) {
             // Bluetooth is now enabled, so set up a chat session

         } else {
             // User did not enable Bluetooth or an error occured

             Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show();
             finish();
         }
     }
 }

This is my connect function:

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private void connect(BluetoothDevice device) {
     m_Device = device;
     BluetoothSocket tmp = null;

     // Get a BluetoothSocket for a connection with the
     // given BluetoothDevice
     try {
         tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
     } catch (IOException e) {

     }
     m_Socket = tmp;

     m_BluetoothAdapter.cancelDiscovery();

     try {
         // This is a blocking call and will only return on a
         // successful connection or an exception
         m_Socket.connect();
     } catch (IOException e) {
      try {
       m_Socket.close();
      }catch (IOException e2) {

         }
      return;

      }

}

Hopefully.. whatever I am doing wrong is simple.. but I'm afraid its never that easy. This is my first time doing any Bluetooth development and maybe I'm doing something blatantly wrong.. but.. I'm not sure why I get the service discovery failed exception.

You can pair/find the device at all times manually on the phone.. it does require a passcode, but I don't think that is the problem that I am having. Any thoughts would be much appreciated!!

Thanks! TxAggieDev

+1  A: 

After.. three days I got it figured out thanks to some very helpful posts.

I had to replace:

tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

with:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
         tmp = (BluetoothSocket) m.invoke(device, 1);

and viola it works!

TxAg
Wait, I have a few Bluetooth apps and I haven't found a situation where using reflection (getclass.getmethod) is necessary. Can you tell me a little more about what hardware you tried? I am very interested to know if reflection is actually necessary in certain situations.
Brad Hein
After some testing. I found that it was actually the phone that was having issues with it. The HTC Incredible did not connect using the first method, however the Samsung Galaxy S and another, can't remember which one, did connect on first try.
TxAg
I was plagued with this exception and this solution seemed to fix my problems. Do you understand what is going on here and why it alleviates the problem of the Service Discovery failing?
rohanbk
Honestly, I do not. From what I've read it might have something to do with the Bluetooth support that HTC is lacking in their phones and by using the reflection its a work around.
TxAg
The best I found was try the first method, which connects faster. Then on the Service Discovery fail implement the reflection in the exception. Also found that the same HTC phones also had issues with UDP detection as well, while Samsung and Motorola devices had no issues.
TxAg
A: 

Thanks for this, I had the same problem on my HTC Wildfire device.

Karel Braeckman
A: 

you are genius!! thanx

kyung Kim