Android蓝牙套接字连接失败

Android蓝牙套接字连接失败

本文介绍了Xamarin Android蓝牙套接字连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个示例Xamarin Android应用程序,通过蓝牙连接设备。

I am trying to create a sample Xamarin Android app to connect devices over bluetooth.

我尝试连接蓝牙插座时出现错误

I get below error when i try to connect the bluetooth socket

  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3819/96c7ba6c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
  at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00084] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11643
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00017] in /Users/builder/data/lanes/3819/96c7ba6c/source/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:15
  at Android.Bluetooth.BluetoothSocket.Connect () [0x00000] in /Users/builder/data/lanes/3819/96c7ba6c/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Bluetooth.BluetoothSocket.cs:141
  at Bluetooth.Droid.Activities.Bluetooth.BluetoothActivity.PairedDevicesListView_ItemClick (System.Object sender, Android.Widget.AdapterView+ItemClickEventArgs e) [0x00094] in C:\Git\Bluetooth\Bluetooth\Bluetooth.Droid\Activities\Bluetooth\BluetoothActivity.cs:92
  --- End of managed Java.IO.IOException stack trace ---
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517)
    at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528)
    at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
    at mono.android.widget.AdapterView_OnItemClickListenerImplementor.n_onItemClick(Native Method)
    at mono.android.widget.AdapterView_OnItemClickListenerImplementor.onItemClick(AdapterView_OnItemClickListenerImplementor.java:30)
    at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
    at android.widget.AbsListView$3.run(AbsListView.java:3860)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

您可以在

感谢,
Nahid

Thanks,Nahid

推荐答案

与UUID配对的问题不受另一台设备支持。这里是解决这个示例代码。请记住这个解决方案可能需要一些鲁棒性,这只是概念的证明。人们说,FetchUuidsWithSdp不是很稳定,所以你可能想要注册广播行动获得UUID或类似的东西。现在的代码。
而不是

The problem that you are pairing with UUID which is not supported on another device. Here is the sample code to solve this. Please keep in mind this solution might require some robustness, this is just proof of concept. People says that FetchUuidsWithSdp is not very stable, so you might want to register for broadcast action to get UUIDs or something like that. Now the code.Instead of

BluetoothSocket btSocket = btDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));

我使用

ParcelUuid[] uuids = null;
                if (btDevice.FetchUuidsWithSdp())
                {
                    uuids = btDevice.GetUuids();
                }
                if ((uuids != null) && (uuids.Length > 0))
                {
                    foreach (var uuid in uuids)
                    {
                        try
                        {
                            btSocket = btDevice.CreateRfcommSocketToServiceRecord(uuid.Uuid);
                            btSocket.Connect();
                            break;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ex: " + ex.Message);
                        }
                    }
                }

具有UUID的另一台设备

and was able to connect to another device with UUIDs

0000111f-0000-1000-8000-00805f9b34fb
00001132-0000-1000-8000-00805f9b34fb
00000000-deca-fade-deca-deafdecacafe

你正在使用。

这篇关于Xamarin Android蓝牙套接字连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:55