【问题标题】:Bluetooth LE Cadence and Speed Sensor not sending notifications on Android蓝牙 LE 踏频和速度传感器未在 Android 上发送通知
【发布时间】:2014-03-03 01:28:29
【问题描述】:

我遇到了蓝牙 LE 踏频和速度传感器 (Wahoo SC) 的问题。我正在使用 Android BluetoothGattLe 示例应用程序,该应用程序已针对节奏和速度传感器进行了调整。

它可以与心率监测器配合使用。相关代码如下。随着心率监测器 onCharacteristicChanged 每秒触发一次,随着心率的变化。但是,当我连接踏频和速度传感器时,什么也没有发生。这个回调似乎没有被触发——尽管我知道 onConnectionStateChange 是在连接时触发的。

有什么想法吗?

    /**
 * Service for managing connection and data communication with a GATT server hosted on a
 * given Bluetooth LE device.
 */
public class BluetoothLeService extends Service {
    private final static String TAG = BluetoothLeService.class.getSimpleName();

    private BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    private String mBluetoothDeviceAddress;
    private BluetoothGatt mBluetoothGatt;
    private int mConnectionState = STATE_DISCONNECTED;

    private static final int STATE_DISCONNECTED = 0;
    private static final int STATE_CONNECTING = 1;
    private static final int STATE_CONNECTED = 2;

    public final static String ACTION_GATT_CONNECTED =
            "com.example.bluetooth.le.ACTION_GATT_CONNECTED";
    public final static String ACTION_GATT_DISCONNECTED =
            "com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
    public final static String ACTION_GATT_SERVICES_DISCOVERED =
            "com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
    public final static String ACTION_DATA_AVAILABLE =
            "com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
    public final static String EXTRA_DATA =
            "com.example.bluetooth.le.EXTRA_DATA";

    public final static UUID UUID_HEART_RATE_MEASUREMENT =
            UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
    public final static UUID UUID_CSC_MEASUREMENT =
            UUID.fromString(SampleGattAttributes.CSC_MEASUREMENT);

    // Implements callback methods for GATT events that the app cares about.  For example,
    // connection change and services discovered.
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                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) {
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                broadcastUpdate(intentAction);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    };

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy


    【解决方案1】:

    您是否设置了 notify 或如果是则指示为 true 然后检查 setCharacteristicNotification() 是否返回值。并检查您是否有读取特征的权限。并请打印 onDescripterWrite 回调方法的状态值。

      for notification
    http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification
    
    descriptor call back method javadoc
    
    http://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onDescriptorWrite%28android.bluetooth.BluetoothGatt,%20android.bluetooth.BluetoothGattDescriptor,%20int%29
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2022-01-04
      • 2021-12-26
      相关资源
      最近更新 更多