2015-10-14 126 views
0

我想要做一個簡單的藍牙連接使用Xamarin工作室。我的代碼成功構建,但引發了Java.IO.IOExeption當我嘗試連接藍牙時。Xamarin Android,藍牙連接不工作

這是我的代碼片段。

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] Assembly Ref addref MatrixDisplay [0xb8da14b8] - > System.Core [0xb8e6b7e0]:3 [BluetoothAdapter] 584852085:getState()。返回12 [BluetoothAdapter] getBluetoothService()調用不帶BluetoothManagerCallback [編舞]跳過了295幀!應用程序可能在其主線程上做了太多工作。由於刪除了根視圖,因此刪除了事件: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]試圖完成一個輸入事件,但輸入事件接收器已被處置。

+0

你確定清單許可已啓用嗎? – Kush

+0

是的我有必要的清單permision,藍牙和bluetooth_admin ... – Ogbe

回答

0

我解決了這個問題,因爲連接後不立即關閉套接字。