【问题标题】:BLE 4.0 getting the broadcast data from device to phoneBLE 4.0 从设备获取广播数据到手机
【发布时间】:2014-11-07 11:49:14
【问题描述】:

我有两个设备。一部 API 级别超过 18 的 Android 手机,另一部是蓝牙设备 4.0。

设备已成功相互连接。 现在命令流程如下: 一种。将“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);

在这种情况下,我通过 GATT 接收器打招呼。这是什么意思。

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。操作结果发送到安卓手机 由设备溴化。 为此,我使用了通知。

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

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

【问题讨论】:

    标签: android


    【解决方案1】:

    看到您的电子邮件,我认为您以某种方式通过蓝牙经典连接,但随后正尝试在 BTLE 协议上“聊天”。

    这就是问题所在。 Android 4.0 设备几乎没有 BTLE。

    即使它有一个 BTLE 芯片(有一些早期的摩托罗拉手机带有 BTLE - 你必须从 Motorola Inc. 导入一个 .jar),它也不会使用你似乎使用的 Android BTLE API。

    长话短说,您应该使用Bluetooth Classic (SPP) 和普通BluetoothSocket,或者使用两个Android BTLE devices

    这里是如何检查设备是否有 BTLE:\

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

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

    // Use this check to determine whether BLE is supported on the device. 
    // Then you can selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
    }
    

    【讨论】:

      【解决方案2】:

      我认为你应该做的两件事。

      1. BluetoothGatt 有自己的 setCharacteristicNotification 方法。除了编写特征描述符来启用通知之外,您还需要调用该方法来启用通知。可以将其视为编写描述符在 BLE 设备上启用通知,而 setCharacteristicNotification 在 Android 设备上启用它。

      所以在上面的 setCharacteristicNotification 方法中,我会添加以下内容:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-24
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多