在android中连接时出错

时间:2013-10-02 13:03:01

标签: android bluetooth

我正在开发一个我必须连接蓝牙设备的应用程序。

我可以扫描蓝牙设备,但无法连接蓝牙设备。

当我点击按钮时它会连接指定的地址,但它没有出现配对对话框。

connect java代码如下:

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();             
    }

单击连接按钮时,我使用adb logcat查看日志。

这是我的日志文件,有些人可以教我这个日志中的错误信息,我该怎么办?

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

1 个答案:

答案 0 :(得分:2)

在我们的蓝牙设置中,您必须先通过设置页面手动配对设备。 (即找到它,连接它,输入PIN)。不确定你是否可以以编程方式做到这一点。

配对后,服务会尝试通过以下方式连接到它:

  1. 获取所有BT设备的列表:

    Set<BluetoothDevice> pairedDevices =  BluetoothAdapter.getDefaultAdapter().getBondedDevices();
    
  2. 找到我们想要的设备(你知道名字还是可以设置它?):

        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. 打开socket / stream进行阅读。

        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);
        }
    
  4. 不要说你的方法有什么不妥,只是用不同的方式来做。