我用这个

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

}

Set devices = adapter.getBondedDevices();

for (BluetoothDevice device : devices) {

System.out.println("Found device: " + device);

}

}

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

提前致谢.

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐