2017-12-07 15 views
0

블루투스 기기 (구글 데이 드림 컨트롤러)의 모든 데이터를 안드로이드 애플리케이션의 특정 사용자 입력에 응답하려고합니다. 웹 응용 프로그램 (https://github.com/mrdoob/daydream-controller.js)이 필요한만큼 데이터를 읽는 것을 발견했습니다. 정말로 앱 버튼에 대한 버튼 피드백 만 있으면됩니다.블루투스 소켓을 연결하십시오.

하지만 연결할 수 없습니다! 연결할 때 항상 오류가 있습니다. 다른 사람들은 비슷한 경험을 가지고 있지만 다른 설정에 .. 나는 해결책

public class TryAgain extends AppCompatActivity { 

    private static final String UUID_SERIAL_PORT_PROFILE = "00001101-0000-1000-8000-00805F9B34FB"; 

    private static final String TAG = "tag"; 

    BluetoothAdapter mBluetoothAdapter; 
    BluetoothDevice mDevice; 
    private BluetoothSocket mSocket = null; 
    private BufferedReader mBufferedReader = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_try_again); 

     findBT(); 

     try { 
      openDeviceConnection(mDevice); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 


    void findBT() 
    { 
     mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if(mBluetoothAdapter == null) 
     { 
      System.out.println("No bluetooth adapter available"); 
     } 

     if(!mBluetoothAdapter.isEnabled()) 
     { 
      Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBluetooth, 0); 
     } 

     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
     if(pairedDevices.size() > 0) 
     { 
      for(BluetoothDevice device : pairedDevices) 
      { 
       if(device.getName().equals("Daydream controller")) 
       { 
        mDevice = device; 
        break; 
       } 
      } 
     } 
     System.out.println("Bluetooth Device Found"); 
    } 


    private void openDeviceConnection(BluetoothDevice device) 
      throws IOException { 
     InputStream aStream = null; 
     InputStreamReader aReader = null; 
     try { 
      mSocket = device.createRfcommSocketToServiceRecord(getSerialPortUUID()); 
      mSocket.connect(); 
      aStream = mSocket.getInputStream(); 
      aReader = new InputStreamReader(aStream); 
      mBufferedReader = new BufferedReader(aReader); 
     } catch (IOException e) { 
      Log.e(TAG, "Could not connect to device", e); 
      close(mBufferedReader); 
      close(aReader); 
      close(aStream); 
      close(mSocket); 
      throw e; 
     } 
    } 

    private void close(Closeable aConnectedObject) { 
     if (aConnectedObject == null) return; 
     try { 
      aConnectedObject.close(); 
     } catch (IOException e) { 
     } 
     aConnectedObject = null; 
    } 

    private UUID getSerialPortUUID() { 
     return UUID.fromString(UUID_SERIAL_PORT_PROFILE); 
    } 
} 

을 찾을 수 없어 첫 번째 단계는 리모콘을 얻을 수있는 정보를 데이터 장치가 전송하는 스트리밍 읽은 후 추출하는 것 정보. 물론 장치에 연결하는 것이 필수적이며 바로 지금 바로 그 부분에서 제가 어려움을 겪고 있습니다.

오류 메시지 :

java.io.IOException: read failed, socket might closed or timeout, read ret: -1 

답변

0

이 같은 배경 스레드에서 openDeviceConnection를 호출해야합니다

Thread yourBGThread = new Thread(){ 
    @Override 
    public void run() { 
     try { 
      openDeviceConnection(mDevice); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

}; 
yourBGThread.start(); 

findBT()

+0

다음은이 코드를 넣어 불행하게도이 결과에 아무것도 변경되지 않습니다 아직도 연결할 수 없습니다. – jan

+0

잘못된 UUID이거나 개념적으로 코드에 이상이있는 경우 확실하지 않습니다 .. – jan

+0

해결 된 코드가 완전히 변경되었습니다 ... – jan