简体   繁体   English

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

[英]Bluetooth LE peripheral stop advertising on connect with Bluetooth LE central device

I want to develop app like Bluetooth LE peripheral device which stop advertising on connect with Bluetooth LE central device and restrict Bluetooth LE peripheral device which connects with multiple Bluetooth LE central. 我想开发类似蓝牙LE外围设备的应用程序,该设备在与蓝牙LE中央设备连接时停止广告并限制与多个蓝牙LE中心连接的蓝牙LE外围设备。

One Bluetooth LE peripheral device only connect with one Bluetooth LE central at a time. 一个蓝牙LE外围设备一次只能连接一个蓝牙LE中央设备。 Other Bluetooth LE central device could not scan after successfully connection of Bluetooth LE peripheral and Bluetooth LE central 蓝牙LE外设和蓝牙LE中心成功连接后,其他蓝牙LE中央设备无法扫描

Till now i try below code: 直到现在我尝试下面的代码:

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 am stopAdvertising on connect with BLE central device mAdvertiser.stopAdvertising(mAdvCallback); 我在与BLE中央设备mAdvertiser.stopAdvertising(mAdvCallback);连接时停止mAdvertiser.stopAdvertising(mAdvCallback);

It is disconnect connection. 它是断开连接。

Please help me in this use case. 请帮帮我这个用例。 THANKS IN ADVANCE 提前致谢

Put BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect) in BluetoothGatt.STATE_CONNECTED before stopAdvertising , because expected Android framework behavior. stopAdvertising之前将BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect)放入BluetoothGatt.STATE_CONNECTED ,因为预期的Android框架行为。 If you need to continue to hold the link and don't want to advertise anymore, you need to call into additional connect() 如果您需要继续保留链接并且不想再做广告,则需要调用其他connect()

Code snippet of Solution 解决方案的代码片段

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

Code snippet of onConnectionStateChange() implementation 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM