4
我用這個project來模擬android模擬器上的藍牙。
我有2個班一個啓用藍牙如何模擬Android模擬器上的藍牙
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 2011-06-08 08:13:31
我這樣做,並意識到「設備」設置它的長度爲零! – Aboelnour 2011-06-08 08:15:01