Как сканировать доступные устройства Bluetooth в диапазоне в Android? - PullRequest
17 голосов
/ 03 июля 2010

Мне нужно получить список доступных устройств Bluetooth в этом районе с помощью Google Android Android 2.1.

Дело в том, что мне нужен не просто список этих устройств, мне нужен какой-то уникальный идентификатор для каждого найденного устройства и мне нужен индикатор того, насколько «хороший» сигнал принимается (например, «уровень» в Android. wifi.ScanResult) ... Как мне это сделать?

Ответы [ 2 ]

41 голосов
/ 28 мая 2012

Проверьте код ниже:

Начало поиска

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);
8 голосов
/ 24 января 2018

Метод вызова 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-Bose Revolve + SoundLink deviceHardwareAddress: MAC .....

...