【问题标题】:Xamarin Android, Bluetooth conection not workingXamarin Android,蓝牙连接不起作用
【发布时间】:2015-10-14 14:05:26
【问题描述】:

我正在尝试使用 Xamarin Studio 进行简单的蓝牙连接。我的代码构建成功,但当我尝试连接蓝牙时引发 Java.IO.IOExeption。

这是我的代码中的一个 sn-p。

    BluetoothAdapter  BluetoothAdapter1 = BluetoothAdapter.DefaultAdapter;
    BluetoothSocket socket;
    BluetoothDevice device
    btnConnect.Click += async delegate {
            if (BluetoothAdapter1 == null)
            {
                AlertDialog1.SetMessage("Device Does not support bluetooth");

                AlertDialog1.Show();
            }


            if(!BluetoothAdapter1.IsEnabled)
            {
                AlertDialog1.SetMessage("Bluetooth is not enabled");
                AlertDialog1.Show();
            }

            else{

                device = (from bd in BluetoothAdapter1.BondedDevices where bd.Name == "GT-N8000" select bd).FirstOrDefault();

                if(device == null)
                {
                    AlertDialog1.SetMessage("Device Not Found \n Make sure you are paired to the bluetooth device");
                    AlertDialog1.Show();

                }

                else
                {
                    ParcelUuid[] list = device.GetUuids();
                    String MyUUID = list[0].ToString();



                    try 
                    {
                    socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString(MyUUID));



                        socket.Connect();
                    btnConnect.Text = socket.RemoteDevice.Name;

                    socket.Close();
                }

                    catch (Exception e)
                    {
                        AlertDialog1.SetMessage(e.Message);
                        AlertDialog1.Show();
                    }


                }
            }

这是我的一些日志输出。

[Mono] 程序集参考 addref MatrixDisplay[0xb8da14b8] -> System.Core[0xb8e6b7e0]: 3 [蓝牙适配器] 584852085:getState()。返回 12 [BluetoothAdapter] 在没有 BluetoothManagerCallback 的情况下调用 getBluetoothService() 【编舞】跳过295帧!应用程序可能在其主线程上做了太多工作。 [ViewRootImpl] 由于根视图被删除而导致的丢弃事件:MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=599.4283, y[0]=596.4481, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=328245301, downTime=328245292, deviceId=1, source=0x1002 } [InputEventReceiver] 尝试完成输入事件,但输入事件接收器已被释放。 [InputEventReceiver] 尝试完成输入事件,但输入事件接收器已被释放。

【问题讨论】:

  • 你确定manifest权限开启了吗?
  • 是的,我有必要的清单权限,蓝牙和 bluetooth_admin...

标签: c# android xamarin


【解决方案1】:

我通过在连接后不立即关闭套接字来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多