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

标签 android bluetooth bluetooth-lowenergy

我想开发像蓝牙 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/

相关文章:

ios - 如何解码 BLE 广告数据

ios - SWIFT - BLE 通信

android - android 中应用程序计费 v3 中的恢复功能的用途或含义是什么

Android Wifi 信号强度

android - android中的NoClassFoundError MultipartEntity错误

iphone - 蓝牙 4.0 与较旧的蓝牙

android - 查找附近的所有蓝牙设备(耳机,电话等),而不必使设备处于可发现模式

c# - 处理来自 C# 的 Java 插件异常?

Android 在 BT 连接上强行杀死我的启动器

android - 我如何知道 BLE 扫描是否正在进行?