设备发现开启时连接蓝牙插座

时间:2014-04-09 13:41:17

标签: android bluetooth

我在我的andorid应用程序中使用蓝牙插座连接来连接近距离的医疗设备。我想通过点击ListView上的特定设备来连接蓝牙插座,而蓝牙发现还没有完成。 我正在使用以下代码连接蓝牙插座以连接到蓝牙范围内的设备。

BluetoothSocket mBluetoothSocket;

mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(UUID
                                .fromString("00001101-0000-1000-8000-00805F9B34FB"));

                        if (mBluetoothSocket != null) {
                            // Device does not support Bluetooth
                            Log.w("MyDebugMsg", "Bluetooth Socket present");
                            mBluetoothAdapter.cancelDiscovery();
                            mDialog2 = ProgressDialog.show(HL7MedicalAppActivity.this,
                                    "", "Connecting to device, Please wait...",
                                    true);

                            new Thread(new Runnable() {
                                public void run() {
                                    try {
                                        mBluetoothSocket.connect();

                                    } catch (IOException e) {
                                        runOnUiThread(new Runnable() {
                                            public void run() {
                                                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                                                        HL7MedicalAppActivity.this);
                                                // Setting Dialog Title
                                                alertDialog.setTitle("Error");
                                                // Setting Dialog Message
                                                alertDialog
                                                        .setMessage("Connection refused by remote device");
                                                // Setting OK Button
                                                alertDialog
                                                        .setPositiveButton(
                                                                "OK",
                                                                new DialogInterface.OnClickListener() {
                                                                    public void onClick(
                                                                            DialogInterface dialog,
                                                                            int id) {
                                                                        HL7MedicalAppActivity.this
                                                                                .finish();
                                                                    }
                                                                });

                                                // Showing Alert Message
                                                alertDialog.show();
                                            }
                                        });
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

但如果我在设备发现完成之前尝试连接,则会显示蓝牙插座连接错误

04-09 19:03:32.720: W/MyDebugMsg(5463): Bluetooth Socket present
04-09 19:03:32.730: I/BluetoothAdapterProperties(2879):    
Callback:discoveryStateChangeCallback with state:0
04-09 19:03:32.730: I/BluetoothAdapterProperties(2879):   
Callback:discoveryStateChangeCallback with state:0
04-09 19:03:32.740: V/BluetoothDiscoveryReceiver(2847): Received:   
android.bluetooth.adapter.action.DISCOVERY_FINISHED
 04-09 19:03:32.740: D/audio_hw_primary(177): select_devices: out_snd_device(2:     
 speaker) in_snd_device(0: )
 04-09 19:03:32.740: D/ACDB-LOADER(177): ACDB -> send_afe_cal
 04-09 19:03:32.750: W/BluetoothAdapter(5463): getBluetoothService() called with no  
 BluetoothManagerCallback
 04-09 19:03:32.750: D/BTIF_SOCK(2879): service_uuid:     
 00001101-0000-1000-8000-00805f9b34fb
 04-09 19:03:32.750: E/bt-btif(2879): DISCOVERY_COMP_EVT slot id:12, failed to find  
 channle,                                       status:2, scn:-86134667
 04-09 19:03:32.750: W/bt-btif(2879): invalid rfc slot id: 12
04-09 19:03:32.750: D/BluetoothSocket(5463): connect(), SocketState: INIT, mPfd:         {ParcelFileDescriptor: FileDescriptor[58]}
04-09 19:03:32.750: W/System.err(5463): java.io.IOException: read failed, socket might 
closed or timeout, read ret: -1
04-09 19:03:32.750: W/System.err(5463):     at  
android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
04-09 19:03:32.750: W/System.err(5463):     at   
android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516)
04-09 19:03:32.750: W/System.err(5463):     at 
 android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
04-09 19:03:32.750: W/System.err(5463):     at        
com.example.hl7medicalapp.HL7MedicalAppActivity$4$1.run(HL7MedicalAppActivity.java:389)
04-09 19:03:32.750: W/System.err(5463):     at java.lang.Thread.run(Thread.java:841)

有人建议如何在设备发现进行时成功连接此BT套接字吗?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

你正在尝试当bluothooth处于发现模式时你正在尝试。发现模式是非常重的过程所以它阻止。你应该在完成发现后尝试。你可以在用户尝试发现时尝试显示吐司以便用户将等待那段时间。