由于某种原因,设备无法通过BLE传输数据(心率)。它可以很好地连接,电话应用程序也可以发现服务并连接到服务。
我从iOS和Health应用程序开始,实际上看到了一些数据,因此,它最初可以正常工作。后来,为Android应用程序上的BLE连接创建了基础,它连接得很好,发现了正确的服务并将其连接。而且没有数据。用iOS重新检查,也保持静音。假设这是设备的问题(传感器等故障),请将其带到我从其购买的商店中。经过测试,与ANT +一样具有魅力。因此,它必须是BLE问题。该代码本身就是来自Android开发人员指南的示例代码。
{
//0x180D - heart rate service
//0x2A37 - heart rate characteristic
private static HrmHandler instance;
private static UUID UUID_HRM_SERVICE = UUID.fromString("0000180d-0000-1000-8000-00805f9b34fb");
private static UUID UUID_HRM_CHARACTERISTIC = UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb");
private static String LOG_TAG = HrmHandler.class.getSimpleName();
private BluetoothAdapter mBtAdapter;
private BluetoothDevice mBtDevice;
private String mBtDeviceAddress;
private Context mContext;
public static HrmHandler getInstance() {
if (instance == null)
instance = new HrmHandler();
return instance;
}
public void connect(Context ctx) {
mContext = ctx;
BluetoothManager bt_manager = (BluetoothManager) ctx.getSystemService(Context.BLUETOOTH_SERVICE);
mBtAdapter = bt_manager.getAdapter();
if (mBtAdapter == null || !mBtAdapter.isEnabled()) return;
mBtAdapter.startLeScan(new UUID[] {UUID_HRM_SERVICE}, this);
}
public void disconnect() {
}
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.v(LOG_TAG, "Found HRM device " + device.getName());
mBtDevice = device;
mBtAdapter.stopLeScan(this);
mBtDeviceAddress = mBtDevice.getAddress();
mBtDevice.connectGatt(mContext, false,this);
}
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
Log.v(LOG_TAG, String.format("Connection state change, new state %d", newState));
if (mBtDeviceAddress == null || !mBtDeviceAddress.equals(gatt.getDevice().getAddress()))
return; //we are not interested about that connection state
if (newState == BluetoothProfile.STATE_CONNECTED) {
Log.v(LOG_TAG, "Device connected");
gatt.discoverServices();
return;
}
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.v(LOG_TAG, "Device disconnected");
return;
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (gatt != null) {
BluetoothGattCharacteristic characteristic = null;
List<BluetoothGattService> services = gatt.getServices();
for (BluetoothGattService s : services) {
Log.v(LOG_TAG, String.format("Discovered %s, uuid %s", s.toString(), s.getUuid().toString()));
}
BluetoothGattService service = gatt.getService(UUID_HRM_SERVICE);
if (service != null)
characteristic = service.getCharacteristic(UUID_HRM_CHARACTERISTIC);
gatt.readCharacteristic(characteristic);
gatt.setCharacteristicNotification(characteristic, true);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if (!UUID_HRM_CHARACTERISTIC.equals(characteristic.getUuid())) {
Log.v(LOG_TAG, "READ: Not interesting characteristic " + characteristic.getUuid().toString());
return;
}
int flags = characteristic.getProperties();
int format = (flags & 0x01) != 0 ? BluetoothGattCharacteristic.FORMAT_UINT16 : BluetoothGattCharacteristic.FORMAT_UINT8;
int heart_rate = characteristic.getIntValue(format, 1);
Log.v(LOG_TAG, String.format("Heart rate %d bpm", heart_rate) );
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
if (!UUID_HRM_CHARACTERISTIC.equals(characteristic.getUuid())) {
Log.v(LOG_TAG, "CHANGED: Not interesting characteristic " + characteristic.getUuid().toString());
return;
}
gatt.readCharacteristic(characteristic);
}
任何人都可以帮助我前进,尤其是Garmin的人吗?或者这是常见且已知的问题,我应该忘掉它,并尝试使用ANT +?