简体   繁体   English

Xamarin Android,蓝牙连接不起作用

[英]Xamarin Android, Bluetooth conection not working

I am trying to do a simple bluetooth connection Using Xamarin studio. 我正在尝试使用Xamarin工作室进行简单的蓝牙连接。 My code builds sucessfully but raises a Java.IO.IOExeption When I try to connect the bluetooth. 当我尝试连接蓝牙时,我的代码成功构建,但是引发了Java.IO.IOExeption。

This is a snippet from my code. 这是我的代码的一部分。

    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();
                    }


                }
            }

This is some of my Output from the log. 这是我的一些日志输出。

[Mono] Assembly Ref addref MatrixDisplay[0xb8da14b8] -> System.Core[0xb8e6b7e0]: 3 [BluetoothAdapter] 584852085: getState(). [单声道]程序集引用addref MatrixDisplay [0xb8da14b8]-> System.Core [0xb8e6b7e0]:3 [BluetoothAdapter] 584852085:getState()。 Returning 12 [BluetoothAdapter] getBluetoothService() called with no BluetoothManagerCallback [Choreographer] Skipped 295 frames! 返回没有调用BluetoothManagerCallback的12 [BluetoothAdapter] getBluetoothService()[Choreographer]跳过了295帧! The application may be doing too much work on its main thread. 该应用程序可能在其主线程上做太多工作。 [ViewRootImpl] Dropping event due to root view being removed: 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] Attempted to finish an input event but the input event receiver has already been disposed. [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,标志= 0x0,edgeFlags = 0x0,pointerCount = 1,historySize = 0,eventTime = 328245301,downTime = 328245292,deviceId = 1,source = 0x1002} [InputEventReceiver]试图完成输入事件,但输入事件接收器已经被处置了。 [InputEventReceiver] Attempted to finish an input event but the input event receiver has already been disposed. [InputEventReceiver]试图完成一个输入事件,但该输入事件接收器已被处置。

我通过在连接后不立即关闭插座来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM