2

2つの銀河を隣り合わせで使用している場合でも、このコードが常に1つのBluetoothデバイスのみを返す理由を誰かが説明できますか?このコードはSamsungGalaxyTabで実行されており、SamsungGalaxyGioの両方を適切なBluetoothアクティベーションでテストするために使用しています。デフォルトの調査を確認すると、機能しますが、このコードでは機能しません。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    out = new TextView(this);
    setContentView(out);
    // Getting the Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    out.append("\nAdapter: " + adapter);

    // Check for Bluetooth support in the first place
    // Emulator doesn't support Bluetooth and will return null
    if (adapter == null) {
        out.append("\nBluetooth NOT supported. Aborting.");
        return;
    }

    // Starting the device discovery
    out.append("\nStarting discovery...");
    adapter.startDiscovery();
    out.append("\nDone with discovery...");

    // Listing paired devices out.append("\nDevices Paired:");
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    for (BluetoothDevice device : devices) {
        out.append("\nFound device: " + device);
    }
}
4

3 に答える 3

5

あなたは自分のしていることを誤解していると思います。

一方でこれを呼び出すことで...

デバイスを設定 = adapter.getBondedDevices(); for (BluetoothDevice device : devices) { out.append("\n見つかったデバイス: " + device); }

... すでにペアリングされているデバイスを検索しています。1 つしか取得できない場合、その理由は単純です。ペアになっているのは 1 つだけです。これにより、ライブかどうかに関係なく、ペアリングされたすべてのデバイスが返されることに注意してください。

一方、あなたは発見を始めています...

adapter.startDiscovery();

... ただし、検出可能な Bluetooth デバイスごとに受信する *BluetoothDevice.ACTION_FOUND* インテントを処理するためのブロードキャスト レシーバーを登録していません。ここでは、検出可能であることが重要です。デフォルトでは、Android デバイスは検出可能ではなく、通常 120 秒の最大時間しか許可されないためです。

于 2011-09-06T07:22:31.473 に答える
2

APIを見てください

http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery ()

startDiscovery は非同期です。約 12 秒間スキャンし、スキャンで見つかったアドレスのデバイス名を取得します。検出が完了するのを待たないので、結果を確認するまでに範囲内のすべてのデバイスが検出されていなくても驚くことではありません。

于 2011-09-06T07:22:13.673 に答える
-1

あなたは言いますか out.append("\nAdapter: " + adapter);

しかし、xml OR INTELLIJを使用してEclipseで作業している場合

TextView txt;
String text;

....

text += ("Adapter:" +  adapter);
txt.setText(text);

エラーが表示されますか?

于 2012-08-21T08:09:11.907 に答える