目前我正在尝试在标签打印机RPP320上连接和打印文本。我正在使用C#和Xamarin.Forms。
所以我有三种不同的连接打印机的方式,所有这些方法都失败了。
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;
}
}
null
变量=&gt;返回method
var method = Device.GetType().GetMethod("createRfcommSocket");
null
变量=&gt;的method
var method = Device.GetType().GetMethod("createInsecureRfcommSocket");
mmSocket.Connect();
抛出异常:{Java.IO.IOException:读取失败,套接字可能关闭或超时, 读取ret:-1 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() &lt; 896ad1d315ca4ba7b117efb8dacaedcf&gt;中的[0x0000c]:0 Java.Interop.JniEnvironment + InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference实例,Java.Interop.JniMethodInfo 方法,Java.Interop.JniArgumentValue * args)[0x00069] in :0点 Java.Interop.JniPeerMembers + JniInstanceMethods.InvokeAbstractVoidMethod (System.String encodedMember,Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue *参数)[0x00014] in :0点 Android.Bluetooth.BluetoothSocket.Connect()[0x0000a] in &lt; 85286732fd894fbbba95d2215e5f9ec6&gt;:0 at DemoApp.Droid.Printing.ConnectThread.ProbeConnection() D:\ DemoApp中的[0x001a4] - 老\ DemoApp \ DemoApp \ DemoApp.Android \打印\ ConnectThread.cs:203 ---托管Java.IO.IOException堆栈跟踪结束--- java.io.IOException:读取失败,socket可能关闭或超时,读取 ret:-1 at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:907) 在 android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:866) 在 android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:540) 在 md5270abb39e60627f0f200893b490a1ade.ButtonRenderer_ButtonClickListener.n_onClick(母语 方法)at md5270abb39e60627f0f200893b490a1ade.ButtonRenderer_ButtonClickListener.onClick(ButtonRenderer_ButtonClickListener.java:30) 在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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)}
我不确定问题出在哪里,我是否应继续使用此方法并尝试修复它并更改我连接打印机的方式。
我在stackoverflow和其他平台上发现了关于这个问题的其他几个问题,但是大多数问题都是用Java编写的解决方案,或者在我的情况下不适用。
提前感谢您的帮助!
答案 0 :(得分:2)
我找到了另一种方法。以下是代码:
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();
}
这个对我有用。
我不认为这是连接的最佳选择,但它是一种解决方法,而我找到了以前代码的解决方案。
答案 1 :(得分:1)
我刚刚找到了解决这个问题的方法:D
IntPtr createRfcommSocket = JNIEnv.GetMethodID(mmDevice.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(mmDevice.Handle, createRfcommSocket, new global::Android.Runtime.JValue(1));
socket = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);
试一试!