微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我用这个project来模拟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();
    }
    }

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

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();
        }


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

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

最佳答案
BluetoothAdapter.getDefaultAdapter()返回认的本地适配器.
如果设备没有蓝牙功能,它将返回null,并且由于您使用的是不支持蓝牙的仿真器,它将返回null.

原文地址:https://www.jb51.cc/android/430426.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐