0

I have an Android application that connects to a paired device. The problem is that if I don't have this device enabled before the application starts, it never works.

The only case that works is when the device is on and then I start the application. If I start the application and next the device, it never connects.

Here is the code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Check if the system supports Bluetooth Low Energy
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();
        finish();
    }

    // Take the system BLE adapter
    bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();

    // Enable Bluetooth in case it's off.
    if (bleAdapter == null || !bleAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
    }

    start_microbit();
}

private void start_microbit() {

    // Get paired devices.
    Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();

    for (BluetoothDevice device : pairedDevices) {

        // Connect to Micro:Bit (the only one paired device)
        device.connectGatt(this, true, new BluetoothGattCallback() {

            // Check if it connects or disconnects from the Micro:Bit
            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);

                switch (newState) {
                    case BluetoothProfile.STATE_CONNECTED:
                        runOnUiThread(new Runnable() {
                            public void run() {
                                ((TextView) findViewById(R.id.state)).setText("Connected to Micro:Bit");
                                ((TextView) findViewById(R.id.state)).setTextColor(Color.GREEN);
                            }
                        });

                        gatt.discoverServices();
                        break;

                    case BluetoothProfile.STATE_DISCONNECTED:
                        runOnUiThread(new Runnable() {
                            public void run() {
                                ((TextView) findViewById(R.id.state)).setText("Not connected");
                                ((TextView) findViewById(R.id.state)).setTextColor(Color.RED);
                            }
                        });

                        gatt.disconnect();
                        break;
                }
            }
        });
    }
}
Lechucico
  • 1,914
  • 7
  • 27
  • 60
  • Your code should work. As soon as you connect with autoConnect = true, Android will try connect forever (until you abort). – Emil Feb 22 '18 at 20:22
  • Never have an experience to connect bonded devices using BLE, but from the code I think you should check the return value of bleAdapter.getBondedDevices(); when your ble device is off. – reTs Feb 23 '18 at 02:55
  • @reTs But this will always return 1 device, because this just gives the bonded devices with my phone, not the connected ones. The place where I should check this is device.connectGatt() but I don't know how to check if it connects or not. – Lechucico Feb 23 '18 at 14:47
  • Is the BluetoothDevice returned contains correct information? Like Emil have said the connectGatt call is the correct so I suspected that the BluetoothDevice object contains corrupted information. – reTs Feb 26 '18 at 05:06

0 Answers0