蓝牙LE外围设备停止广告与蓝牙LE中央设备连接
问题描述:
我想开发应用程序,如蓝牙LE外围设备停止广告与蓝牙LE中央设备连接并限制连接多个蓝牙LE中心的蓝牙LE外围设备。蓝牙LE外围设备停止广告与蓝牙LE中央设备连接
一个蓝牙LE外围设备一次只能连接一个蓝牙LE中央。成功连接蓝牙LE外围设备和蓝牙LE的中央
至此之后 其他蓝牙LE核心设备无法扫描我尝试下面的代码:
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) {
}
};
我对stopAdvertising连接与BLE中心设备mAdvertiser.stopAdvertising(mAdvCallback);
这是断开连接。
请帮我在这个用例。 感谢提前
答
将BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect)
置于BluetoothGatt.STATE_CONNECTED
之前stopAdvertising
,因为预期的Android框架行为。如果需要继续持有该链接,不想再投放广告,您需要调用解决方案的额外connect()
代码片段
//******************* SOLUTION **************************
BluetoothDevice mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
mGattServer.connect(mDevice, false);
//*******************************************************
onConnectionStateChange的代码段()实现
@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);
}
}
在我的实验中,似乎你不需要getRemoteDevice。它的工作原理也是这样的:'if(status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED && device!= null)mGattServer.connect(device,false); mAdvertiser.stopAdvertising(mAdvCallback);'如您所见,无论连接是否成功,我都会停止投放广告,因为在连接发生任何变化时继续投放广告似乎并不好。 – JustAMartin
这不适合我。但是我稍后停止了广告,而不是onConnectionStateChange。即使我调用mGattServer.connect(mDevice,false),设备也会断开;在停止广告之前。 –
请把mGattServer.connect(mDevice,false);在onConnectionStateChange – Palak