0

For my application, I'm trying to programmatically pair with Ble Beacon using BluetoothGatt API.I can able to pair with Ble beacon up to lollipop.But i cannot pair in Marshmallow(My testing device is oneplus 3).

Also, i gave ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION permission in the manifest file and turned on GPS location.

BluetoothGatt mGatt; 
baBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public void connectToDevice(String sMacId) {        

    BluetoothDevice device = baBluetoothAdapter.getRemoteDevice(sMacId);       

    if (mGatt == null) {
        mGatt = device.connectGatt(this, false, gattCallback);
    }
}

This is my BluetoothGatt callback method.After calling discoverServices(),it is directly going to BluetoothProfile.STATE_DISCONNECTED case.

private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                Log.e("gattCallback", "STATE_CONNECTED");
                gatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                Log.e("gattCallback", "STATE_DISCONNECTED");
                break;
            default:
                Log.e("gattCallback", "STATE_OTHER");
        }
    }
}

I also tried in native bluetooth.But it showing error.My beacon pairing password is 123456.This password is working less than marshmallow.Please help me! Please see this image link

Siddharthan
  • 61
  • 2
  • 9
  • Where does it show "Pin or Passkey wrong"? In LogCat or in a screen dialog? Can you please paste the exact message if from LogCat. If it shows up on the screen an image would be helpful. – davidgyoung Dec 20 '16 at 14:31
  • @davidgyoung Please see the above image link – Siddharthan Dec 26 '16 at 06:33
  • The dialog shown looks like a bluetooth classic pairing dialog initiated by a user by tapping on the device name in settings. Do not confuse Bluetooth LE connections with Bluetooth classic pairing. They are two very different things. Pairing should not be needed to establish a connection with GATT APIs. – davidgyoung Dec 26 '16 at 12:12
  • @davidgyoung thanks for the replay sir.Beacon pairing is not happening in GATT APIs also.I'm not getting any error message in my application while pairing beacon and Less than marshmallow beacon is pairing is working in native blue tooth. – Siddharthan Dec 29 '16 at 10:45
  • @davidgyoung i tested with nRF Connect app in marshmallow.It's not working sir.Please see this link sir https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en – Siddharthan Dec 29 '16 at 10:49

1 Answers1

0

If someone is still looking for a solution for this, see this answer on stackoverflow.com adding gatt.connect() after mDevice.connectGatt(mContext, false, mBluetoothGattCallback); did the trick.

Milad Rashidi
  • 1,296
  • 4
  • 22
  • 40
sleepystar96
  • 721
  • 3
  • 12