2016-04-03 14 views
0

私は数日間この作業をしていますが、問題を解決することはできません。私のBluetoothデバイスに接続できません

これは私が今持っているものである - >

ブルートゥースハンドラ

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サーバー

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のリスナー

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

私はしたいと思います接続する下記のコントローラで - だから、>

enter image description here

、私は、コントローラからのボタンイベントをキャッチしたいと思いますが、私はもう分からない。..

私はまた、既知のエラーを持っています: Java.IO.IOException: "read failed, socket might closed or timeout, read ret: -1

コントローラのUUIDがある:00001124-0000-1000-8000-00805f9b34fb

答えて

0

問題は解決しました。私はBluetoothソケットは必要ありませんでした。私はちょうどオーバーライドメソッド "KeyDown"と "KeyUp"を使用しました。かもしれない、読み取りが失敗したソケット:

IOExceptionが:読み取りが失敗し、ソケットは、その後、あなたがここに私の修正をお読みください閉じたかもしれません:それはあなたがソケットを必要としますが、IOExceptionがような例外を持っていれば

:)今素晴らしい作品閉じた - AndroidのBluetooth 4.3

0

私はあなたがコードをチェックして、あなたの設定があるかどうかを確認することができます私のGithub上のBluetooth(2.0)デバイスに接続するための一つの例を持っていますここで正しいのは、https://github.com/AlejandroRuiz/Mono/blob/master/Arduino/Bluetooth/MainActivity.csです。コードについて具体的な質問がある場合は、4.0 BLEに接続する方法が古い2.0と非常に異なるため、どのような種類のBluetoothを使用しているかを確認する必要があることをお知らせください。

+0

私はまだ問題がありますが、それはもう 'socket.Connect();'ではありません。それは解決されました:)本当に奇妙な問題でした。http://stackoverflow.com/a/36412975/2318669を参照してください。しかし、今私はボタンイベントをキャッチしたいが、読み込みが機能していない。あなたの 'beginListenForData()'メソッドもうまくいきませんでした。あなたはその問題を私に助けてくれますか? – Jamie

関連する問題