Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android棒棒糖外设:呼叫BluetoothLeAdvertiser.stopAdvertising(AdvertiseCallback)会断开与连接的中央服务器的连接_Android_Bluetooth Lowenergy_Ads - Fatal编程技术网

Android棒棒糖外设:呼叫BluetoothLeAdvertiser.stopAdvertising(AdvertiseCallback)会断开与连接的中央服务器的连接

Android棒棒糖外设:呼叫BluetoothLeAdvertiser.stopAdvertising(AdvertiseCallback)会断开与连接的中央服务器的连接,android,bluetooth-lowenergy,ads,Android,Bluetooth Lowenergy,Ads,我希望在中心连接(并订阅特定特性)时停止广告: 调用stopAdvertising()时,中央和外围设备将断开连接 从logcat: 04-01 11:26:06.179 7068-7085/包装等级﹕ onDescriptorWriteRequest() 04-01 11:26:06.179 7068-7085/包装等级﹕ ----- 特性:80a1a1a5-8b5b-e88b-9d24-2e609654b852 04-01 11:26:06.207 7068-7085/包D/蓝牙GattSe

我希望在中心连接(并订阅特定特性)时停止广告:

调用stopAdvertising()时,中央和外围设备将断开连接 从logcat:

04-01 11:26:06.179 7068-7085/包装等级﹕ onDescriptorWriteRequest()

04-01 11:26:06.179 7068-7085/包装等级﹕ ----- 特性:80a1a1a5-8b5b-e88b-9d24-2e609654b852

04-01 11:26:06.207 7068-7085/包D/蓝牙GattServer﹕ onServerConnectionState()-status=0 serverIf=5 device=00:07:80:2F:0F:A2

注释了stopAdvertising()之后,与中央服务器的连接(和通信)将继续


有没有人在Android的BLE外围设备实现中遇到过这个问题?在iOS中,不存在此类问题。

您不需要在进入连接后调用
停止广告

LE控制器的链路层有5种状态:“空闲”、“广告”、“扫描”、“启动”和“连接”

当你做广告时,你处于“广告”状态。当它连接时,它将进入“已连接”状态

最有可能的情况是,
stopAdvertising
方法假定您在调用时处于“广告”状态,并且在不检查此项的情况下,执行您在“广告”状态下调用它时应该执行的操作:它进入“空闲”状态

因此,当您调用它时,无论当前状态如何,LL都会进入“空闲”状态


这似乎是Android的BLE主机堆栈中的一个bug。在“已连接”状态下调用
stopAdvertising
时,正确的行为应该是返回错误代码(例如“此命令的状态无效”)或直接忽略。

Bogdan的回答适用于蓝牙LE 4.0。但是,4.1外围设备可以连接到多个中央设备,因此外围设备可以在连接完成后决定是否停止广告,而不会立即失去连接。这看起来像是5.1.1版Android L中的一个bug,如果您添加更多日志,将有助于解决此问题。也是5.0还是5.1/5.01?我在Nexus6上有同样的问题,Android 5.1.1和6.0.1都是外设。请调查一下,我在7.0中仍然有这种行为。在我呼叫停止广告后,连接的外围设备断开连接。这对我们来说非常不方便,因为我们的应用程序只需要1个连接,但如果我们不能停止广告,则可以输入更多连接。
private final BluetoothGattServerCallback bleCallbacks = new BluetoothGattServerCallback() {
    @Override
    public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value){
        Log.v(DEBUG_TAG, "onDescriptorWriteRequest()...");

        BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
        Log.v(DEBUG_TAG, "----- characteristic: " + characteristic.getUuid().toString());

        if (characteristic.equals(peripheralCharacteristic)){
            descriptor.setValue(value);
            if (bluetoothGattServer.sendResponse(device,
                    requestId,
                    BluetoothGatt.GATT_SUCCESS,
                    offset,
                    value)){
                central = device;
                stopAdvertising(); //causes disconnection
                return;
            }
        }

        bluetoothGattServer.sendResponse(device,
                requestId,
                BluetoothGatt.GATT_WRITE_NOT_PERMITTED,
                0,
                null);
    }
...
}

private void stopAdvertising(){
    if (bluetoothLeAdvertiser != null) {
        bluetoothLeAdvertiser.stopAdvertising(advertiseCallback);
    }
}