Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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
Java 如何知道一个可编程设备何时订阅Android上的某个特性?_Java_Android_Bluetooth Lowenergy - Fatal编程技术网

Java 如何知道一个可编程设备何时订阅Android上的某个特性?

Java 如何知道一个可编程设备何时订阅Android上的某个特性?,java,android,bluetooth-lowenergy,Java,Android,Bluetooth Lowenergy,来自iOS-dev后台,当使用蓝牙LE作为外围设备时,您可以在“中央”LE设备订阅(启用通知)某个特征时注册回调 我很难看到这是如何在Android上实现的。如果您使用蓝牙LE作为中心,我可以看到您的订阅方式: bluetoothGatt.setCharacteristicNotification(characteristicToSubscribeTo,true) 这与iOS上的相同: peripheralDevice.setNotifyValue(true,用于特征特性订阅) 现在,在iOS上

来自iOS-dev后台,当使用蓝牙LE作为外围设备时,您可以在“中央”LE设备订阅(启用通知)某个特征时注册回调

我很难看到这是如何在Android上实现的。如果您使用蓝牙LE作为中心,我可以看到您的订阅方式:
bluetoothGatt.setCharacteristicNotification(characteristicToSubscribeTo,true)

这与iOS上的相同:
peripheralDevice.setNotifyValue(true,用于特征特性订阅)

现在,在iOS上调用上述命令后,在外围端,您会收到一个回调,表示central已使用类似于以下的方法进行订阅:
外围设备管理器(manager,central subscribedCentral DidSubscribeTecharacteristic characteristic)
然后提供对订阅/启用通知的设备的引用以及对其特性的引用

Android上的等价物是什么

为了清楚起见,我将指出,我不需要订阅Android上的某个特性,该设备作为外围设备,需要在其他设备订阅特性时得到通知


很抱歉,如果这很明显,但我在文档或其他地方找不到它。

在设置悬赏后,我有了一个想法,哈哈,我应该再等一会儿,让我的大脑有更多的时间思考;)

iOS似乎已经将订阅的内部工作方式抽象了一点。在ios CoreBluetooth框架下,中心会写入特征的某个“描述符”,表示中心希望订阅特征的值

以下是需要添加到
BluetoothGattServerCallback
子类的代码:

    @Override
    public void onDescriptorWriteRequest(BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
        super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite, responseNeeded, offset, value);

        Log.i(TAG, "onDescriptorWriteRequest, uuid: " + descriptor.getUuid());

        if (descriptor.getUuid().equals(Descriptor.CLIENT_CHARACTERISTIC_CONFIGURATION) && descriptor.getValue().equals(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
            Log.i(TAG, "its a subscription!!!!");

            for (int i = 0; i < 30; i++) {
                descriptor.getCharacteristic().setValue(String.format("new value: %d", i));
                mGattServer.notifyCharacteristicChanged(device, descriptor.getCharacteristic(), false);
            }
        }
    }
@覆盖
public void onDescriptorWriteRequest(蓝牙设备设备、int请求ID、蓝牙GattDescriptor描述符、布尔准备写入、布尔响应eded、int偏移量、字节[]值){
super.onDescriptorWriteRequest(设备、请求ID、描述符、preparedWrite、响应eded、偏移量、值);
Log.i(标记“onDescriptorWriteRequest,uuid:”+descriptor.getUuid());
if(descriptor.getUuid().equals(descriptor.CLIENT\u CHARACTERISTIC\u CONFIGURATION)和&descriptor.getValue().equals(BluetoothGattDescriptor.ENABLE\u NOTIFICATION\u VALUE)){
Log.i(标记“这是订阅!!!!”);
对于(int i=0;i<30;i++){
descriptor.getCharacteristic().setValue(String.format(“新值:%d”,i));
mGattServer.notifyCharacteristicChanged(设备,描述符.getCharacteristic(),false);
}
}
}
最好用于uuid(
描述符。客户端特征配置
,但原始值为
00002902-0000-1000-8000-00805f9b34fb

同意@stefreak

以及

未对远程设备执行任何操作,此API仅更改了本地蓝牙堆栈上的一个通知位,即,如果外围设备向本地发送通知,本地堆栈将判断应用程序是否已注册此通知,如果是,则将其传输到应用程序,否则忽略它


因此,除了setCharacteristicNotification之外,您还需要为注册的通知编写writeDescriptor(这是告诉远程用户需要发送通知的步骤)。

您需要回调之类的东西吗?不确定问题出在哪里?如果不是的话,很抱歉,但我想我很清楚我需要某种方法来了解设备是如何订阅的。如果是通过回调,那太好了,但我只需要知道如何获取这些信息。这是iOS上的一个回调,所以这是我唯一的一个例子。@kanecheshire你找到什么了吗?是否需要相同的
BluetoothGattCallback.onCharacteristicChanged()
,我认为BluetoothGattCallback是用于中央而非外围设备的。。。还是这样@josemigallasI还注意到,您需要在服务器端添加特性以使其正常工作:
BluetoothGattDescriptor描述符=新的BluetoothGattDescriptor(UUID.fromString(“00002902-0000-1000-8000-00805f9b34fb”)、BluetoothGattDescriptor.PERMISSION\u READ | BluetoothGattDescriptor.PERMISSION\u WRITE);beacCharacteristic.addDescriptor(描述符);service.addCharacteristic(beacCharacteristic)正确。Gatt服务器还需要判断描述符的值来决定是否发送通知。但是我认为android API的命名有时没有意义…啊,这看起来不错。我要试试这个!抱歉,回复时间太长了,古怪已经有一段时间没有这么做了:/np如果在这么多个月后仍然有用,那就很高兴了虽然我还没有机会尝试一下,但这篇文章似乎确实与您在上面发布的内容相关联:
bluetoothGatt.setCharacteristicNotification(characteristicToSubscribeTo, true);