I'm trying to programmatically pair with Ble iBeacon using BluetoothGatt API in android.I can able to pair with Ble iBeacon up to lollipop.But i cannot pair in Marshmallow(My testing device is oneplus 3).I'm using altbeacon library for beacon scanning.
Also, i gave ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION permission in the manifest file and turned on GPS location.
private BluetoothGatt mGatt;
BluetoothAdapter baBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
baBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(myReceiver, intentFilter);
}
Here i'm sending beacon mac id
public void PairToDevice(String sMacId) {
BluetoothDevice device = baBluetoothAdapter.getRemoteDevice(sMacId);
if (mGatt == null) {
mGatt = device.connectGatt(this, false, gattCallback);
}
}
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Log.i("onConnectionStateChange", "Status: " + status);
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");
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
}
}
Here i'm checking bonding state.Bonding state is always none in Marshmallow.But lessthan marshmallow it is working fine.
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int nState = device.getBondState();
if (nState == BluetoothDevice.BOND_BONDED) {
Log.e("Bond Information", "device bonded");
} else if (nState == BluetoothDevice.BOND_BONDING) {
Log.e("Bond Information", "device bonding");
} else if (nState == BluetoothDevice.BOND_NONE) {
Log.e("Bond Information", "bond none");
}
}
}
};