gpt4 book ai didi

android - Bluetooth LE 外围设备在与 Bluetooth LE 中央设备连接时停止广告

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:55 28 4
gpt4 key购买 nike

我想开发像蓝牙 LE 外围设备这样的应用程序,它停止在与蓝牙 LE 中央设备连接时做广告,并限制与多个蓝牙 LE 中央设备连接的蓝牙 LE 外围设备。

一个蓝牙 LE 外围设备一次只能与一个蓝牙 LE 中心连接。Bluetooth LE peripheral与Bluetooth LE central成功连接后,其他Bluetooth LE central设备无法扫描

直到现在我尝试下面的代码:

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) {
}
};

我在连接 BLE 中央设备时停止广告 mAdvertiser.stopAdvertising(mAdvCallback);

是断开连接。

请帮我解决这个用例。提前致谢

最佳答案

stopAdvertising 之前将 BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect) 放在 BluetoothGatt.STATE_CONNECTED 中,因为预期的 Android 框架行为。如果需要继续持有链接,不想再打广告,需要调用额外的connect()

解决方案代码 fragment

//******************* SOLUTION **************************
BluetoothDevice mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
mGattServer.connect(mDevice, false);
//*******************************************************

onConnectionStateChange() 实现的代码 fragment

@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);
}
}

关于android - Bluetooth LE 外围设备在与 Bluetooth LE 中央设备连接时停止广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41184446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com