Как найти устройства в радиусе действия с помощью Bluetooth? - PullRequest
6 голосов
/ 24 июня 2011

Я новичок в android. Я хочу разработать приложение для поиска устройств в диапазоне, используя Bluetooth программно. Если у кого-то есть идея, пожалуйста, дайте мне пример кода.

Ответы [ 3 ]

8 голосов
/ 02 мая 2012

Find The Devices in the Range by using Bluetooth programmatically.

Да, вы можете сделать это, используя BroadcastReceiver , ознакомьтесь с кодом ниже, это поможет вам.

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

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

Находит устройство

    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);
1 голос
/ 24 июня 2011

вы можете использовать метод startDiscovery ().

У меня нет примера кода, но вы можете взглянуть на: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery%28%29

Надеюсь, это поможет!

1 голос
/ 24 июня 2011

Создайте приемник Broad Cast примерно следующим образом и добавьте информацию об устройстве

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
ArrayList<HashMap<String, String> arl = new ArrayList<HashMap<String, String>();    
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // If it's already paired, skip it, because it's been listed already
                HashMap<String, String> deviceMap = new HashMap<String, String>();
                deviceMap.put(device.getName(), device.getAddress());
                arl.add(deviceMap);

            // When discovery is finished, change the Activity title
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                setProgressBarIndeterminateVisibility(false);
                setTitle(R.string.select_device);
                if (mNewDevicesArrayAdapter.getCount() == 0) {
                    String noDevices = getResources().getText(R.string.none_found).toString();
                    mNewDevicesArrayAdapter.add(noDevices);
                }
            }
        }


    };
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...