gpt4 book ai didi

c# - Xamarin.Forms - 读取失败,套接字可能关闭或超时,读取 ret : -1

转载 作者:行者123 更新时间:2023-11-30 16:42:40 27 4
gpt4 key购买 nike

目前我正在尝试连接标签打印机 RPP320 并在其上打印文本。我正在使用 C# 和 Xamarin.Forms。

所以我有 3 种不同的方式连接到打印机,但都失败了。

foreach (var parcelUuid in parcelUuids)
{
mBluetoothAdapter.CancelDiscovery();
//METHOD A

try
{
var method = Device.GetType().GetMethod("createRfcommSocket");
mmSocket = (BluetoothSocket)method.Invoke(Device, new object[] { Port });
mmSocket.Connect();
Debug.WriteLine("Connected...");
isConnected = true;
DoDeviceConnected();
break;
}
catch (Exception e)
{

}

//METHOD B

try
{
var method = Device.GetType().GetMethod("createInsecureRfcommSocket");
mmSocket = (BluetoothSocket)method.Invoke(Device, new object[] { Port });
mmSocket.Connect();
Debug.WriteLine("Connected...");
isConnected = true;
DoDeviceConnected();
break;
}
catch (Exception e)
{

}

}

if (!isConnected)
{
//METHOD C

try
{
IntPtr createRfcommSocket = JNIEnv.GetMethodID(Device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
// JNIEnv.GetMethodID(device.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(Device.Handle, createRfcommSocket, new global::Android.Runtime.JValue(Port));
mmSocket = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);
/*
mmSocket =
mBluetoothAdapter.GetRemoteDevice(Device.Address)
.CreateRfcommSocketToServiceRecord(
UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));
*/
mmSocket.Connect();
DoDeviceConnected();
}
catch (IOException connectException)
{

Debug.WriteLine("IO Error: " + connectException.Message);
// Unable to connect; close the socket and get out
try
{
mmSocket.Close();
}
catch (IOException closeException)
{
Debug.WriteLine("Error: " + closeException.Message);
}
throw new Exception(connectException.Message);
return;
}
}
  • 在方法 A 中,此代码为 method 变量返回 null => var method = Device.GetType().GetMethod("createRfcommSocket");
  • 在方法 B 中,此代码为 method 变量返回 null => var method = Device.GetType().GetMethod("createInsecureRfcommSocket");
  • 在方法 C 中,此代码 => mmSocket.Connect(); 抛出异常:

{Java.IO.IOException: read failed, socket might closed or timeout, read ret: -1 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in :0 at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in :0 at Android.Bluetooth.BluetoothSocket.Connect () [0x0000a] in <85286732fd894fbbba95d2215e5f9ec6>:0 at DemoApp.Droid.Printing.ConnectThread.ProbeConnection () [0x001a4] in D:\DemoApp - old\DemoApp\DemoApp\DemoApp.Android\Printing\ConnectThread.cs:203 --- 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:907) at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:866) at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:540) at md5270abb39e60627f0f200893b490a1ade.ButtonRenderer_ButtonClickListener.n_onClick(Native Method) at md5270abb39e60627f0f200893b490a1ade.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30) at android.view.View.performClick(View.java:6199) at android.widget.TextView.performClick(TextView.java:11090) at android.view.View$PerformClick.run(View.java:23647) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6724) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) }

我不确定问题出在哪里,我是否应该继续使用这种方法并尝试修复它并更改我连接到打印机的方式。

我在 stackoverflow 和其他平台上发现了几个关于这个问题的其他问题,但其中大部分是用 Java 编写的解决方案,或者不适用于我的情况。

在此先感谢您的帮助!

最佳答案

我找到了另一种方法。下面是代码:

if (this.mBluetoothAdapter.IsEnabled)
{
BluetoothDevice device = this.mBluetoothAdapter.GetRemoteDevice(Device.Address);

var serialUUID = UUID.FromString("00001101-0000-1000-8000-00805f9b34fb");

BluetoothSocket socket = device.CreateRfcommSocketToServiceRecord(serialUUID);
socket.Connect();
}

这个对我有用。

我不认为这是连接的最佳选择,但它是一种解决方法,同时我找到了以前代码的解决方案。

关于c# - Xamarin.Forms - 读取失败,套接字可能关闭或超时,读取 ret : -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433911/

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