如何使用蓝牙找到该范围内的设备?

kir*_*ran 6 android bluetooth

我是android的新手.我想开发一个应用程序,通过编程方式使用蓝牙找到该范围内的设备.如果有任何想法,请给我一些示例代码.

Dee*_*thi 8

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

找到一个设备

    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)