本文介绍了蓝牙LE外围设备停止与蓝牙LE中央设备连接的广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发诸如Bluetooth LE外围设备之类的应用程序,该应用程序在与Bluetooth LE中央设备连接时停止广告,并限制与多个Bluetooth LE中央设备连接的Bluetooth LE外围设备.

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.

一个Bluetooth LE外围设备一次只能连接一个Bluetooth LE中央设备.成功连接Bluetooth LE外围设备和Bluetooth LE Central之后,其他Bluetooth LE Central设备无法扫描

One Bluetooth LE peripheral device only connect with one Bluetooth LE central at a time.Other Bluetooth LE central device could not scan after successfully connection of Bluetooth LE peripheral and Bluetooth LE central

现在我尝试下面的代码:

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

我正在停止与BLE中央设备mAdvertiser.stopAdvertising(mAdvCallback);

I am stopAdvertising on connect with BLE central device mAdvertiser.stopAdvertising(mAdvCallback);

这是断开连接.

在这个用例中,请帮助我.提前感谢

Please help me in this use case.THANKS IN ADVANCE

推荐答案

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

Put BluetoothGattServer.connect(BluetoothDevice device, boolean autoConnect) in BluetoothGatt.STATE_CONNECTED before stopAdvertising, because expected Android framework behavior. If you need to continue to hold the link and don't want to advertise anymore, you need to call into additional 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);
    }
}

这篇关于蓝牙LE外围设备停止与蓝牙LE中央设备连接的广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 04:05
查看更多