首页 文章

蓝牙LE外设在与蓝牙LE中央设备连接时停止广告

提问于
浏览
3

我想开发类似蓝牙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) {
        }
    };

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

它是断开连接 .

请帮帮我这个用例 . 提前致谢

1 回答

  • 5

    BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect) 放在 BluetoothGatt.STATE_CONNECTED 之前 stopAdvertising ,因为预期的Android框架行为 . 如果您需要继续保留链接并且不想再做广告,则需要调用额外的 connect()

    Code snippet of Solution

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

    Code snippet of onConnectionStateChange() implementation

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

相关问题