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

xen*_*ite 19 android bluetooth scanning

我需要使用谷歌android 2.1获取该地区可用的蓝牙设备列表.

事实是,我不仅需要这些设备的列表,我需要为每个设备找到一些唯一的ID,我需要一个指示器,接收信号的"好"程度(如android.wifi.ScanResult中的"级别") )... 我怎么做?

Dee*_*thi 43

查看下面的代码:

开始搜索

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);
Run Code Online (Sandbox Code Playgroud)


use*_*696 9

调用方法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);
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

结果

名称:LE-Bose Revolve + SoundLink设备硬件地址:MAC 04:52:C7:D1:B2:76

.....