gpt4 book ai didi

android - Bluetooth LE Cadence 和 Speed Sensor 不在 Android 上发送通知

转载 作者:行者123 更新时间:2023-11-30 03:03:52 25 4
gpt4 key购买 nike

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

它可以与心率监测器配合使用。相关代码如下。随着心率变化,心率监测器 onCharacteristicChanged 每秒触发一次。但是,当我连接 Cadence 和 Speed 传感器时,没有任何反应。这个回调似乎没有被触发 - 虽然我知道 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);
}
};

最佳答案

您是否将通知或指示设置为真,如果是,则检查 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

关于android - Bluetooth LE Cadence 和 Speed Sensor 不在 Android 上发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22136191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com