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

Abo*_*our 5 android bluetooth

我用这个项目来模拟android模拟器上的蓝牙.
我有2个类,一个启用蓝牙

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();
    }
    }
Run Code Online (Sandbox Code Playgroud)

另一次扫描设备并列出它们

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

第二个设备没有检测到任何设备,所以我的代码出了什么问题?
提前致谢.

Nor*_*csi 5

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