Quantcast
Channel: Bluetooth start discovery not giving results - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Bluetooth start discovery not giving results

$
0
0

This is my first app in Android. I am very new to Android and Java. I am kind of expert in iOS/ObjC though. I am learning by doing. So I jumped straight into making an app to connect to a bluetooth device. First step is of course to get a list of the bluetooth device available in range.

Here is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest...>

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application ...

Here is my Activity code:

private BluetoothAdapter btAdapter;

    @Override
    public void onDestroy() {
        super.onDestroy();
       // Unregister broadcast listeners
        unregisterReceiver(mReceiver);
    }


    /*-------------  ON CREATE ------------------------------*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btAdapter =  BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            System.out.println ("Bluetooth non support");
        } else {
            System.out.println ("Bluetooth initialized");
        }

        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mReceiver, filter);

        IntentFilter filterDevice = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filterDevice);

        if (btAdapter.isEnabled()) {
            String mydeviceaddress = btAdapter.getAddress();
            String mydevicename = btAdapter.getName();

            String status = mydevicename + " : " + mydeviceaddress;
            System.out.println(status);
            System.out.println ("Start discover");
            btAdapter.startDiscovery();
        } else {
            System.out.println ("Not enabled");
            Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBT, 1);
        }
    }


    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        System.out.println("1");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        System.out.println("2");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        System.out.println("3");
                        // SCAN HERE
                        btAdapter.startDiscovery();
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        System.out.println("4");
                        break;
                }
            }

            if (BluetoothDevice.ACTION_FOUND.equals(action))
            {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                System.out.println(device.getName() + "\n" + device.getAddress());
            } else {
                System.out.println("What de fuq");
            }

        }
    };

I turned on bluetooth on my android phone, and then running the app shows that log:

Bluetooth initialized Start discover

And thats all. Other logs are not printing out. Any idea why? My code seems perfect idk.

EDIT: Screenshot of the Bluetooth module HC-05 being detected by Android. enter image description here


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images