I wrote an android app (Peripheral) that can advertise and setup a gatt server. When I connect to it with other android device (Central), for example using nRFConnect
application or LightBlue
, the Peripheral side disconnects immediately after connection callback received.
This is my snipped code for setup gatt server:
// Create our primary service
BluetoothGattService primaryService = new BluetoothGattService(
UUIDs.UUID_SERVICE,
BluetoothGattService.SERVICE_TYPE_PRIMARY
);
// Create our write characteristic
BluetoothGattCharacteristic writeCharacteristic = new BluetoothGattCharacteristic(
UUIDs.UUID_CHARACTERISTIC,
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattCharacteristic.PERMISSION_WRITE_ENCRYPTED_MITM
);
// [START Add Client Characteristic Configuration]
BluetoothGattDescriptor descriptor1 = new BluetoothGattDescriptor(
UUIDs.UUID_CLIENT_CHARACTERISTIC_CONFIGURATION,
(BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM)
);
descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
writeCharacteristic.addDescriptor(descriptor1);
// [END Add Client Characteristic Configuration]
// [START Add Characteristic User Description]
BluetoothGattDescriptor descriptor2 = new BluetoothGattDescriptor(
UUIDs.UUID_CHARACTERISTIC_USER_DESCRIPTION,
(BluetoothGattDescriptor.PERMISSION_READ_ENCRYPTED_MITM | BluetoothGattDescriptor.PERMISSION_WRITE_ENCRYPTED_MITM)
);
try
{
descriptor2.setValue((new String("Sample Description")).getBytes("UTF-8"));
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
writeCharacteristic.addDescriptor(descriptor2);
// [END Add Characteristic User Description]
// Add the write characteristic to service
if (primaryService != null)
{
// Add the characteristic to service
primaryService.addCharacteristic(writeCharacteristic);
}
else
{
// TODO: handle this state...
}
// Add the primary service to gatt server
if (mGattServer != null)
{
// Add the service to gatt server
mGattServer.addService(primaryService);
}
else
{
// TODO: handle this state...
}
And my BluetoothGattServerCallback
:
public void onConnectionStateChange(BluetoothDevice device, int status, int newState)
{
Log.d(TAG, "Our gatt server connection state changed, new state: " + Integer.toString(newState));
super.onConnectionStateChange(device, status, newState);
switch (newState)
{
case BluetoothProfile.STATE_CONNECTED:
Log.d(TAG, "Connection State: STATE_CONNECTED");
// Stop the advertisement
//BLEManager.getInstance().stopAdvertising();
// todo later...
break;
case BluetoothProfile.STATE_DISCONNECTED:
Log.d(TAG, "Connection State: STATE_DISCONNECTED");
// Start the advertisement again
//BLEManager.getInstance().startAdvertising();
// todo later...
break;
case BluetoothProfile.STATE_CONNECTING:
Log.d(TAG, "Connection State: STATE_CONNECTING");
// Nothing
break;
case BluetoothProfile.STATE_DISCONNECTING:
Log.d(TAG, "Connection State: STATE_DISCONNECTING");
// Nothing
break;
default:
// Nothing
}
if (null != mConnectionCallback && BluetoothGatt.GATT_SUCCESS == status)
{
mConnectionCallback.onConnectionStateChange(device, newState);
}
}
And this is my log for Peripheral side:
03-04 11:37:05.115 BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=5B:95:51:3C:AC:96
03-04 11:37:05.118 GattServerCallback: Our gatt server connection state changed, new state: 2
03-04 11:37:05.118 GattServerCallback: Connection State: STATE_CONNECTED
03-04 11:37:06.975 BluetoothGattServer: onServerConnectionState() - status=0 serverIf=5 device=5B:95:51:3C:AC:96
03-04 11:37:06.975 GattServerCallback: Our gatt server connection state changed, new state: 0
03-04 11:37:06.975 GattServerCallback: Connection State: STATE_DISCONNECTED
03-04 11:37:07.476 BluetoothGattServer: close()
03-04 11:37:07.476 BluetoothGattServer: unregisterCallback() - mServerIf=5
There is not any obscure about how to advertise and setup a gatt server, but I'm confused! I'm not able to set the BLE Connection Parameters
through Android Peripheral API, or any other low level config, so everything is here!
Have anybody any idea that can help me?!