Reputation:
Currently I am trying to connect and print text on label printer RPP320. I am using C# and Xamarin.Forms.
So what I have are 3 different ways to connect to printer and all of them are failing.
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
for method
variable => var method = Device.GetType().GetMethod("createRfcommSocket");
null
for method
variable => var method = Device.GetType().GetMethod("createInsecureRfcommSocket");
mmSocket.Connect();
throws exception:{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) }
I am not sure where the problem is, should I continue with this approach and try fix it and change the way I'm connecting to printer.
I found several other questions about this issue in stackoverflow and other platforms, but most of them we're with solution written in Java or weren't applicable in my case.
Thanks in advance for help !
Upvotes: 4
Views: 2199
Reputation:
I found another way of doing it. Here is the following code:
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();
}
This one work for me.
I don't think this is the best option of connecting, but it's kind of work-around, while I find solution with previous code.
Upvotes: 3
Reputation: 590
I just found a way of getting this to work :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);
Give it a shot!
Upvotes: 1