gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:15:06 24 4
gpt4 key购买 nike

我在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com