我用這個
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);
}
}
第二個裝置沒有檢測到任何裝置,是以我的代碼出了什麼問題?
提前緻謝.