BLE 4.0从设备获取广播数据来电话电话、数据、设备、BLE

2023-09-13 01:41:12 作者:柇荟.

我有两个设备。一款使用Android操作系统的手机与API级别超过18,另一种是蓝牙4.0的设备。

设备成功连接到彼此。 现在流的命令是如下: 一个。发送你好文本蓝牙设备。

  UUID UUID = UUID.fromString(18cda784-4bd3-4370-85bb-bfed91ec86af);
BluetoothGattCharacteristic selectedChar = selectedGattService.getCharacteristic(UUID);
mBluetoothLeService.setCharacteristicNotification(selectedChar,真正的);
布尔标志= selectedChar.setValue();
mBluetoothLeService.writeCharacteristic(selectedChar);
 

在这种情况下,我通过关贸总协定reciver越来越打招呼。这个是什么意思。

  registerReceiver(mGattUpdateReceiver,makeGattUpdateIntentFilter());
    私有静态的IntentFilter makeGattUpdateIntentFilter(){
        最后的IntentFilter的IntentFilter =新的IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        intentFilter.addAction(BluetoothLeService.EXTRA_DATA);
        返回IntentFilter的;
    }
 

乙。蓝牙设备将执行某些操作 通过蓝牙设备自动完成

℃。操作的结果发送到android手机 按设备Brodcated。 为此,我使用的通知。

 公共无效setCharacteristicNotification(BluetoothGattCharacteristic特点,
            布尔启用){
        如果(mBluetoothAdapter == NULL || mBluetoothGatt == NULL){
            返回;
        }

        BluetoothGattDescriptor描述符= characteristic.getDescriptor(UUID
                .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        的System.out.println(nov7 descriptordescriptor+描述符);
        如果(描述符!= NULL){
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(描述);
        }
    }
 
ble广播基础

我没有收到任何数据。任何想法吧。

解决方案

两件事情我看你应该做的。

BluetoothGatt都有自己的setCharacteristicNotification方法。除了写作特征描述启用通知,则需要调用该方法来启用通知。把它看成是写的描述使BLE设备上的通知和setCharacteristicNotification使其在Android设备上。

因此​​,在您setCharacteristicNotification上述方法我想补充以下内容:

  //我假设你有你的BluetoothGattService对象访问BluetoothGatt对象
gatt.setCharacteristicNotification(特征,真实);
 

您不应尝试,直到你收到的描述是书面确认写入任何数据的特点。这意味着你需要等待,直到你得到你的实现BluetoothGattCallback的回调onDescriptorWrite。

I have two devices. one Android phone with API level more than 18 and other is blue-tooth device 4.0.

Devices are successfully connected to each other. Now flow of command is as follow: a. Send the "hello" text to blue-tooth device.

UUID uuid = UUID.fromString("18cda784-4bd3-4370-85bb-bfed91ec86af");
BluetoothGattCharacteristic selectedChar = selectedGattService.getCharacteristic(uuid);
mBluetoothLeService.setCharacteristicNotification(selectedChar, true);
boolean flag = selectedChar.setValue("");
mBluetoothLeService.writeCharacteristic(selectedChar);

In this case I am getting hello through the GATT reciver. what is the meaning of this.

registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
    private static IntentFilter makeGattUpdateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
        intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
        intentFilter.addAction(BluetoothLeService.EXTRA_DATA);
        return intentFilter;
    }

b. bluetooth device will perform some operations auto done by bluetooth device

c. Result of operation is sent to android phone Brodcated by device. For this I used notification.

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
            boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            return;
        }

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID
                .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        System.out.println("nov7 descriptordescriptor " + descriptor);
        if (descriptor != null) {
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mBluetoothGatt.writeDescriptor(descriptor);
        }
    }

I am not getting any data. Any idea please.

解决方案

Two things I see that you should be doing.

BluetoothGatt has its own setCharacteristicNotification method. In addition to writing the characteristic descriptor to enable notifications, you need to call that method to enable notifications. Think of it as writing the descriptor enables notifications on the BLE device and setCharacteristicNotification enables it on the Android device.

So in your setCharacteristicNotification method above I would add the following:

// I'm assuming you have access to the BluetoothGatt object in your BluetoothGattService object
gatt.setCharacteristicNotification(characteristic, true);

You shouldn't be trying to write any data to the characteristic until you have received confirmation that the descriptor was written. That means you need to wait until you get the callback to onDescriptorWrite in your implementation of BluetoothGattCallback.