Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to simulate bluetooth on android emulator

I used this project to simulate the bluetooth on android emulator.
I have 2 classes one enabling the bluetooth

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

another scan for devices and list them

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

the second device doesn't detect any devices, so what's wrong with my code?
thanks in advance.

like image 218
Aboelnour Avatar asked Jun 08 '11 08:06

Aboelnour


1 Answers

BluetoothAdapter.getDefaultAdapter() returns the default local adapter. It returns null if the device doesn't have Bluetooth capability and since you are using the emulator which doesn't support Bluetooth it will return null.

like image 197
Norbert Bicsi Avatar answered Oct 22 '22 02:10

Norbert Bicsi