如何在Java Android中以编程方式连接到特定的蓝牙名称?

如何在Java Android中以编程方式连接到特定的蓝牙名称?,java,android,bluetooth,Java,Android,Bluetooth,我试着用谷歌和百度搜索这个话题,但是我找不到。(可能我没有看到,如果有,请引导我去搜索。)。我的目的是让我的android应用程序只连接到特定的蓝牙名称。例如,我只想将其连接到“ABC” 签出[此][1]链接 [1] :https://developer.android.com/guide/topics/connectivity/bluetooth#FindingDevices查找设备时启动蓝牙搜索-检查设备名称。如果它符合你想要的-connect.听起来不错,让我试试,我会更新你的。在andr

我试着用谷歌和百度搜索这个话题,但是我找不到。(可能我没有看到,如果有,请引导我去搜索。)。我的目的是让我的android应用程序只连接到特定的蓝牙名称。例如,我只想将其连接到“ABC”

签出[此][1]链接


[1] :https://developer.android.com/guide/topics/connectivity/bluetooth#FindingDevices

查找设备时启动蓝牙搜索-检查设备名称。如果它符合你想要的-connect.听起来不错,让我试试,我会更新你的。在android开发者页面上有一个如何使用蓝牙API的示例,阅读后我相信你将能够实现你想要的。
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();
      // 
      // use try-catch to keep tying on the logic -- John Melody added
      try {
        if (deviceName.contains("somethingsomething")) {
          System.out.println(String.format("Ok %s", deviceName));
        }
      } catch(Exception e) {
        e.printStackTrace();
      }
    //
    }
  }
};