我正在尝试创建一个示例Xamarin Android应用程序,通过蓝牙连接设备。
当我尝试连接蓝牙插座时出现以下错误
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)
您可以在https://github.com/sstechGitHost/Bluetooth.git
找到我的样本谢谢, Nahid
答案 0 :(得分:3)
您正在与其他设备不支持的UUID配对的问题。以下是解决此问题的示例代码。请记住,这个解决方案可能需要一些稳健性,这只是概念的证明。人们说FetchUuidsWithSdp不是很稳定,所以你可能想注册广播动作以获得UUID或类似的东西。现在的代码。 而不是
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连接到另一台设备
0000111f-0000-1000-8000-00805f9b34fb
00001132-0000-1000-8000-00805f9b34fb
00000000-deca-fade-deca-deafdecacafe
但不是“ 连续“你正在使用。