android - 在设备发现开启时连接蓝牙套接字

标签 android bluetooth

我在我的 andorid 应用程序中使用蓝牙套接字连接来连接近距离的医疗设备。我想通过单击 ListView 上的特定设备来连接蓝牙套接字,而蓝牙发现尚未完成。 我正在使用以下代码连接蓝牙 socket 以连接到蓝牙范围内的设备。

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 套接字吗?

非常感谢任何帮助。

最佳答案

您收到错误消息是因为您在蓝牙处于发现模式时正在尝试。发现模式是非常繁重的过程,因此它会阻塞。您应该在完成发现后尝试。您可以在用户尝试发现时显示 toast ,以便用户会等到那个时候。

关于android - 在设备发现开启时连接蓝牙套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22964423/

相关文章:

javascript - BluetoothSerial 插件不适用于 ionic

android - 在 Android Marshmallow 中请求多个蓝牙权限

ios - 我可以在 iOS 中测量蓝牙信号强度吗?

android.provider.Settings.ACTION_BLUETOOTH_SETTINGS 在三星上崩溃

java - Android android.intent.action.SEND youtube 视频

android - 如何访问使用 MIFARE 应用程序目录结构的 MIFARE Classic 卡?

java - 无法停止/更新处理程序 (removeCallbacksAndMessages)

android数据绑定(bind)使用 "&&"逻辑运算符

android - 点击时显示 Flutter 文本选择工具栏溢出

ios - CoreBluetooth 与非 iOS 设备的连接