小编Mag*_*gic的帖子

BLE设备自动与Android设备断开连接.Android BLE

我正在使用Android Nexus 7通过Bluetooth Low Energy链接连接设备.如果我不与设备进行任何通信,我可以连接设备并保持连接.

但是,如果我通过单击按钮启用一个特定特征的通知,则设备将在数秒传输数据后与平板电脑断开连接.

有谁知道可能是什么问题?非常感谢你!

这是我的代码:

    public boolean setCharacteristicNotification(boolean enabled){

      if (mBluetoothAdapter == null || mBluetoothGatt == null) {
          Log.w(TAG, "BluetoothAdapter not initialized");
               return false;      
      }

      BluetoothGattService Service = mBluetoothGatt.getService(UUID_MY_SERVICE);
      if (Service == null) {
          Log.e(TAG, "service not found!");
          return false;
      }

      BluetoothGattCharacteristic characteristic = Service.getCharacteristic(UUID_MY_CHARACTERISTIC);

      final int charaProp = characteristic.getProperties();

      if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
          mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); 

            mBluetoothGatt.writeDescriptor(descriptor);

          return true;
      }

    return false;

}
Run Code Online (Sandbox Code Playgroud)

android bluetooth bluetooth-lowenergy android-4.3-jelly-bean android-4.4-kitkat

10
推荐指数
1
解决办法
6753
查看次数

看不懂特色.Android BLE

我想将我的远程BLE设备的特定数据的数据读取到我的Android平板电脑Nexus 7.

问题是,即使没有打电话,我也可以通过启用该特性的通知来接收数据readCharacteristic.但是,readCharacteristic如果不启用通知,我无法通过调用成功读取特征.

mBluetoothGatt.readCharacteristic(characteristic)返回false.因此,该功能onCharacteristicRead从未被触发过.我还检查了属性值BluetoothGattCharacteristic.PROPERTY_READ,它是30.

有没有人对这里发生的事情有所了解?我真的需要分别阅读这个特性.因为如果我只根据通知分析数据,我无法弄清楚数据的开始位置.这是因为我的设备每次都会发送12bytes.它会不断发送字节数组.但是,通知会一次一个字节地给我带来数据.所以我不知道哪个是字节数组的起始字节.

我正在使用Android提供的示例代码.

这是片段:

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    boolean status = mBluetoothGatt.readCharacteristic(characteristic);
    System.out.println("Initialize reading process status:" + status);

}



public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if …
Run Code Online (Sandbox Code Playgroud)

android characteristics bluetooth-lowenergy android-bluetooth android-4.3-jelly-bean

6
推荐指数
1
解决办法
1万
查看次数

Android BLE,无法真正写出特性

我正在开发一个 Android 项目,该项目通过 BLE 链接连接 Nexus 7 和生物传感器。问题是,我可以成功检测并获取传感器的服务和特征列表。当我写一些数据到特定的特性时,onCharacteristicWrite会自动调用并显示我写操作成功。但是,传感器永远不会从平板电脑接收任何信息。如果我在 iPhone 上使用类似的应用程序,一切正常。所以设备没有问题。有没有人知道这个问题?

这是我的写代码:

 private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            mConnected = true;
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" +
                    mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            mConnected = false;
            Log.i(TAG, "Disconnected from GATT server.");
        }
    }

    @Override
    public void …
Run Code Online (Sandbox Code Playgroud)

android characteristics bluetooth-lowenergy

5
推荐指数
1
解决办法
6125
查看次数