2

有人能解释一下为什么这段代码总是只给我一个蓝牙设备,即使我正在使用两个星系,一个紧挨着另一个?此代码在三星 Galaxy Tab 上运行,我正在使用正确的蓝牙激活测试三星 Galaxy Gio。如果我检查默认研究它可以工作..但不能使用此代码:

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("\nFound device: " + device); }

...您正在查找已配对的设备。如果你只得到一个,原因很简单,你只有一对。考虑到这将返回所有配对的设备,无论它们是否处于活动状态。

另一方面,您开始发现...

适配器.startDiscovery();

... However you have not registered a broadcast receiver to process the *BluetoothDevice.ACTION_FOUND* intents that you will receive with each discoverable Bluetooth device seen. Discoverable is key here because by default Android devices are not discoverable and they only allow a maximum time of typical 120 seconds.

于 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

do you say out.append("\nAdapter: " + adapter);

but if do you are work in eclipse with xml OR INTELLIJ

TextView txt;
String text;

....

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

Do you see the error?

于 2012-08-21T08:09:11.907 回答