0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Processing] Bluetooth で接続されているデバイス名とポート名を対応付ける (Windows)

Posted at

要約

  • レジストリ HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\BTHENUM にデバイスの情報が含まれている
    • コマンド REG QUERY ~ でレジストリを読み込める
      • /s オプションでツリー以下をすべて表示
      • /v オプションで特定の値を抽出
        • FriendlyName にデバイス名が含まれているキーがある
        • PortName にポート名が含まれているキーがある
    • Runtime.getRuntime().exec() でコマンド実行できる
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\BTHENUM" /s /v FriendlyName

を実行すると

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\BTHENUM\Dev_XXXXXXXXXXXX\x&xxxxxxx&x&BluetoothDevice_XXXXXXXXXXXX
    FriendlyName    REG_SZ    【デバイス名】

のようなリストが得られる。

REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\BTHENUM" /s /v PortName

を実行すると

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\BTHENUM\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}_LOCALMFG&XXXX\x&xxxxxxxx&x&XXXXXXXXXXXX_XXXXXXXXX\Device Parameters
    PortName    REG_SZ    【ポート名】

のようなリストが得られる。

これらの XXXXXXXXXXXX の部分が MAC アドレスになっていて一致しているので、結果を混ぜ合わせると対応が得られる。

コード

import java.io.InputStreamReader;
import java.util.Map;

class BluetoothDevice {
  String address;
  String name;
  String port;
  
  BluetoothDevice(String address, String name, String port) {
    this.address = address;
    this.name = name;
    this.port = port;
  }
  
  String toString() {
    return "BluetoothDevice(address: " + address + ", name: " + name + ", port: " + port + ")";
  }
}

ArrayList<BluetoothDevice> discoverBluetoothDevicesForWindows() {
  ArrayList<BluetoothDevice> devices = new ArrayList();
  try {
    Runtime r = Runtime.getRuntime();
    Process p;
    BufferedReader br;
    HashMap<String, String> hm = new HashMap();
    p = r.exec("REG QUERY HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\BTHENUM /s /v FriendlyName");
    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while (true) {
      String s = br.readLine();
      if (s == null) break;
      String key = "BluetoothDevice_";
      int idx = s.indexOf(key);
      if (idx != -1) {
        String address = s.substring(idx + key.length());
        s = br.readLine();
        String name = s.substring("    FriendlyName    REG_SZ    ".length());
        hm.put(address, name);
      }
    }
    p = r.exec("REG QUERY HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\BTHENUM /s /v PortName");
    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while (true) {
      String s = br.readLine();
      if (s == null) break;
      for (Map.Entry<String, String> entry : hm.entrySet()) {
        String address = entry.getKey();
        String name = entry.getValue();
        if (s.contains(address)) {
          s = br.readLine();
          String port = s.substring("    PortName    REG_SZ    ".length());
          devices.add(new BluetoothDevice(address, name, port));
        }
      }
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  return devices;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?