BluetoothSocket。连接超时

BluetoothSocket。连接超时,bluetooth,xamarin.android,Bluetooth,Xamarin.android,这件事让我毛骨悚然。我在网络上搜索了一个有效的例子,但没有找到 我正在尝试连接到蓝牙设备。原始代码在Java版本中工作 在调用Connect()之前,一切都正常工作,然后一切都挂起。我需要能够向蓝牙设备发送和接收数据,我的理解是必须首先调用Connect() 代码: //首先,检查Bluetooth是否可用 使用(BluetoothAdapter btAdapter=BluetoothAdapter.DefaultAdapter) { //如果没有适配器,则退出 if(btAdapter==nu

这件事让我毛骨悚然。我在网络上搜索了一个有效的例子,但没有找到

我正在尝试连接到蓝牙设备。原始代码在Java版本中工作

在调用Connect()之前,一切都正常工作,然后一切都挂起。我需要能够向蓝牙设备发送和接收数据,我的理解是必须首先调用Connect()

代码:

//首先,检查Bluetooth是否可用
使用(BluetoothAdapter btAdapter=BluetoothAdapter.DefaultAdapter)
{
//如果没有适配器,则退出
if(btAdapter==null)
{
//显示祝酒词
Toast.MakeText(m_上下文,“蓝牙未打开”,ToastLength.Long).Show();
//出口
返回false;
}
//我们找到了一个适配器,现在获取连接设备的列表
foreach(btAdapter.BondedDevices中的蓝牙设备)
{
//如果不是P25,请跳过
如果(!device.Name.ToLower()包含(“p25”))
继续;
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,
新JValue(1));
//我们找到了设备,确认它已经配对并打开了
使用(BluetoothSocket NewSocket=new Java.Lang.Object(socket.JavaCast())
{
尝试
{
//如果未创建任何内容,则跳过
if(NewSocket==null)
继续;
//连接
NewSocket.Connect();
//获取输出流
NewSocket.OutputStream.Close();
//合上插座
NewSocket.Close();
}
捕获(例外例外)
{
日志错误(“DeviceLink”,例外消息);
继续;
}
}
}
}

Hy,对不起,我的英语不好,我相信你开发了用于BlueBamboo的,我也有很多问题要用Monodroid在BlueBamboo PM25中打印,解决方法如下,创建一个包含3个全局变量的新类:

            public BluetoothAdapter Adapter;
            public BluetoothDevice RemoteDevice;
            public BluetoothSocket Socket;
现在是班上的老师

            Adapter = BluetoothAdapter.DefaultAdapter;

            if (Adapter == null) throw new NotSupportedException("Dispositivo sem suporte a bluetooth");
            if (!Adapter.IsEnabled) Adapter.Enable();

            foreach (var device in Adapter.BondedDevices)
            {
                if (!device.Name.ToLower().Contains("p25")) continue;

                RemoteDevice = Adapter.GetRemoteDevice(device.Address);
                Socket = RemoteDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));

                if (Socket == null) continue;

                Socket.Connect();
                break;


            }
实例仅当该类在您的活动中使用后,仅关闭套接字或在您的类中实现IDisposable

         public void Dispose()
         {
              Socket.Close();
         }

开心点

我知道这篇文章很旧,我在这里搜索其他东西,但我遇到了同样的问题,为了解决“挂起”问题,我在不同于UI线程的线程上运行连接部分,然后用结果更新UI线程。这对我来说是可行的…(顺便说一句,我在C#中使用Monodroid)

private void connectBTwThread()
{
新线程(新线程开始(()=>
{
TextView txtList=findviewbyd(Resource.Id.devicesList);
#区域连接例程
BluetoothSocket btSockets=bth.BondedDevices.Where(d=>d.Name.ToUpper().Contains(“JVC”)).Single()
.createrFComSocketToServiceRecord(_uuid);
尝试
{
RunOnUiThread(()=>
{
txtList.Text+=System.Environment.NewLine;
txtList.Text+=“正在尝试连接到”+btSockets.RemoteDevice.Name
+System.Environment.NewLine;
});
btSockets.Connect();
如果(btSockets.IsConnected)
RunOnUiThread(()=>{txtList.Text+=“连接到”+btSockets.RemoteDevice.Name;});
btSockets.Close();
}
捕获(IOE异常)
{
RunOnUiThread(()=>{txtList.Text+=“BT连接失败:”+e.Message+System.Environment.NewLine;});
尝试
{
btSockets.Close();
}
捕获(IOException关闭异常)
{
返回;
}
}
#端区
})).Start();
}
我发布它是为了回馈SO社区,希望有人能从中受益

         public void Dispose()
         {
              Socket.Close();
         }
 private void connectBTwThread()
    {
        new Thread(new ThreadStart(() =>
        {
            TextView txtList = FindViewById<TextView>(Resource.Id.devicesList);

            #region Connect routine

            BluetoothSocket btSockets = bth.BondedDevices.Where(d => d.Name.ToUpper().Contains("JVC")).Single()
                .CreateRfcommSocketToServiceRecord(_uuid);

            try
            {
                RunOnUiThread(() =>
                {
                    txtList.Text += System.Environment.NewLine;
                    txtList.Text += "Attempting to Connect to " + btSockets.RemoteDevice.Name
                        + System.Environment.NewLine;
                });

                btSockets.Connect();

                if (btSockets.IsConnected)
                    RunOnUiThread(() => { txtList.Text += "Connected to " + btSockets.RemoteDevice.Name; });

                btSockets.Close();
            }
            catch (IOException e)
            {
                RunOnUiThread(() => { txtList.Text += "BT Connect Failed: " + e.Message + System.Environment.NewLine; });
                try
                {
                    btSockets.Close();
                }
                catch (IOException closeException)
                {
                    return;
                }
            }
            #endregion

        })).Start();
    }