2016-04-03 17 views
0

Günlerdir bunun üzerinde çalışıyorum, fakat sorunu çözemiyorum. Bluetooth cihazımla bağlanamıyorum

Bu

şu anda var ne ->

Bluetooth işleyicisi

protected BluetoothAdapter bluetoothAdapter; 
    protected BluetoothServer btServer; 
    protected BluetoothSocket btSocket; 
    protected BluetoothDevice pairedBTDevice; 
    protected BluetoothListener btListener; 
    protected ParcelUuid uuid; 

    public BluetoothHandler() 
    { 
     BluetoothAdapter = null; 
    } 

    public void Initialize() 
    { 
     BluetoothAdapter = BluetoothAdapter.DefaultAdapter; 

     // Check if it has a bluetooth interface 
     if (BluetoothAdapter == null) 
     { 
      Console.WriteLine("Bluetooth is not available!"); 
      return; 
     } 

     // Check if bluetooth interface is enabled 
     if (!BluetoothAdapter.IsEnabled) 
     { 
      BluetoothAdapter.Enable(); 
     } 

     int count = 0; 

     // Get all the devices in the bluetooth list 
     var listDevices = BluetoothAdapter.BondedDevices; 
     if (listDevices.Count > 0) 
     { 
      foreach (var btDevice in listDevices) 
      { 
       // Get the specific controller 
       if (btDevice.Name == "MOCUTE-032_B52-CA7E") 
       { 
        UUID = btDevice.GetUuids().ElementAt(count); 
        pairedBTDevice = btDevice; 
       } 
       count++; 
      } 
     } 

     // Check if bluetooth is enabled 
     // Check if there is a device 
     if (BluetoothAdapter.IsEnabled && pairedBTDevice != null) 
     { 
      // Check if it's paired 
      if (pairedBTDevice.BondState == Bond.Bonded) 
      { 
       // First start the server 
       btServer = new BluetoothServer(this); 

       Thread.Sleep(1000); 

       // Start a new thread 
       Thread thread = new Thread(new ThreadStart(Connect)); 
       thread.Start(); 
      } 
     } 
    } 

    protected void Connect() 
    { 
     // Check if there is no socket already 
     if (btSocket == null) 
     { 
      try 
      { 
       btSocket = pairedBTDevice.CreateRfcommSocketToServiceRecord(UUID.Uuid); 
      } 
      catch (IOException ex) 
      { 
       throw ex; 
      } 
     } 

     try 
     { 
      Console.WriteLine("Attempting to connect..."); 

      // Create a socket connection 
      btSocket.Connect(); 
     } 
     catch 
     { 
      Console.WriteLine("Connection failed..."); 
      Console.WriteLine("Attempting to connect..."); 
      try 
      { 
       btSocket = pairedBTDevice.CreateInsecureRfcommSocketToServiceRecord(UUID.Uuid); 
       btSocket.Connect(); 
      } 
      catch 
      { 
       Console.WriteLine("Connection failed..."); 
       return; 
      } 
     } 

     Console.WriteLine("Client socket is connected!"); 

     Read(); 
    } 

    protected void Read() 
    { 
     btListener = new BluetoothListener(); 
     btListener.Read(btSocket); 
    } 

Bluetooth sunucu

private BluetoothHandler bluetoothHandler; 
    private BluetoothServerSocket serverSocket; 
    private Thread thread; 

    public BluetoothServer(BluetoothHandler bluetoothHandler) 
    { 
     this.bluetoothHandler = bluetoothHandler; 

     BluetoothServerSocket tmp = null; 

     try 
     { 
      tmp = bluetoothHandler.BluetoothAdapter.ListenUsingRfcommWithServiceRecord("MOCUTE-032_B52-CA7E", bluetoothHandler.UUID.Uuid); 
     } 
     catch (IOException ex) 
     { 
      throw ex; 
     } 

     serverSocket = tmp; 

     thread = new Thread(new ThreadStart(Run)); 
     thread.Start(); 
    } 

    protected void Run() 
    { 
     System.Console.WriteLine("Server is running..."); 
     while (true) 
     { 
      try 
      { 
       serverSocket.Accept(); 
      } 
      catch (IOException ex) 
      { 
       System.Console.WriteLine("FAILED! === > " + ex); 
      } 
     } 
    } 

Bluetooth dinleyici

protected Stream mmInStream; 

    public void Read(BluetoothSocket socket) 
    { 
     Stream tmpIn = null; 

     try 
     { 
      tmpIn = socket.InputStream; 
     } 
     catch (IOException ex) 
     { 
      throw ex; 
     } 

     mmInStream = tmpIn; 

     Thread thread = new Thread(new ThreadStart(Run)); 
     thread.Start(); 
    } 

    protected void Run() 
    { 
     byte[] buffer = new byte[1024]; 
     int bytes; 

     Console.WriteLine("Waiting for events..."); 

     while (true) 
     { 
      try 
      { 
       if (mmInStream.IsDataAvailable()) 
       { 
        bytes = mmInStream.Read(buffer, 0, buffer.Length); 
       } 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 
    } 

İsterim bağlamak Aşağıdaki kontrolörü ile - Yani>

enter image description here

, ben denetleyicisinden düğmeleri olayları yakalamak istiyorum ama

Ben de bilinen hata var .. artık hiçbir fikrim yok: kumandanın Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1

UUID: 00001124-0000-1000-8000-00805f9b34fb

cevap

0

Sorun çözüldü. Bluetooth yuvasına ihtiyacım yoktu. Az önce "KeyDown" ve "KeyUp" yöntemlerini kullandım.

IOException: Başarısız okumak, soket olabilir başarısız okumak, soket o zaman burada benim saptamak okumalısınız kapalı olabilir: Bu bir soket ihtiyaç ve IOException gibi bir istisna var ise harika şimdi :)

işleri kapalı - Android 4.3 üzerinde Bluetooth

0

sana kodu kontrol ve kurulum olup olmadığını görebilirsiniz benim Github Bluetooth (2.0) cihaza bağlanmak için bir örnek var Burada kod hakkında herhangi bir özel bir sorunuz varsa, lütfen https://github.com/AlejandroRuiz/Mono/blob/master/Arduino/Bluetooth/MainActivity.cs bağlantıyı doğrulayın lütfen bana bildirin, ayrıca ne tür bir bluetooth kullandığınızdan emin olmanız gerekir çünkü bir 4.0 BLE'ye bağlanmanın yolu, eski 2.0

+0

Cevabınız için teşekkürler, ben hala bir sorunum var, ama 'socket.Connect(); 'ile ilgili değil. Bu çözüldü :) Gerçekten garip bir problemdi, bkz http://stackoverflow.com/a/36412975/2318669. Ancak, şimdi düğme olaylarını yakalamak istiyorum, ancak okuma çalışmıyor. 'BeginListenForData()' yönteminiz de işe yaramadı. Bana bu konuda yardımcı olabilir misiniz? – Jamie