저는 안드로이드 스튜디오를 사용하여 블루투스 응용 프로그램을 만들고 있는데 문제는 응용 프로그램이 장치를 한 번만 검사한다는 것입니다. 기기를 찾으면 다른 기기를 찾지 않습니다. 기기 주변에서 사용 가능한 모든 기기를 검색하고 싶습니다. 활동 코드는 다음과 같습니다블루투스에서 하나의 장치 만 감지되었습니다.
는package com.example.yubrajsharma.my_application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import java.util.ArrayList;
import static android.view.View.INVISIBLE;
public class MainActivity extends AppCompatActivity{
BluetoothAdapter mBluetoothAdapter;
int count;
Button lister;
public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
String[] mBTDevice;
ArrayAdapter<String> adapter;
private static final String TAG = "MainActivity";
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
unregisterReceiver(mReciever);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressBar pg = (ProgressBar) findViewById(R.id.progressBar);
pg.setVisibility(View.INVISIBLE);
Button searchbtn = (Button) findViewById(R.id.searchbtn);
Button lister = (Button) findViewById(R.id.lists);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter.isEnabled()){
lister.setVisibility(View.VISIBLE);
}
lister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listdata();
}
});
}
private BroadcastReceiver mReciever = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
ProgressBar pg = (ProgressBar) findViewById(R.id.progressBar);
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
}
count = mBTDevices.size();
int j = 0;
mBTDevice = new String[count];
if(count>0) {
for (BluetoothDevice device : mBTDevices) {
mBTDevice[j] = device.getName();
j++;
}
ListView pairing = (ListView) findViewById(R.id.paired);
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mBTDevice);
pairing.setAdapter(adapter);
}
else{
mBTDevice[0] = "no devices found";
ListView pairing = (ListView) findViewById(R.id.paired);
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,mBTDevice);
pairing.setAdapter(adapter);
}
mBluetoothAdapter.cancelDiscovery();
pg.setVisibility(View.INVISIBLE);
Log.d(TAG, "Disabled");
}
};
private void listdata() {
if(mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
ProgressBar pg = (ProgressBar) findViewById(R.id.progressBar);
pg.setVisibility(View.VISIBLE);
IntentFilter infill = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReciever, infill);
}
public void enableDisableBT(View view) {
Button lister = (Button) findViewById(R.id.lists);
if(!mBluetoothAdapter.isEnabled()){
Intent enableBTintent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBTintent);
IntentFilter btIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, btIntent);
}
if(mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.disable();
IntentFilter btIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, btIntent);
}
}
}
XML 파일의 코드는 다음과 같습니다
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yubrajsharma.my_application.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/lists"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List Devices"
android:visibility="invisible"
app:layout_constraintLeft_toRightOf="@+id/searchbtn"
app:layout_constraintBottom_toBottomOf="@+id/searchbtn"
android:layout_marginRight="28dp"
android:layout_marginEnd="28dp"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/paired"
android:layout_alignEnd="@+id/paired" />
<Button
android:id="@+id/searchbtn"
android:onClick="enableDisableBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON/OFF"
android:visibility="visible"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="3dp" />
<ListView
android:id="@+id/paired"
android:layout_width="368dp"
android:layout_height="422dp"
app:layout_constraintTop_toBottomOf="@+id/lists"
app:layout_constraintLeft_toLeftOf="@+id/searchbtn"
android:layout_marginBottom="13dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="29dp"
android:layout_marginRight="29dp"
android:layout_toLeftOf="@+id/lists"
android:layout_toStartOf="@+id/lists"
tools:visibility="invisible" />
</RelativeLayout>
어떻게 사용 가능한 모든 장치를 스캔? 이 호출됩니다 한 번 BroadcastReceiver.onReceive
방법에 잘