2017-11-24 2 views
0

나는 다음과 같은 메시지가, 단지 내 삼성 갤럭시 S7에 수신 블루투스 연결을 시도 0 : 내 넥서스 5 배에 동일한 코드를 사용블루투스 문제 : myUserId =

I/BT: Attempting to connect to Protocol: 00001105-0000-1000-8000-00805f9b34fb 
D/BluetoothAdapter: cancelDiscovery 
D/BluetoothUtils: isSocketAllowedBySecurityPolicy start : device null 
D/BluetoothSocket: connect(): myUserId = 0 
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
D/BluetoothSocket: getInputStream(): myUserId = 0 
D/BluetoothSocket: getOutputStream(): myUserId = 0 

저를 보내지 않습니다 "myUserId = 0".

"문제"는 내 앱과 다른 기기 간의 통신을 차단하는 것 같습니다.

private class btSend extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected void onPreExecute(){ 
      Log.i("BT Connection", "Establishing connection..."); 

     } 

     @Override 
     protected Void doInBackground(Void... devices) { 

      List<UUID> list = new ArrayList<>(); 

      for (ParcelUuid id : bluetoothDevice.getUuids()) { 
       list.add(id.getUuid()); 
      } 

      BluetoothSocketConnector bluetoothSocketConnector = new BluetoothSocketConnector(bluetoothDevice, false, bluetoothAdapter, list); 
      try { 
       bluetoothSocket = bluetoothSocketConnector.connect(); 

       inputStream = bluetoothSocket.getInputStream(); 
       outputStream = bluetoothSocket.getOutputStream(); 

       btConnectSuccess = true; 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result){ 
      super.onPostExecute(result); 

      if(btConnectSuccess) { 
       while(true){ 
        try { 
         sleep(5000); 
         sendData(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 


    } 

같이 sendData :

void sendData() throws IOException 
    { 
     String msg = "Test message\r\n"; 
     if (msg!=null) { 
      byte[] msgBuffer = msg.getBytes(); 
      try { 
       outputStream.write(msgBuffer); 
       Log.w("Bluetooth", "Data sent"+msgBuffer); 
      } catch (IOException e) { 
       Log.e("BT send error", e.getMessage(), e); 
      } 
     } 
    } 

BluetoothSocketConnector :

여기 내 코드의

public class BluetoothSocketConnector { 

    private BluetoothSocketWrapper bluetoothSocket; 
    private BluetoothDevice device; 
    private boolean secure; 
    private BluetoothAdapter adapter; 
    private List<UUID> uuidCandidates; 
    private int candidate; 


    /** 
    * @param device the device 
    * @param secure if connection should be done via a secure socket 
    * @param adapter the Android BT adapter 
    * @param uuidCandidates a list of UUIDs. if null or empty, the Serial PP id is used 
    */ 
    public BluetoothSocketConnector(BluetoothDevice device, boolean secure, BluetoothAdapter adapter, 
            List<UUID> uuidCandidates) { 
     this.device = device; 
     this.secure = secure; 
     this.adapter = adapter; 
     this.uuidCandidates = uuidCandidates; 

     if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) { 
      this.uuidCandidates = new ArrayList<UUID>(); 
      this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
     } 
    } 

    public BluetoothSocketWrapper connect() throws IOException { 
     boolean success = false; 
     while (selectSocket()) { 
      adapter.cancelDiscovery(); 

      try { 
       bluetoothSocket.connect(); 
       success = true; 
       break; 
      } catch (IOException e) { 
       //try the fallback 
       try { 
        bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket()); 
        Thread.sleep(500); 
        bluetoothSocket.connect(); 
        success = true; 
        break; 
       } catch (FallbackException e1) { 
        Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e); 
       } catch (InterruptedException e1) { 
        Log.w("BT", e1.getMessage(), e1); 
       } catch (IOException e1) { 
        Log.w("BT", "Fallback failed. Cancelling.", e1); 
       } 
      } 
     } 

     if (!success) { 
      throw new IOException("Could not connect to device: "+ device.getAddress()); 
     } 

     return bluetoothSocket; 
    } 

    private boolean selectSocket() throws IOException { 
     if (candidate >= uuidCandidates.size()) { 
      return false; 
     } 

     BluetoothSocket tmp; 
     UUID uuid = uuidCandidates.get(candidate++); 

     Log.i("BT", "Attempting to connect to Protocol: "+ uuid); 
     if (secure) { 
      tmp = device.createRfcommSocketToServiceRecord(uuid); 
     } else { 
      tmp = device.createInsecureRfcommSocketToServiceRecord(uuid); 
     } 
     bluetoothSocket = new NativeBluetoothSocket(tmp); 

     return true; 
    } 

    public static interface BluetoothSocketWrapper { 

     InputStream getInputStream() throws IOException; 

     OutputStream getOutputStream() throws IOException; 

     String getRemoteDeviceName(); 

     void connect() throws IOException; 

     String getRemoteDeviceAddress(); 

     void close() throws IOException; 

     BluetoothSocket getUnderlyingSocket(); 

    } 


    public static class NativeBluetoothSocket implements BluetoothSocketWrapper { 

     private BluetoothSocket socket; 

     public NativeBluetoothSocket(BluetoothSocket tmp) { 
      this.socket = tmp; 
     } 

     @Override 
     public InputStream getInputStream() throws IOException { 
      return socket.getInputStream(); 
     } 

     @Override 
     public OutputStream getOutputStream() throws IOException { 
      return socket.getOutputStream(); 
     } 

     @Override 
     public String getRemoteDeviceName() { 
      return socket.getRemoteDevice().getName(); 
     } 

     @Override 
     public void connect() throws IOException { 
      socket.connect(); 
     } 

     @Override 
     public String getRemoteDeviceAddress() { 
      return socket.getRemoteDevice().getAddress(); 
     } 

     @Override 
     public void close() throws IOException { 
      socket.close(); 
     } 

     @Override 
     public BluetoothSocket getUnderlyingSocket() { 
      return socket; 
     } 

    } 

    public class FallbackBluetoothSocket extends NativeBluetoothSocket { 

     private BluetoothSocket fallbackSocket; 

     public FallbackBluetoothSocket(BluetoothSocket tmp) throws FallbackException { 
      super(tmp); 
      try 
      { 
       Class<?> clazz = tmp.getRemoteDevice().getClass(); 
       Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE}; 
       Method m = clazz.getMethod("createRfcommSocket", paramTypes); 
       Object[] params = new Object[] {Integer.valueOf(1)}; 
       fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params); 
      } 
      catch (Exception e) 
      { 
       throw new FallbackException(e); 
      } 
     } 

     @Override 
     public InputStream getInputStream() throws IOException { 
      return fallbackSocket.getInputStream(); 
     } 

     @Override 
     public OutputStream getOutputStream() throws IOException { 
      return fallbackSocket.getOutputStream(); 
     } 


     @Override 
     public void connect() throws IOException { 
      fallbackSocket.connect(); 
     } 


     @Override 
     public void close() throws IOException { 
      fallbackSocket.close(); 
     } 

    } 

    public static class FallbackException extends Exception { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = 1L; 

     public FallbackException(Exception e) { 
      super(e); 
     } 

    } 
} 

답변

0

좋아, 내가 대답과 함께 한 다른 프로젝트 작업을 몇 일 후. .. 사실 "myUserId = 0"은 문제가 아니 었습니다. 리스너는 내 앱의 다른 곳에서 루프에 갇혀있었습니다.

이 로그 메시지가 동작에 영향을 미치지 않는 것 같습니다. 공식 Android 블루투스 데모에서 확인한 결과 동일한 메시지가 표시되었지만 잘 작동했습니다.