2016-06-03 3 views
0

내 ListeningThread가 기압계 활동의 UI 스레드를 고정시키고 있습니다. 왜 이런 일이 일어나는지 모르겠습니다. 필요한 도움말 :Android : 블루투스 청취 스레드가 UI 스레드를 차단합니다.

이 내 ListeningThread입니다 :

public class ListeningThread extends Thread { 

String TAG = "bluetoothThread"; 

private final BluetoothServerSocket bluetoothServerSocket; 
BluetoothAdapter bl; 

public ListeningThread(BluetoothAdapter bluetoothAdapter, String appName) { 
    BluetoothServerSocket temp = null; 
    bl = bluetoothAdapter; 
    try { 
     temp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(appName, Constants.MY_UUID_SECURE); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    bluetoothServerSocket = temp; 
} 

public void start() { 
    Log.d(TAG, "ListeningThread running"); 
    BluetoothSocket bluetoothSocket; 
    //This will block while listening until a BluetoothSocket is returned or an exception occurs 
    while (true) { 
     try { 
      bluetoothSocket = bluetoothServerSocket.accept(); 
      Log.d(TAG, "ListeningThread connected"); 
     } catch (IOException e) { 
      break; 
     } 
     // If a connection is accepted 
     if (bluetoothSocket != null) { 

      // Manage the connection in a separate thread 

      try { 
       bluetoothServerSocket.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      break; 
     } 
    } 
} 

// Cancel the listening socket and terminate the thread 
public void cancel() { 
    try { 
     bluetoothServerSocket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

그리고 이것은 스레드 동결 사용하여 활동 : 나는 문제가 실행 방법에 어딘가에 확신

public class BarometerActivity extends AppCompatActivity { 

BluetoothAdapter bluetoothAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_barometer); 
    if (SingletonBluetoothAdapter.bluetoothAdapter == null) { 
     SingletonBluetoothAdapter.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    } 
    bluetoothAdapter = SingletonBluetoothAdapter.bluetoothAdapter; 

    ListeningThread t = new ListeningThread(bluetoothAdapter, getString(R.string.app_name)); 
    t.start(); 
} 

} 

을 스레드의 나는이 방법을 주석으로 둘러 쌌다가 모든 것이 잘 돌아갔다.

답변

관련 문제