三星 Galaxy 上的 Android-Java 蓝牙
有人可以解释一下为什么这段代码总是只给我返回一个蓝牙设备,即使我正在使用两个星系,一个紧挨着另一个?该代码在三星 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你误解了你在做什么。
一方面通过调用这个...
您正在查找已经配对的设备。如果你只得到一个,原因很简单,你只有一对。请注意,这将返回所有配对的设备,无论它们是否处于活动状态。
另一方面,你正在开始一个发现......
...但是,您尚未注册广播接收器来处理您将在看到的每个可发现蓝牙设备中收到的 *BluetoothDevice.ACTION_FOUND* 意图。 可发现是这里的关键,因为默认情况下,Android 设备是不可发现的,并且它们只允许典型的 120 秒的最长时间。
I think you are misunderstanding what you are doing.
On one hand by calling this ...
... 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 ...
... 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.
查看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.
你说
out.append("\nAdapter: " + adapter);
但如果你在 Eclipse 中使用 xml 或 INTELLIJ 工作,
你看到错误了吗?
do you say
out.append("\nAdapter: " + adapter);
but if do you are work in eclipse with xml OR INTELLIJ
Do you see the error?