Error: socket might closed or timeout, read ret: -1
Hello guys,
I am getting the above error when I connect to device blutooth, this only happens when I am going to print a variable inside For
Could someone tell me how I can handle this problem?
Follow the code I'm using
foreach (BluetoothDevice d in bthD)
{
if (d.Name.ToString().Trim() == "MPT-III")
{
Java.Util.UUID UUID = Java.Util.UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothSocket s = null;
s = d.CreateRfcommSocketToServiceRecord(UUID);
for (int i = 0; i < sparArray.Count; i++)
{
s = d.CreateInsecureRfcommSocketToServiceRecord(UUID);
string agroquimico = mitens[i].ToString();
clslogin login = new clslogin();
int agroquimico_id = int.Parse(agroquimico.Substring(0, agroquimico.IndexOf(" |")));
string pega_apaga = agroquimico.Substring(0, agroquimico.IndexOf(" / "));
pega_dose = agroquimico.Replace(pega_apaga, "").Replace(" / ", "");
pega_dose = pega_dose.Substring(0, pega_dose.IndexOf("--"));
byte[] buffer = System.Text.Encoding.GetEncoding(0).GetBytes(agroquimico + "\n");
IntPtr createRfcommSocket = JNIEnv.GetMethodID(d.Class.Handle, "createRfcommSocket", "(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr _socket = JNIEnv.CallObjectMethod(d.Handle, createRfcommSocket, new Android.Runtime.JValue(1));
s = Java.Lang.Object.GetObject<BluetoothSocket>(_socket, JniHandleOwnership.TransferLocalRef);
bth1.CancelDiscovery();
try
{
s.Connect();
s.OutputStream.Write(buffer, 0, buffer.Length);
s.Close();
}
catch
{
//Toast.MakeText(this, "Failed to connect: " + connectException.Message, ToastLength.Short).Show();
Toast.MakeText(this, "Failed to connect", ToastLength.Short).Show();
}
}
}
}