Android BLE 4.0从设备到电话获取广播数据

Android BLE 4.0从设备到电话获取广播数据,android,Android,我有两个装置。一款API级别超过18的Android手机和另一款蓝牙设备4.0 设备已成功相互连接。 现在命令流程如下: A.将“hello”文本发送到蓝牙设备 UUID uuid = UUID.fromString("18cda784-4bd3-4370-85bb-bfed91ec86af"); BluetoothGattCharacteristic selectedChar = selectedGattService.getCharacteristic(uuid); mBluetoothLe

我有两个装置。一款API级别超过18的Android手机和另一款蓝牙设备4.0

设备已成功相互连接。 现在命令流程如下: A.将“hello”文本发送到蓝牙设备

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);
在这种情况下,我通过关贸总协定的接受者得到你好。这是什么意思

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。蓝牙设备将执行一些操作 自动完成蓝牙设备

c。操作结果被发送到android手机 由设备驱动。 为此,我使用了通知

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);
        }
    }

我没有得到任何数据。任何想法请。

看到您的电子邮件,我认为您通过蓝牙经典连接,但正在尝试通过BTLE协议“聊天”

这就是问题所在。 安卓4.0设备几乎不可能有BTLE

即使它有一个BTLE芯片(有一些早期的摩托罗拉手机带有BTLE——你必须从摩托罗拉公司进口一个.jar),它也不会使用你似乎使用的Android BTLE API

所以长话短说,你要么用普通的,要么用两个

以下是如何检查设备是否有BTLE:\

如果您要声明您的应用仅可用于支持BLE的设备,请在应用的清单中包含以下内容:

但是,如果你想让你的应用程序可用于不支持BLE的设备,你仍然应该>在你的应用程序清单中包含此元素,但设置required=“false”。然后在运行时,您可以使用PackageManager.hasSystemFeature()确定可恢复的可用性:


我认为你应该做两件事

  • BluetoothGatt有自己的setCharacteristicNotification方法。除了编写特征描述符以启用通知外,还需要调用该方法以启用通知。可以将其视为写入描述符在可编程设备上启用通知,而setCharacteristicNotification在Android设备上启用通知
  • 因此,在上面的setCharacteristicNotification方法中,我将添加以下内容:

    // I'm assuming you have access to the BluetoothGatt object in your BluetoothGattService object
    gatt.setCharacteristicNotification(characteristic, true);
    
  • 在收到描述符已写入的确认之前,不应尝试向特征写入任何数据。这意味着您需要等待,直到在BluetoothGattCallback的实现中获得对onDescriptorWrite的回调
  • // I'm assuming you have access to the BluetoothGatt object in your BluetoothGattService object
    gatt.setCharacteristicNotification(characteristic, true);