如何在Android模拟器上模拟蓝牙

6

我使用了这个项目来模拟安卓模拟器上的蓝牙。
我有两个类,其中一个启用了蓝牙。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    BluetoothAdapter.SetContext(this);

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if(adapter==null) { 
        System.out.println("\nBluetooth NOT supported. Aborting.");
      return;
    }

    if (!adapter.isEnabled()) {
        adapter.enable();
    }
    }

扫描其他设备并将它们列出

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        BluetoothAdapter.SetContext(this);

        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        System.out.println("\nAdapter: " + adapter);

        if(adapter==null) { 
            System.out.println("\nBluetooth NOT supported. Aborting.");
          return;
        }

        if (!adapter.isEnabled()) {
            adapter.enable();
        }

        if (adapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            adapter.startDiscovery();
        }


        Set<BluetoothDevice> devices = adapter.getBondedDevices();
        for (BluetoothDevice device : devices) {
            System.out.println("Found device: " + device);
        }
    }

第二个设备没有检测到任何设备,那么我的代码有什么问题呢?
提前致谢。

你需要在你的代码中加入一些日志输出,以便查看正在发生什么,哪些方法正在运行等。 - Blundell
我做了那件事,发现“devices”集合的长度为零! - Aboelnour
1个回答

6

BluetoothAdapter.getDefaultAdapter() 返回默认的本地适配器。如果设备没有蓝牙功能,则返回null。由于您正在使用不支持蓝牙的模拟器,因此它将返回null。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接