三星 Galaxy 上的 Android-Java 蓝牙

发布于 2024-12-03 03:32:04 字数 1080 浏览 1 评论 0原文

有人可以解释一下为什么这段代码总是只给我返回一个蓝牙设备,即使我正在使用两个星系,一个紧挨着另一个?该代码在三星 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);
    }
}

Can someone explain me why this code give me back always only one bluetooth device, even if i'm working with two galaxy one next to the other one? This code is running on a Samsung Galaxy Tab and i'm using for tests a Samsung Galaxy Gio both with the right Bluetooth activation. If i check on the default research it works ..but not with this code:

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无言温柔 2024-12-10 03:32:04

我认为你误解了你在做什么。

一方面通过调用这个...

设置设备=adapter.getBondedDevices();
对于(蓝牙设备设备:设备){
out.append("\n找到设备:" + device);
}

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

另一方面,你正在开始一个发现......

适配器.startDiscovery();

...但是,您尚未注册广播接收器来处理您将在看到的每个可发现蓝牙设备中收到的 *BluetoothDevice.ACTION_FOUND* 意图。 可发现是这里的关键,因为默认情况下,Android 设备是不可发现的,并且它们只允许典型的 120 秒的最长时间。

I think you are misunderstanding what you are doing.

On one hand by calling this ...

Set devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices) {
out.append("\nFound device: " + device);
}

... you are looking up the already paired devices. If you only get one the reason is simple, you have only one paired. Take into account that this will return all the paired devices, no matter if they are live or not.

On the other hand you are starting a discovery with ...

adapter.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.

情话已封尘 2024-12-10 03:32:04

查看API

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

startDiscovery 是异步的 - 它将扫描大约 12 秒,然后获取扫描中找到的任何地址的设备名称。您无需等待发现完成,因此在您检查结果时并未发现范围内的所有设备也就不足为奇了。

Look at the API

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

startDiscovery is asynchronous - it will scan for about 12 seconds and then get the device names of any addresses found in the scan. You don't wait for discovery to finish so it's not surprising that not all devices in range are discovered by the time you check the results.

未央 2024-12-10 03:32:04

你说 out.append("\nAdapter: " + adapter);

但如果你在 Eclipse 中使用 xml 或 INTELLIJ 工作,

TextView txt;
String text;

....

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

你看到错误了吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文