2016-07-25 2 views
0

조각 내에서 Bluetooth 연결의 상태를 알려주기 위해 TextView를 변경하려하지만 msgReceived.setText(string)이 호출 될 때 아무 것도 일어나지 않는 것 같습니다. 이 일을 어떻게 하죠?동일한 활동의 ​​조각 내 TextView 텍스트를 변경할 수 없습니다.

dleedesign.dubcommunicationstestapp D/BluetoothService: Bounded device 40:EF:4C:C2:A9:32 
dleedesign.dubcommunicationstestapp D/BluetoothService: Bounded device 98:D3:31:70:80:C5 
dleedesign.dubcommunicationstestapp D/BluetoothService: start 
dleedesign.dubcommunicationstestapp D/BluetoothService: setState() 0 -> 1 
dleedesign.dubcommunicationstestapp W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
dleedesign.dubcommunicationstestapp D/BluetoothService: Socket Type: nullBEGIN mAcceptThreadThread[Thread-20627,5,main] 
dleedesign.dubcommunicationstestapp D/BluetoothService: connect to: 98:D3:31:70:80:C5 
dleedesign.dubcommunicationstestapp D/BluetoothService: setState() 1 -> 2 
dleedesign.dubcommunicationstestapp D/Main: Btservice started- listening 
dleedesign.dubcommunicationstestapp I/BluetoothService: BEGIN mConnectThread SocketType:null 
dleedesign.dubcommunicationstestapp W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_STATE_CHANGE: 1 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_STATE_CHANGE: 2 
dleedesign.dubcommunicationstestapp E/BluetoothService: Unable to connect socket 
                         java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
                          at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517) 
                          at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528) 
                          at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320) 
                          at dleedesign.dubcommunicationstestapp.Bluetooth$ConnectThread.run(Bluetooth.java:408) 
dleedesign.dubcommunicationstestapp D/BluetoothService: start 
dleedesign.dubcommunicationstestapp D/BluetoothService: setState() 2 -> 1 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_TOAST { when=-15ms what=5 target=dleedesign.dubcommunicationstestapp.SecondFragment$1 } 
dleedesign.dubcommunicationstestapp D/Main: MESSAGE_STATE_CHANGE: 1 
+0

은 무엇입니까 (버튼 사용하려고하는 등 다른 텍스트 뷰와 동일)

msgReceived = (TextView) myView.findViewById(R.id.msgReceived); 

, 레이아웃을 팽창한다 네가 본 메시지? 또한 logcat 스 니펫이 유용 할 수 있습니다. IMHO 다른 스레드에서 블루투스를 사용하고 UI 스레드에 정보를 표시하려면 'AsyncTask'와 같은 것을 사용해야합니다. –

+0

더 효율적인 방법이 되겠습니까? –

답변

1

을 시도 :

package dleedesign.dubcommunicationstestapp; 

import android.app.Fragment; 
import android.bluetooth.BluetoothAdapter; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

public class SecondFragment extends Fragment implements View.OnClickListener { 

View myView; 

public final String TAG = "Main"; 
private Bluetooth bt; 
public Button sendCommand; 
public TextView msgReceived; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    myView = inflater.inflate(R.layout.second_layout, container, false); 

    sendCommand = (Button) inflater.inflate(R.layout.second_layout, container, false).findViewById(R.id.sendCommand); 
    msgReceived = (TextView) inflater.inflate(R.layout.second_layout, container, false).findViewById(R.id.msgReceived); 
    msgReceived.setText("Ready to connect"); 

    bt = new Bluetooth(new MainActivity(), mHandler); 
    connectService(); 

    return myView; 
} 

@Override 
public void onClick(View v) 
{ 
    connectService(); 
} 

public void connectService() 
{ 
    try { 
     msgReceived.setText("Connecting..."); 
     BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if(btAdapter.isEnabled()) 
     { 
      bt.start(); 
      bt.connectDevice("HC-06"); 
      Log.d(TAG, "Btservice started- listening"); 
      msgReceived.setText("Connected!"); 
     } 
     else 
     { 
      Log.w(TAG, "Btservice started - bluetooth is not enabled"); 
      msgReceived.setText("Bluetooth not enabled"); 
     } 
    } catch (Exception e) { 
     Log.e(TAG, "Unable to start bluetooth", e); 
     msgReceived.setText("Unable to connect: " + e); 
    } 
} 

private final Handler mHandler = new Handler() 
{ 
    @Override 
    public void handleMessage(Message msg) 
    { 
     switch (msg.what) 
     { 
      case Bluetooth.MESSAGE_STATE_CHANGE: 
       Log.d(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1); 
       break; 
      case Bluetooth.MESSAGE_WRITE: 
       Log.d(TAG, "MESSAGE_WRITE"); 
       break; 
      case Bluetooth.MESSAGE_READ: 
       Log.d(TAG, "MESSAGE_READ"); 
       break; 
      case Bluetooth.MESSAGE_DEVICE_NAME: 
       Log.d(TAG, "MESSAGE_DEVICE_NAME " + msg); 
       break; 
      case Bluetooth.MESSAGE_TOAST: 
       Log.d(TAG, "MESSAGE_TOAST " + msg); 
       break; 
     } 
    } 
}; 

} 

편집 : 나는 SecondFragment을 선택하면 다음 밖으로 뱉어 로그 캣 메시지가 여기에 조각 내 자바 파일입니다 무슨 일이 일어나면 findViewById 할 때마다 레이아웃이 지정된 뷰를 부 풀린 다음 Tex로 캐스팅하려고합니다. tView. 이것은 findViewById을 다른 견해에서 호출하고 있습니다. 당신이 다음

myview = inflater.inflate.... 

와 함께 첫 번째 줄에 그랬던 것처럼 먼저

+1

버튼이 잘 작동하지 않기 때문에 괜찮습니다. 설명 주셔서 감사합니다! –

+0

당신은 오신 것을 환영합니다! – wanpanman

0

msgReceived = (TextView) myView.findViewById(R.id.msgReceived); 
+0

감사합니다. –

+0

당신은 오신 것을 환영합니다! 응답으로 그것을 유효하게하고 upvote 그것을 사람들이 그것이 좋은 것다는 것을 인식 할 수있는 시간을, 걸릴주십시오, 환호! –

+0

모든 UI 요소가 포함 된 해당 메서드를 사용하는 것이 좋습니다 –

관련 문제