如何在android中扫描可用的蓝牙设备?

问题描述:

我需要获得该地区可用的蓝牙设备列表使用谷歌Android 2.1。如何在android中扫描可用的蓝牙设备?

事情是,我不只需要这些设备的列表,我需要为每个设备找到一个唯一的ID,我需要一个指标,如何“好”的信号接收(如“水平”在Android .wifi.ScanResult)...我该怎么做?

Here有一个发现设备的完整示例。

您可以使用MAC地址作为唯一ID。

关于信号强度,我认为你应该使用RSSI(接收信号强度指标)。下面

+0

有人对此RSSI有经验吗?我在这里有点不安全,因为这被定义为一个常量? – xenonite 2010-07-03 10:35:53

+1

你的第一个链接 - http://developer.android.com/guide/topics/wireless/bluetooth.html#FindingDevices已经死了 – Dayan 2016-05-24 21:01:22

+0

链接已经打破了 – Charlie 2016-07-24 21:14:14

退房代码:

开始搜索

mBluetoothAdapter.startDiscovery(); 
mReceiver = new BroadcastReceiver() { 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    //Finding devices     
    if (BluetoothDevice.ACTION_FOUND.equals(action)) 
    { 
     // Get the BluetoothDevice object from the Intent 
     BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
     // Add the name and address to an array adapter to show in a ListView 
     mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
    } 
    } 
}; 

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
registerReceiver(mReceiver, filter); 
+3

对于简单易懂的代码:) – Lucifer 2012-08-22 03:08:23

+0

干净的代码,应该是正确的答案。 – Dayan 2016-05-24 21:02:16

+0

不适合我。 – 2017-03-29 20:55:15

bluetoothScanning,需要调用方法方面

void bluetoothScanning(){ 

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
    context.registerReceiver(mReceiver, filter); 
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    mBluetoothAdapter.startDiscovery(); 

} 


// Create a BroadcastReceiver for ACTION_FOUND. 
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // Discovery has found a device. Get the BluetoothDevice 
      // object and its info from the Intent. 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      String deviceName = device.getName(); 
      String deviceHardwareAddress = device.getAddress(); // MAC address 

      Log.i("Device Name: " , "device " + deviceName); 
      Log.i("deviceHardwareAddress " , "hard" + deviceHardwareAddress); 
     } 
    } 
}; 

结果

名称:LE-百色旋转+的SoundLink deviceHardwareAddress :MAC 04:52:C7:D1:B2:76

.....