有人可以解释一下为什么这个代码总是只给我一个蓝牙设备,即使我正在使用另一个旁边的两个星系吗?此代码在三星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);
}
}
答案 0 :(得分:5)
我认为你误解了你在做什么。
一方面称之为......
Set devices = adapter.getBondedDevices(); for(BluetoothDevice device:devices){ out.append(“\ nFound device:”+ device); }
...您正在查找已配对的设备。如果你只得到一个原因很简单,你只有一个配对。考虑到这将返回所有配对设备,无论它们是否有效。
另一方面,你开始发现......
adapter.startDiscovery();
...但是,您尚未注册广播接收器来处理您将在每个可发现的蓝牙设备上看到的* BluetoothDevice.ACTION_FOUND *意图。 可发现是关键所在,因为默认情况下Android设备不是可发现的,它们只允许最长120秒的时间。
答案 1 :(得分:2)
查看API
http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()
startDiscovery是异步的 - 它将扫描大约12秒,然后获取扫描中找到的任何地址的设备名称。您不必等待发现完成,因此当您检查结果时,并不是所有设备都被发现也就不足为奇了。
答案 2 :(得分:-1)
out.append("\nAdapter: " + adapter);
吗?
但是你是否在使用xml或INTELLIJ进行eclipse工作
TextView txt;
String text;
....
text += ("Adapter:" + adapter);
txt.setText(text);
你看到错误吗?