views:

1770

answers:

5

Hello, I want to use Android 2.1 to connect to an external Bluetooth device, wich is offering an SPP port to me. In this case it is an external GPS unit. When I'm trying to connect I can't connect an established socket while being in the "client" mode. Then if I try to set up a socket (being in the server role), to RECEIVE text from my PC everything works just fine. The Computer can connect as the client to the Socket on the Phone via SPP using the SSP UUID or some random UUID. So the Problem is not that I'm using the wrong UUID.

But the other way around (e.g. calling connect on the established client socket) createRfcommSocketToServiceRecord(UUID uuid)) just doesn't work. Sadly I don't have the time to inspect the problem further. It would be greate If somebody could point me the right way. Phone: Samsung Galaxy Spica I-5700 Android 2.1 In the following part of the Logfile has to be the Problem.

Greets PhilDev

P.S. I'm going to be present during the Office hours.

Here the log file:


03-21 03:10:52.020: DEBUG/BluetoothSocket.cpp(4643): initSocketFromFdNative
03-21 03:10:52.025: DEBUG/BluetoothSocket(4643): connect
03-21 03:10:52.025: DEBUG/BluetoothSocket(4643): doSdp
03-21 03:10:52.050: DEBUG/ADAPTER(2132): create_device(01:00:00:7F:B5:B3)
03-21 03:10:52.050: DEBUG/ADAPTER(2132): adapter_create_device(01:00:00:7F:B5:B3)
03-21 03:10:52.055: DEBUG/DEVICE(2132): Creating device [address = 01:00:00:7F:B5:B3] /org/bluez/2132/hci0/dev_01_00_00_7F_B5_B3 [name = ]
03-21 03:10:52.055: DEBUG/DEVICE(2132): btd_device_ref(0x10c18): ref=1
03-21 03:10:52.065: INFO/BluetoothEventLoop.cpp(1914): event_filter: Received signal org.bluez.Adapter:DeviceCreated from /org/bluez/2132/hci0
03-21 03:10:52.065: INFO/BluetoothService.cpp(1914): ... Object Path = /org/bluez/2132/hci0/dev_01_00_00_7F_B5_B3
03-21 03:10:52.065: INFO/BluetoothService.cpp(1914): ... Pattern = 00001101-0000-1000-8000-00805f9b34fb, strlen = 36
03-21 03:10:52.070: DEBUG/DEVICE(2132): *************DiscoverServices********
03-21 03:10:52.070: INFO/DTUN_HCID(2132): dtun_client_get_remote_svc_channel: starting discovery on  (uuid16=0x0011)
03-21 03:10:52.070: INFO/DTUN_HCID(2132):    bdaddr=01:00:00:7F:B5:B3
03-21 03:10:52.070: INFO/DTUN_CLNT(2132):     Client calling DTUN_METHOD_DM_GET_REMOTE_SERVICE_CHANNEL (id 4)
03-21 03:10:52.070: INFO/(2106): DTUN_ReceiveCtrlMsg: [DTUN] Received message [BTLIF_DTUN_METHOD_CALL] 4354
03-21 03:10:52.070: INFO/(2106): handle_method_call: handle_method_call :: received DTUN_METHOD_DM_GET_REMOTE_SERVICE_CHANNEL (id 4), len 134
03-21 03:10:52.075: ERROR/BTLD(2106): ****************search UUID = 1101***********
03-21 03:10:52.075: INFO//system/bin/btld(2103):         btapp_dm_GetRemoteServiceChannel()
03-21 03:10:52.120: DEBUG/BluetoothService(1914): updateDeviceServiceChannelCache(01:00:00:7F:B5:B3)
03-21 03:10:52.120: DEBUG/BluetoothEventLoop(1914): ClassValue: null for remote device: 01:00:00:7F:B5:B3 is null
03-21 03:10:52.120: INFO/BluetoothEventLoop.cpp(1914): event_filter: Received signal org.bluez.Adapter:PropertyChanged from /org/bluez/2132/hci0
03-21 03:10:52.305: WARN/BTLD(2106): bta_dm_check_av:0
03-21 03:10:56.395: DEBUG/WifiService(1914): ACTION_BATTERY_CHANGED pluggedType: 2
03-21 03:10:57.440: WARN/BTLD(2106): SDP - Rcvd conn cnf with error: 0x4  CID 0x43
03-21 03:10:57.440: INFO/BTL-IFS(2106): send_ctrl_msg: [BTL_IFS CTRL] send BTLIF_DTUN_SIGNAL_EVT (CTRL) 13 pbytes (hdl 10)
03-21 03:10:57.445: INFO/DTUN_CLNT(2132): dtun-rx signal [DTUN_SIG_DM_RMT_SERVICE_CHANNEL] (id 42) len 15
03-21 03:10:57.445: INFO/DTUN_HCID(2132): dtun_dm_sig_rmt_service_channel: success=1, service=00000000
03-21 03:10:57.445: ERROR/DTUN_HCID(2132): discovery unsuccessful!

package de.phil_dev.android.BT;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ThinBTClient extends Activity {

    private static final String TAG = "THINBTCLIENT";
    private static final boolean D = true;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothSocket btSocket = null;
    private BufferedInputStream inStream = null;
    private BluetoothServerSocket myServerSocket;
    private ConnectThread myConnection;
    private ServerThread myServer;
    // Well known SPP UUID (will *probably* map to
    // RFCOMM channel 1 (default) if not in use);
    // see comments in onResume().
    private static final UUID MY_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");


    // .fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee");

    // ==> hardcode your slaves MAC address here <==
    // PC
    // private static String address = "00:09:DD:50:86:A0";

    // GPS
    private static String address = "00:0B:0D:8E:D4:33";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (D)
            Log.e(TAG, "+++ ON CREATE +++");

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available.",
                    Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        if (!mBluetoothAdapter.isEnabled()) {
            Toast.makeText(this,
                    "Please enable your BT and re-run this program.",
                    Toast.LENGTH_LONG).show();
            finish();
            return;
        }

        if (D)
            Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");
    }

    @Override
    public void onStart() {
        super.onStart();
        if (D)
            Log.e(TAG, "++ ON START ++");
    }

    @Override
    public void onResume() {
        super.onResume();

        if (D) {
            Log.e(TAG, "+ ON RESUME +");
            Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
        }

        // Make the phone discoverable

        // When this returns, it will 'know' about the server,
        // via it's MAC address.
        // mBluetoothAdapter.startDiscovery();
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        Log.e(TAG, device.getName() + " connected");


//       myServer = new ServerThread();
//       myServer.start();

        myConnection = new ConnectThread(device);
        myConnection.start();

    }

    @Override
    public void onPause() {
        super.onPause();

        if (D)
            Log.e(TAG, "- ON PAUSE -");

        try {
            btSocket.close();
        } catch (IOException e2) {
            Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (D)
            Log.e(TAG, "-- ON STOP --");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (D)
            Log.e(TAG, "--- ON DESTROY ---");
    }

    private class ServerThread extends Thread {
        private final BluetoothServerSocket myServSocket;

        public ServerThread() {
            BluetoothServerSocket tmp = null;

            // create listening socket
            try {
                tmp = mBluetoothAdapter
                        .listenUsingRfcommWithServiceRecord(
                                "myServer", MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "Server establishing failed");
            }

            myServSocket = tmp;
        }

        public void run() {
            Log.e(TAG, "Beginn waiting for connection");
            BluetoothSocket connectSocket = null;

            InputStream inStream = null;
            byte[] buffer = new byte[1024];
            int bytes;

            while (true) {
                try {
                    connectSocket = myServSocket.accept();
                } catch (IOException e) {
                    Log.e(TAG, "Connection failed");
                    break;
                }

                Log.e(TAG, "ALL THE WAY AROUND");
                try {
                    connectSocket = connectSocket.getRemoteDevice()
                            .createRfcommSocketToServiceRecord(MY_UUID);
                    connectSocket.connect();
                } catch (IOException e1) {
                    Log.e(TAG, "DIDNT WORK");
                }

                // handle Connection
                try {
                    inStream = connectSocket.getInputStream();
                    while (true) {
                        try {
                            bytes = inStream.read(buffer);
                            Log.e(TAG, "Received: " + buffer.toString());
                        } catch (IOException e3) {
                            Log.e(TAG, "disconnected");
                            break;
                        }
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    break;
                }

            }
        }

        void cancel() {

        }
    }

    private class ConnectThread extends Thread {

        private final BluetoothSocket mySocket;
        private final BluetoothDevice myDevice;

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

            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "CONNECTION IN THREAD DIDNT WORK");
            }
            mySocket = tmp;
        }

        public void run() {
            Log.e(TAG, "STARTING TO CONNECT THE SOCKET");
            setName("My Connection Thread");
            InputStream inStream = null;
            boolean run = false;
            //mBluetoothAdapter.cancelDiscovery();

            try {
                mySocket.connect();
                run = true;
            } catch (IOException e) {
                run = false;
                Log.e(TAG, this.getName()
                        + ": CONN DIDNT WORK, Try closing socket");
                try {
                    mySocket.close();
                } catch (IOException e1) {
                    Log.e(TAG, this.getName() + ": COULD CLOSE SOCKET", e1);

                    this.destroy();
                }
            }

            synchronized (ThinBTClient.this) {
                myConnection = null;
            }

            byte[] buffer = new byte[1024];
            int bytes;
            // handle Connection
            try {
                inStream = mySocket.getInputStream();
                while (run) {

                    try {
                        bytes = inStream.read(buffer);
                        Log.e(TAG, "Received: " + buffer.toString());
                    } catch (IOException e3) {
                        Log.e(TAG, "disconnected");
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // starting connected thread (handling there in and output
        }

        public void cancel() {
            try {
                mySocket.close();
            } catch (IOException e) {
                Log.e(TAG, this.getName() + " SOCKET NOT CLOSED");
            }
        }

    }

}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.phil_dev.android.BT" android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".ThinBTClient" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest> 
A: 

Have you tried opening client socket to the pc and see if that works? Couldn't find any obvious errors in your modified code.

Are you sure that the MAC are correct?

awiden
A: 

http://stackoverflow.com/questions/2661932 - much the same issue, also on Samsung Spica GT i5700

Ciaran
+1  A: 

I found this exact problem happens when one of the devices has the Bluetooth turned off.

Luis
+1  A: 

I'm having a similar problem on HTC Droid.

Mine gets a tiny bit further though and connect() succeeds but I am unable to receive so much as a single byte on the socket InputStream.

http://stackoverflow.com/questions/2853790/why-cant-htc-droid-running-ota-2-1-communicate-with-rfcomm

I think we've hit a limitation in the way HTC has implemented Bluetooth on their phones. Note that the manufacturers, such as HTC, have to write drivers so that Android OS can communicate with their proprietary hardware. HTC seems to have released 2.0/2.1 without complete driver support.

Brad Hein
A: 

http://stackoverflow.com/questions/2661932/problem-with-bluetooth-on-android-2-1-samsung-spica-i5700-where-pairing-works-b

you should view the site i give! maybe the problem is your device's system 's problem. try to update your firmware ! now i am also blocked by the firmware . because my device's rom is not the official, so maybe some problems can not be solved! it is a bug!