找不到所有的蓝牙设备android

时间:2014-10-02 09:42:25

标签: android android-intent bluetooth broadcastreceiver

我正在尝试连续显示蓝牙设备并在屏幕上显示它们但它从未向我显示所有设备,而是一次只显示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;

3 个答案:

答案 0 :(得分: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);
   }
  }
 }
}

使用此代码,我可以通过调用

来获取所有设备
MonitorBluetooth monitor=new MonitorBluetooth(this.getApplicationContext());
monitor.start();

来自Activity.onCreate(Bundle)

答案 1 :(得分:1)

尝试这个顺序:

public void displayDetectedDevices(){
    mBluetoothAdapter.startDiscovery();

    // Create a BroadcastReceiver for ACTION_FOUND
    mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            // When discovery finds a device
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceAddress = device.getAddress();
                String s = " ";
                unpairedDevices.add(deviceName + s + deviceAddress +" \n");
                unpairedDevicesList = new ArrayList<String>(unpairedDevices);
                Toast.makeText(getActivity(), unpairedDevicesList.toString(), Toast.LENGTH_LONG).show();
            }
        }
    };
}

请记住onCreate()声明如下:

public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    unpairedDevicesList = new ArrayList<String>();
    unpairedDevices = new HashSet<String>();
}

记住#2在onCreate()之前声明以下内容:

ArrayList<String> unpairedDevicesList;
Set<String> unpairedDevices;

最后的想法:

我同时使用了SetarrayList来放置检测到的设备。套装中不包含任何重复项。根据您要做的事情,两者都有用,因为它们提供不同的功能。另外here是我的github页面,它包含更多信息和一个完整的蓝牙应用程序。

当设置填充时,设备将开始填充屏幕,具体取决于您希望UI的工作方式。我用了一个简单的清单。

答案 2 :(得分:0)

我的蓝牙耳机问题也一样。我有两个蓝牙耳机,它们都已通电,但blueToothAdapter.startDiscovery()未检测到它们。

后来我发现,在蓝牙耳机的情况下,你需要长按开始按钮激活他们的配对模式,以便通过android蓝牙发现进行检测。

这是从developer.android.com发现附近蓝牙设备的一个很好的例子。