Bluetooth LE Wireless Peripheral Advertising connects to a central Bluetooth LE device
I want to develop an application such as a Bluetooth LE peripheral that stops ads when connected to a central Bluetooth LE and restricts a Bluetooth LE peripheral that connects to multiple central Bluetooth LEs.
One Bluetooth LE peripheral only connects to one Bluetooth LE at a time. Another Bluetooth LE host failed to scan after successfully pairing the Bluetooth LE peripheral and the Bluetooth LE host
So far I am trying to make the code below:
private final BluetoothGattServerCallback mGattServerCallback = new BluetoothGattServerCallback() {
@Override
public void onServiceAdded(int status, BluetoothGattService service) {
super.onServiceAdded(status, service);
}
@Override
public void onConnectionStateChange(BluetoothDevice device, final int status, int newState) {
super.onConnectionStateChange(device, status, newState);
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
mBluetoothDevices.add(device);
// Bluetooth LE peripheral stop advertising on connect with Bluetooth LE central device
mAdvertiser.stopAdvertising(mAdvCallback);
Log.v(TAG, "Connected to device: " + device.getAddress());
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
mBluetoothDevices.remove(device);
Log.v(TAG, "Disconnected from device");
}
} else {
mBluetoothDevices.remove(device);
// There are too many gatt errors (some of them not even in the documentation) so we just
// show the error to the user.
final String errorMessage = getString(R.string.status_errorWhenConnecting) + ": " + status;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}
});
Log.e(TAG, "Error when connecting: " + status);
}
}
@Override
public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,
BluetoothGattCharacteristic characteristic) {
}
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
Log.v(TAG, "Notification sent. Status: " + status);
}
@Override
public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId,
BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
}
@Override
public void onDescriptorWriteRequest(BluetoothDevice device, int requestId,
BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded,
int offset,
byte[] value) {
}
};
I stop ads when connected with central BLE device mAdvertiser.stopAdvertising(mAdvCallback);
Disconnects the connection.
Please help me with this use case. THANKS FOR THE EXPANSION
source to share
Put BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect)
in BluetoothGatt.STATE_CONNECTED
before stopAdvertising
because the expected behavior of the android. If you need to continue to hold the link and do not want to advertise anymore, you need to call additionalconnect()
Solution code snippet
//******************* SOLUTION **************************
BluetoothDevice mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
mGattServer.connect(mDevice, false);
//*******************************************************
A snippet of onConnectionStateChange () implementation code
@Override
public void onConnectionStateChange(BluetoothDevice device, final int status, int newState) {
super.onConnectionStateChange(device, status, newState);
if (status == BluetoothGatt.GATT_SUCCESS) {
if (newState == BluetoothGatt.STATE_CONNECTED) {
mBluetoothDevices.add(device);
//******************* SOLUTION **************************
BluetoothDevice mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
mGattServer.connect(mDevice, false);
//*******************************************************
// Bluetooth LE peripheral stop advertising on connect with Bluetooth LE central device
mAdvertiser.stopAdvertising(mAdvCallback);
Log.v(TAG, "Connected to device: " + device.getAddress());
} else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
mBluetoothDevices.remove(device);
Log.v(TAG, "Disconnected from device");
}
} else {
mBluetoothDevices.remove(device);
// There are too many gatt errors (some of them not even in the documentation) so we just
// show the error to the user.
final String errorMessage = getString(R.string.status_errorWhenConnecting) + ": " + status;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show();
}
});
Log.e(TAG, "Error when connecting: " + status);
}
}
source to share