Ble is advertising AdvertiseData added in BluetoothGattServer and advertise is done and device get connected. after connect discoverServices() is also called but services are not discovered i.e. onServiceDiscovered() callback not triggered.
//BluetoothGatt client work fine with Google sample Adevertiser that i have tested but server side i have added BluetoothGattServer code below listed
//ADVERTISER CODE /** * Starts BLE Advertising. */
private void startAdvertising() {
Log.d(TAG, "Service: Starting Advertising");
if (mAdvertiseCallback == null) {
AdvertiseSettings settings = buildAdvertiseSettings();
AdvertiseData data = buildAdvertiseData();
mAdvertiseCallback = new SampleAdvertiseCallback();
if (mBluetoothLeAdvertiser != null) {
mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
}
}
}
//GATT SERVER creating gatt server instance attaching services and characteristics that are going to expose
private void initServer() {
BluetoothGattService service =
new BluetoothGattService(Constants.Service_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic characteristic =
new BluetoothGattCharacteristic(Constants.Char_UUID,
//Read + write characteristics and permission
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
service.addCharacteristic(characteristic);
mBluetoothGattServer.addService(service);
}
// Callbacks to handle all Gatt client connection to handle read + write request
private BluetoothGattServerCallback mBGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
super.onConnectionStateChange(device, status, newState);
Log.d(TAG ,"OnConnection state change");
if(newState == BluetoothProfile.STATE_CONNECTED){
}else if(newState == BluetoothProfile.STATE_DISCONNECTED){
}
}
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
super.onServiceAdded(status, service);
Log.i(TAG, " Service are added");
}
//Advertise data
private AdvertiseData buildAdvertiseData() {
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(new ParcelUuid(Constants.Service_UUID))
.build();
}
//Advertise setting
private AdvertiseSettings buildAdvertiseSettings() {
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
.setConnectable(true)
.setTimeout(0)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
.build();
return settings;
}
//Get references to system Bluetooth objects if we don't have them already.
private void initialize() {
if (mBluetoothLeAdvertiser == null) {
BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
if (mBluetoothManager != null) {
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter != null) {
mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
//register the instance of server to BLE framework
mBluetoothGattServer = mBluetoothManager.openGattServer(this,mBGattServerCallback);
initServer();
} else {
Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, getString(R.string.bt_null), Toast.LENGTH_LONG).show();
}
}
}
//Log summery
` // the service uuid and characteristics uuid are coming into advertisment data { onGetService() - Device=65:5E:34:E1:47:55 UUID=0000xxxx-0000-1000-8000-00805xxxx 10-31 17:55:11.542 23276-25749/ D/BluetoothGatt: onGetService() - Device=65:5E:34:E1:47:55 UUID=0000xxxx-0000- 1000-8000-0080xxxxx
onGetCharacteristic() - Device=65:5E:34:E1:47:55 UUID=0000xxxxx-0000-1000-8000-00805fxxxxx srvcType=0 srvcInstId=0 charInstId=0 charProps=32 10-31 17:55:11.627 23276-23290/ D/BluetoothGatt: onGetCharacteristic() - Device=65:5E:34:E1:47:55 UUID=0000xxx-0000-1000-8000-00805fxxxx srvcType=0 srvcInstId=0 charInstId=1 charProps=10 10-31 17:55:11.718 23276-23289/ D/BluetoothGatt: onGetCharacteristic() - Device=65:5E:34:E1:47:55 UUID=0000xxxx-0000-1000-8000-00805fxxxxx srvcType=0 srvcInstId=0 charInstId=0 charProps=2 } `