How to find the devices in the range by using bluetooth?

后端 未结 3 1692
广开言路
广开言路 2021-02-03 14:17

I am new to android.I want to develop an application to find the devices in the range by using Bluetooth programmatically.If any one has idea please give some sample code to me.

3条回答
  •  盖世英雄少女心
    2021-02-03 15:02

    Find The Devices in the Range by using Bluetooth programmatically.

    Yes you can do this using BroadcastReceiver, check out below code, it will help you.

    Starting search

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

    Finds a device

        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);
    

提交回复
热议问题