More information on this problem:  The run() method of the
ConnectThread class within the BluetoothService class of the
BluetoothChat example calls connect() on the BluetoothSocket class.
This happens after the device is successfully discovered and in
response to the user selecting it from a list of discovered devices.
The first thing the run() method does is to cancel discovery.  It then
calls the BluetoothSocket connect() method which throws an
IOException, whose message is "Service discovery failed".  Here is the
code for the run() method.  The connectionFailed() method is called
every time I try to connect to one of my devices but never for a
different but supposedly identical one.

       public void run() {

            // Always cancel discovery because it will slow down a
connection
            mAdapter.cancelDiscovery();

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
            } catch (IOException e) {
                connectionFailed(e.getMessage());
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() socket during
connection failure", e2);
                }
                // Start the service over to restart listening mode
                BluetoothService.this.start();
                return;
            }

            // Reset the ConnectThread because we're done
            synchronized (BluetoothService.this) {
                mConnectThread = null;
            }

            // Start the connected thread
            connected(mmSocket, mmDevice);
        }

And here is the ConnectThread class definition and its constructor,
which does not throw an exception:

   private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            mmDevice = device;
            BluetoothSocket tmp = null;

            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try {
                tmp =
device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "create() failed", e);
            }
            mmSocket = tmp;
        }

Could there be a problem with the connect() method of BluetoothSocket
that causes it to fail to account for some variability in Bluetooth
devices?  Maybe some devices take longer to connect than others and
connect() is not tolerant of these longer connection times?

On Mar 21, 6:41 pm, DonFrench <dcfre...@gmail.com> wrote:
> I have an Android 2.0 app that successfully sends messages to another
> device via Bluetooth SPP, using a slightly modifed version of
> BluetoothChat.   I only had to change the UUID to make the Chat
> example work.  Here is the puzzle:  My app successfully and
> consistently connects to one of two supposedly identical devices.
> However, it consistently fails to connect to the other one, giving a
> Service Discovery Failed message.  Yet, a similar program running on a
> Windows Mobile device can connect to either of the two devices.  What
> could possibly be causing the Android program to fail to connect to
> one of the two devices while successfully connecting to the other,
> especially since the WM program can connect to either one?  Note that
> I manufacture the two devices and they have identical hardware and
> software.  The program on the WM devices uses a Windows system process
> for making the connection and so unfortunately I don't have access to
> the actual code that they use to make the connection.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

To unsubscribe from this group, send email to 
android-developers+unsubscribegooglegroups.com or reply to this email with the 
words "REMOVE ME" as the subject.

Reply via email to