找不到所有的蓝牙设备android

JNI*_*oad 5 android bluetooth broadcastreceiver android-intent

我正在尝试连续显示蓝牙设备并在屏幕上显示它们但它从来没有向我显示所有设备,而是一次只显示1个.我找不到我做错了什么.这是我的代码,也许你可以在其中找到任何问题.谢谢

class monitorBluetooth extends monitor {
private ListView mLvDevices;
private ArrayList<String> mDeviceList = new ArrayList<String>();

public monitorBluetooth(service service) {

super(service);
    bluetooth = BluetoothAdapter.getDefaultAdapter();

this.bReceiver = new  BluetoothReceiver();
}

public void finalize() throws Throwable {

super.finalize();
}

public void run() {        

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    service.registerReceiver(this.bReceiver, filter);

if(service != null) {
        bluetooth = BluetoothAdapter.getDefaultAdapter();
        bluetooth.startDiscovery();
 }       
}
class BluetoothReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
    Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();

    String action = intent.getAction();

    if(BluetoothDevice.ACTION_FOUND.equals(action)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String uuid = intent.getStringExtra(BluetoothDevice.EXTRA_UUID);
    int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
     mDeviceList.add(device.getAddress() + ", " + device.getName()); // get mac address

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mDeviceList);
            mLvDevices.setAdapter(adapter);
    }

    }

}
}

BluetoothAdapter bluetooth;
private BluetoothReceiver bReceiver;
Run Code Online (Sandbox Code Playgroud)

小智 1

你的代码似乎有效。我将您添加的内容写信给 LogCat mDeviceList,并获得了多个设备。

也许这是你显示名称的方式,或者周围可见的 BT 设备不超过一台?

编辑:添加代码

public class MonitorBluetooth extends Thread{
 BluetoothAdapter bluetooth;
 private BluetoothReceiver bReceiver;
 private Context mContext;

 public MonitorBluetooth(Context context){
  bluetooth=BluetoothAdapter.getDefaultAdapter();
  this.bReceiver=new BluetoothReceiver();
  this.mContext=context;
 }

 public void finalize() throws Throwable{
  super.finalize();
 }

 public void run(){
  {
   IntentFilter filter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
   mContext.registerReceiver(this.bReceiver,filter);
  }
  {
   IntentFilter filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
   mContext.registerReceiver(this.bReceiver,filter);
  }
  {
   IntentFilter filter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
   mContext.registerReceiver(this.bReceiver,filter);
  }
  bluetooth=BluetoothAdapter.getDefaultAdapter();
  bluetooth.startDiscovery();
 }

 class BluetoothReceiver extends BroadcastReceiver{
  public void onReceive(Context context,Intent intent){
   String action=intent.getAction();
   if(BluetoothDevice.ACTION_FOUND.equals(action)){
    BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.d("BluetoothReceiver","found "+device.getAddress()+", "+device.getName());
   }
   else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
    Log.d("BluetoothReceiver","discovery started");
   }
   else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
    Log.d("BluetoothReceiver","discovery finished");
    context.unregisterReceiver(this);
   }
  }
 }
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,我可以通过调用获取所有设备

MonitorBluetooth monitor=new MonitorBluetooth(this.getApplicationContext());
monitor.start();
Run Code Online (Sandbox Code Playgroud)

来自 Activity.onCreate(Bundle)