三星Galaxy上的Android-Java蓝牙

Lor*_*uto 2 java android bluetooth galaxy

有人可以解释一下为什么这个代码总是只给我一个蓝牙设备,即使我正在使用另一个旁边的两个星系吗?此代码在三星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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Fer*_*lez 5

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

一方面称之为......

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

...你正在查找已经配对的设备.如果你只得到一个原因很简单,你只有一个配对.考虑到这将返回所有配对设备,无论它们是否有效.

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

adapter.startDiscovery();

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