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

标签 android bluetooth

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

最佳答案

以编程方式使用蓝牙查找范围内的设备。

是的,您可以使用 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);

关于android - 如何使用蓝牙找到范围内的设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6464015/

相关文章:

android - 当我选择单选按钮时,它应该同时提交答案并打开下一个 Activity 。我怎样才能做到这一点?

c# - 从 Xamarin.Android 中的单击事件处理程序访问 CheckBox 属性

bluetooth - 配置并运行蓝牙后运行脚本 - Raspberry Pi 3

android - 如何使配套应用程序将一个 android 设备连接到另一个 android 设备...?

android - 蓝牙连接超时: dm_pm_timer expires

android - 对齐 View 以模拟数学积分表达式

java - 如何使用 public <T extends View> T findViewById(int id);在Android Activity中编译sdk 26?

android - 首选项 : can I find the input type when the preference item is clicked

ios - 在iOS上还原Microsoft Band连接

java - 找不到包 android.bluetooth