마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

블루투스 장치 찾기문의

0 추천

블루투스 장치 찾기에서 

장치를 찾을 경우

사용가능한 다른 장치에서 

장치 이름이 null이 뜰때가 있습니다.

이부분을 아래와 같이 수정하면 되는건가요??

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                         if(device.getName()==null){
                                    device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                         }

                        mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());

                 }
}

 

 

 

또한 장치찾기한 후 같은 디바이스가 중복으로 출력이 되는 경우가 있습니다.

예를 들어서

A01

00:00:00:00:00:00

A01

00:00:00:00:00:00

A01

00:00:00:00:00:00

 

위와같이 중복으로 출력되는 현상이 있던데

이 경우는 어떻게 처리해야하나요??

A01

00:00:00:00:00:0D0

쿠쿠부다스 (6,470 포인트) 님이 2016년 12월 15일 질문
문자열 검사는 TextUtils.isEmpty(str) 을 쓰면 편합니다.

1개의 답변

0 추천

안녕하세요

공백 및 널 체크는 이렇게 하시면 될것 같습니다.

if(!device.getName().equalsIgnoreCase("") || device.getName() != null)

어댑터 안에 값과 받아온 디바이스 값을 중복체크 후 어댑터에 add 하시면 될 것 같습니다.

어댑터를 ArrayAdater<String> 컬렉션 객체로 만들어서 예시를 했습니다.

for(int i = 0; i < mNewDevicesArrayAdapter.getCount(); i++){

    if (!mNewDevicesArrayAdapter.getItem(i).equalsIgnoreCase("비교할대상"){
        mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
    }

}

수고하세요

히로시 (10,800 포인트) 님이 2016년 12월 15일 답변
안녕하세요 답변 감사합니다.
공백 및 널일 경우는
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
        if(!device.getName().equalsIgnoreCase("") || device.getName() !=null){
            mNewDevicesArrayAdapter.add(device.getName() + "\n" +                
                        device.getAddress());
        }
    }
}
식으로 진행을 하면 될거같은거는 이해가 됐습니다.

아래에 중복체크 후 add할려면
공백 및 널체크를 한 후 그안에 중복체크를 해서  어탭터에 add를 해야하는거로 말씀을 하시는 거로 생각이 됩니다.

if (BluetoothDevice.ACTION_FOUND.equals(action)) {
   BluetoothDevice device =      
       intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
   if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
     if(!device.getName().equalsIgnoreCase("") || device.getName() !=null){
    for(int i=0; i<mNewDevicesArrayAdapter.getCount(); i++ ){
         
   if(!mNewDevicesArrayAdapter.getItem(i).equalsIgnoreCase(device.getName())){
        mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());       
           }
    }
      }
   }
}

이런식으로 말씀이시간요??
위와 같이 진행하니 중복제거가 아닌 디바이스가 하나도 뜨지가 않네요 ㅠㅠ
아직 초짜라서 ㅠㅠ
...