bluetooth - 是否有任何解决方案可以同时将数据从 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)
解决方案
推荐阅读
- php - 我需要为我的输出按天分别显示自定义,如下所示
- wordpress - 访问 JSON 数据的自定义 wordpress 查询
- node.js - React Native index.js 无法解析模块'/src/App.js 的路径
- angular - Angular 2+ w/Jasmine 测试:[ngClass]=”methodName(configObj)”问题
- r - 如何让 Windows 识别到 R 中的“pt_br.utf-8”
- oracle - 触发 oracle 16 年龄限制
- python - python regex - finding phone number
- cordova - 从子页面返回时,cordova-plugin-wkwebview-engine 重新加载应用程序
- php - 用数据库值替换字符串中的字符
- xaml - Xamarin.Forms 你可以在 XAML 的数据上下文中定义数据吗