首页 > 解决方案 > 是否有任何解决方案可以同时将数据从 BluetoothGattServer 发送到连接的设备?

问题描述

这是我的服务器代码:

private BluetoothGattServer mGattServer;

mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();
GattServerCallback gattServerCallback = new GattServerCallback(this, context.getApplicationContext());
mGattServer = mBluetoothManager.openGattServer(context, gattServerCallback);

        BluetoothGattService service = new BluetoothGattService(SERVICE_UUID,
                BluetoothGattService.SERVICE_TYPE_PRIMARY);

        // Write characteristic
        BluetoothGattCharacteristic writeCharacteristic = new BluetoothGattCharacteristic(
                CHARACTERISTIC_ECHO_UUID,
                BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
                // Somehow this is not necessary, the client can still enable notifications//
                BluetoothGattCharacteristic.PERMISSION_WRITE | BluetoothGattDescriptor.PERMISSION_READ);


        BluetoothGattDescriptor clientConfigurationDescriptor = new BluetoothGattDescriptor(
                CHARACTERISTIC_AUDIO_UUID,
                BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE);
        clientConfigurationDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

        service.addCharacteristic(writeCharacteristic);
        mGattServer.addService(service);

连接客户端后,我会将连接的设备添加到我的设备列表中:

@Override    
public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
        super.onConnectionStateChange(device, status, newState);
        mServerActionListener.log("onConnectionStateChange " + device.getAddress()
                + "\nstatus " + status
                + "\nnewState " + newState);

        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mServerActionListener.addDevice(device);
            //mServerActionListener.notifyCharacteristicEcho("start".getBytes());
        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mServerActionListener.removeDevice(device);
        }
    }

我在这里发送数据:

 BluetoothGattService service = mGattServer.getService(SERVICE_UUID);
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);
        characteristic.setValue(value);
        // Indications require confirmation, notifications do not
        boolean confirm = BluetoothUtils.requiresConfirmation(characteristic);

        for (BluetoothDevice device : mDevices) {
            mGattServer.notifyCharacteristicChanged(device, characteristic, confirm);
        }

现在我通过遍历设备列表将数据发送到多个连接的设备。但是我们无法通过这种方式通知流式传输等冗长的数据。因为当要发送多个通知时,应用程序必须等待接收到 onNotificationSent 回调,然后才能发送其他通知。
是否有任何其他解决方案可用于将数据发送到连接的设备?我想同时向所有连接的设备发送通知。

在 ios 中,可以将数据发送到设备列表。'chunk' 是下面代码中的设备列表。

peripheralManager.updateValue(chunk, for: transCharacteristic, onSubscribedCentrals: arrCentral)

标签: bluetoothbluetooth-lowenergybluetooth-gattandroid-ble

解决方案


推荐阅读