0

I am developing an application where I have to connect to Bluetooth device.

I can scan the bluetooth device, but it can not connect the bluetooth device.

It will connect the specified address when I click the button, but it does not appeared the pair dialog.

The connect java code is the following:

BluetoothDevice connect_device = mBluetoothAdapter.getRemoteDevice("D5:51:79:11:93:B8");

try {
       socket = connect_device.createRfcommSocketToServiceRecord(my_UUID);
       socket.connect();
    }
catch (IOException e) 
    {
       e.printStackTrace();             
    }

I use adb logcat to see the log when I click the connect button.

And here is my log file, does some can teach me what the wrong message in this log, and what should I do?

W/InputMethodManagerService( 1718): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4eab5a28 attribute=null, token = android.os.BinderProxy@4eaf6350
I/BluetoothAdapterProperties( 2565): Callback:discoveryStateChangeCallback with state:0
W/BluetoothAdapter( 4750): getBluetoothService() called with no BluetoothManagerCallback
V/BluetoothDiscoveryReceiver( 3693): Received: android.bluetooth.adapter.action.DISCOVERY_FINISHED
D/BTIF_SOCK( 2565): service_uuid: 00001802-0000-1000-8000-00805f9b34fb
D/BluetoothSocket( 4750): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[53]}
D/audio_hw_primary( 1495): found out /dev/snd/pcmC0D0p
W/audio_hw_primary( 1495): out_write() limiting sleep time 68571 to 23219
W/audio_hw_primary( 1495): out_write() limiting sleep time 57482 to 23219
W/audio_hw_primary( 1495): out_write() limiting sleep time 45873 to 23219
W/audio_hw_primary( 1495): out_write() limiting sleep time 34104 to 23219
W/bt-btif ( 2565): info:x10
D/        ( 2565): remote version info [d0:51:62:40:93:c5]: 6, 1d, 7d3
D/btif_config_util( 2565): btif_config_save_file(L153): in file name:/data/misc/bluedroid/bt_config.new
W/bt-sdp  ( 2565): process_service_search_attr_rsp
E/bt-btif ( 2565): DISCOVERY_COMP_EVT slot id:17, failed to find channle,                                status:1, scn:0
W/bt-btif ( 2565): invalid rfc slot id: 17
W/System.err( 4750): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
W/System.err( 4750):    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:504)
W/System.err( 4750):    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:515)
W/System.err( 4750):    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:319)
W/System.err( 4750):    at com.example.preventthelost.DeviceList$4.onItemClick(DeviceList.java:183)
W/System.err( 4750):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
W/System.err( 4750):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
W/System.err( 4750):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
W/System.err( 4750):    at android.widget.AbsListView$1.run(AbsListView.java:3463)
W/System.err( 4750):    at android.os.Handler.handleCallback(Handler.java:730)
D/dalvikvm( 4750): GC_FOR_ALLOC freed 619K, 13% free 4701K/5348K, paused 4ms, total 4ms
W/System.err( 4750):    at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err( 4750):    at android.os.Looper.loop(Looper.java:137)
W/System.err( 4750):    at android.app.ActivityThread.main(ActivityThread.java:5103)
W/System.err( 4750):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 4750):    at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err( 4750):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
W/System.err( 4750):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err( 4750):    at dalvik.system.NativeStart.main(Native Method)
I/Choreographer( 4750): Skipped 165 frames!  The application may be doing too much work on its main thread.
E/System  ( 4750): Uncaught exception thrown by finalizer
E/System  ( 4750): java.io.IOException: socket not created
E/System  ( 4750):      at android.net.LocalSocketImpl.shutdownInput(LocalSocketImpl.java:363)
E/System  ( 4750):      at android.net.LocalSocket.shutdownInput(LocalSocket.java:184)
E/System  ( 4750):      at android.bluetooth.BluetoothSocket.close(BluetoothSocket.java:458)
E/System  ( 4750):      at android.bluetooth.BluetoothSocket.finalize(BluetoothSocket.java:225)
E/System  ( 4750):      at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:187)
E/System  ( 4750):      at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:170)
E/System  ( 4750):      at java.lang.Thread.run(Thread.java:841)
E/bt-btm  ( 2565): btm_sec_disconnected - Clearing Pending flag
W/InputMethodManagerService( 1718): Window already focused, ignoring focus gain
of: com.android.internal.view.IInputMethodClient$Stub$Proxy@4ea705b4 attribute=n
ull, token = android.os.BinderProxy@4eaf6350
André Stannek
  • 7,773
  • 31
  • 52
Wun
  • 6,211
  • 11
  • 56
  • 101

1 Answers1

1

In our bluetooth setup, you have to pair the device manually via the settings page first. (ie find it, connect it, enter the PIN). Unsure if you can do that programatically.

Once paired, a service attempts to connect to it via:

  1. Get list of all BT devices:

    Set<BluetoothDevice> pairedDevices =  BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    
  2. Find device we want (Do you know name or can set it?):

        for (BluetoothDevice device : pairedDevices) {
            String name = device.getName();
            Log.d(TAG,device.getName());
            if (name.contains("NAME OF DEVICE")) {
                mmDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
                break;
            }
        }
    
  3. Open the socket/stream for reading.

        UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); // Standard SerialPortService ID
        try {
            mBluetoothAdapter.cancelDiscovery();
            mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid);
            try {
                mmSocket.connect();
                Log.i(TAG, "bluetooth socket connected");
                try {
                    mmInputStream = mmSocket.getInputStream();
                    Log.i(TAG, "getInputStream succeeded");
                } catch (IOException e_getin) {
                    Log.i(TAG, "getInputStream failed", e_getin);
                }
            } catch (IOException econnect) {
                Log.i(TAG, "connect socket failed", econnect);
            }
        } catch (IOException ecreate) {
            Log.i(TAG, "create socket failed", ecreate);
        }
    

Not to say there's anything wrong w/ your approach, just showing a different way to do it.

K5 User
  • 606
  • 1
  • 6
  • 10
  • 1
    Is ``mmDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());`` different from ``mmDevice = device;``? – christophe31 Jan 29 '14 at 11:05
  • I am facing a similar issue. http://stackoverflow.com/questions/35397159/getbluetoothservice-called-with-no-bluetoothmanagercallback – Tushar Banne Feb 17 '16 at 06:09