2011-06-08 315 views
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); 
     } 
    } 

第二设备没有检测到任何设备,所以有什么错我的代码?
在此先感谢。

+0

您需要在代码中注明一些日志,看看发生了什么,正在运行什么方法等 – Blundell 2011-06-08 08:13:31

+0

我这样做,并意识到“设备”设置它的长度为零! – Aboelnour 2011-06-08 08:15:01

回答

3

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