블로그를 보고 따라했는데 페어링된 기기찾기는 되는데 새로운 디바이스 찾기가 안되네요.
저는 갤6사용하고있고 주변에 갤럭시a8기종으로 블루투스를 켜놓고 테스트를 하고있어요.
그리고 혹시몰라 BT스피커도 페어링 상태로 켜놨어요
그런데 onReceiver함수에서 Toast로 "검색 시작"은 뜨는데 좀 있다가 "검색 종료"라고 뜨고 새로운 디바이스를 못찾고있어요.
public class MainActivity extends AppCompatActivity {
//BluetoothAdapter
BluetoothAdapter mBluetoothAdapter;
//블루투스 요청 액티비티 코드
final static int BLUETOOTH_REQUEST_CODE = 100;
//UI
TextView txtState;
Button btnSearch;
ListView listDevice;
//Adapter
SimpleAdapter adapterDevice;
//list - Device 목록 저장
List<Map<String,String>> dataDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//UI
txtState = (TextView)findViewById(R.id.txtState);
btnSearch = (Button)findViewById(R.id.btnSearch);
listDevice = (ListView)findViewById(R.id.listDevice);
//Adapter
dataDevice = new ArrayList<>();
adapterDevice = new SimpleAdapter(this, dataDevice, android.R.layout.simple_list_item_2, new String[]{"name","address"}, new int[]{android.R.id.text1, android.R.id.text2});
listDevice.setAdapter(adapterDevice);
//블루투스 지원 유무 확인
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//블루투스를 지원하지 않으면 null을 리턴한다
if(mBluetoothAdapter == null){
Toast.makeText(this, "블루투스를 지원하지 않는 단말기 입니다.", Toast.LENGTH_SHORT).show();
finish();
return;
}
//블루투스 브로드캐스트 리시버 등록
//리시버1
IntentFilter stateFilter = new IntentFilter();
stateFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //BluetoothAdapter.ACTION_STATE_CHANGED : 블루투스 상태변화 액션
registerReceiver(mBluetoothStateReceiver, stateFilter);
//리시버2
IntentFilter searchFilter = new IntentFilter();
searchFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //BluetoothAdapter.ACTION_DISCOVERY_STARTED : 블루투스 검색 시작
searchFilter.addAction(BluetoothDevice.ACTION_FOUND); //BluetoothDevice.ACTION_FOUND : 블루투스 디바이스 찾음
searchFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //BluetoothAdapter.ACTION_DISCOVERY_FINISHED : 블루투스 검색 종료
registerReceiver(mBluetoothSearchReceiver, searchFilter);
//1. 블루투스가 꺼져있으면 활성화
// if(!mBluetoothAdapter.isEnabled()){
// mBluetoothAdapter.enable(); //강제 활성화
// }
//2. 블루투스가 꺼져있으면 사용자에게 활성화 요청하기
if(!mBluetoothAdapter.isEnabled()){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BLUETOOTH_REQUEST_CODE);
}
}
//블루투스 상태변화 BroadcastReceiver
BroadcastReceiver mBluetoothStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//BluetoothAdapter.EXTRA_STATE : 블루투스의 현재상태 변화
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
//블루투스 활성화
if(state == BluetoothAdapter.STATE_ON){
txtState.setText("블루투스 활성화");
}
//블루투스 활성화 중
else if(state == BluetoothAdapter.STATE_TURNING_ON){
txtState.setText("블루투스 활성화 중...");
}
//블루투스 비활성화
else if(state == BluetoothAdapter.STATE_OFF){
txtState.setText("블루투스 비활성화");
}
//블루투스 비활성화 중
else if(state == BluetoothAdapter.STATE_TURNING_OFF){
txtState.setText("블루투스 비활성화 중...");
}
}
};
//블루투스 검색결과 BroadcastReceiver
BroadcastReceiver mBluetoothSearchReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch(action){
//블루투스 디바이스 검색 종료
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
dataDevice.clear();
Toast.makeText(MainActivity.this, "블루투스 검색 시작", Toast.LENGTH_SHORT).show();
break;
//블루투스 디바이스 찾음
case BluetoothDevice.ACTION_FOUND:
Toast.makeText(MainActivity.this, "블루투스 찾음", Toast.LENGTH_SHORT).show();
//검색한 블루투스 디바이스의 객체를 구한다
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//데이터 저장
Map map = new HashMap();
map.put("name", device.getName()); //device.getName() : 블루투스 디바이스의 이름
map.put("address", device.getAddress()); //device.getAddress() : 블루투스 디바이스의 MAC 주소
dataDevice.add(map);
//리스트 목록갱신
adapterDevice.notifyDataSetChanged();
break;
//블루투스 디바이스 검색 종료
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
Toast.makeText(MainActivity.this, "블루투스 검색 종료", Toast.LENGTH_SHORT).show();
btnSearch.setEnabled(true);
break;
}
}
};
//블루투스 검색 버튼 클릭
public void mOnBluetoothSearch(View v){
//검색버튼 비활성화
btnSearch.setEnabled(false);
//mBluetoothAdapter.isDiscovering() : 블루투스 검색중인지 여부 확인
//mBluetoothAdapter.cancelDiscovery() : 블루투스 검색 취소
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
//mBluetoothAdapter.startDiscovery() : 블루투스 검색 시작
mBluetoothAdapter.startDiscovery();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case BLUETOOTH_REQUEST_CODE:
//블루투스 활성화 승인
if(resultCode == Activity.RESULT_OK){
}
//블루투스 활성화 거절
else{
Toast.makeText(this, "블루투스를 활성화해야 합니다.", Toast.LENGTH_SHORT).show();
finish();
return;
}
break;
}
}
@Override
protected void onDestroy() {
unregisterReceiver(mBluetoothStateReceiver);
unregisterReceiver(mBluetoothSearchReceiver);
super.onDestroy();
}
}