BLE 4.0从设备到手机获取广播数据

时间:2014-11-07 11:49:14

标签: android

enter image description here

我有两个设备。一款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;
    }

湾蓝牙设备将执行一些操作 由蓝牙设备自动完成

℃。操作结果发送到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);
        }
    }

我没有收到任何数据。请问。

2 个答案:

答案 0 :(得分:0)

看到了您的电子邮件,我认为您通过蓝牙经典以某种方式连接,但之后又尝试聊天'关于BTLE协议。

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

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

所以长话短说,你应该使用Bluetooth Classic (SPP)和正常的BluetoothSocket,或者使用两个Android BTLE devices

以下是检查设备是否具有BTLE的方法:\

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

  

  但是,如果您想让您的应用适用于不支持BLE的设备,您仍然应该>在您的应用的清单中包含此元素,但必须设置=" 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();
}

答案 1 :(得分:0)

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

  1. BluetoothGatt有自己的setCharacteristicNotification方法。除了编写特征描述符以启用通知之外,还需要调用该方法来启用通知。想象一下,编写描述符可以在BLE设备上启用通知,而setCharacteristicNotification可以在Android设备上启用它。
  2. 因此,在上面的setCharacteristicNotification方法中,我将添加以下内容:

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