Я вытягиваю свои волосы на этом. Я искал по всему Интернету рабочий пример и не смог его найти.BluetoothSocket.Connect Timeout
Я пытаюсь подключиться к устройству Bluetooth. Исходный код работает в версии Java.
Все работает до тех пор, пока не вызывается Connect(), а затем все просто зависает. Мне нужно иметь возможность отправлять и получать данные на устройство bluetooth, и я понимаю, что сначала нужно вызвать Connect().
Код:
// first, check that blue tooth is available
using (BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter)
{
// if no adapter, then exit
if (btAdapter == null)
{
// show toast message
Toast.MakeText(m_Context, "Bluetooth is not turned on", ToastLength.Long).Show();
// exit
return false;
}
// we found an adapter, now get the list of attached devices
foreach (BluetoothDevice device in btAdapter.BondedDevices)
{
// skip if not a P25
if (!device.Name.ToLower().Contains("p25"))
continue;
BluetoothDevice mdevice = btAdapter.GetRemoteDevice(device.Address);
IntPtr createRfcommSocket = JNIEnv.GetMethodID(
mdevice.Class.Handle,
"createRfcommSocket",
"(I)Landroid/bluetooth/BluetoothSocket;");
IntPtr socket = JNIEnv.CallObjectMethod(
mdevice.Handle,
createRfcommSocket,
new JValue(1));
// we found the device, confirm it's actually paired and turned on
using (BluetoothSocket NewSocket = new Java.Lang.Object(socket).JavaCast<BluetoothSocket>())
{
try
{
// skip if nothing created
if (NewSocket == null)
continue;
// connect
NewSocket.Connect();
// get the output stream
NewSocket.OutputStream.Close();
// close the socket
NewSocket.Close();
}
catch (Exception excep)
{
Log.Error("DeviceLink", excep.Message);
continue;
}
}
}
}