在处于核心角色的Android应用中 - 您如何获取存储在BLE外设的org.bluetooth.characteristic.location_and_speed服务中的org.bluetooth.service.location_and_navigation特征中的纬度和经度?
我正在尝试以下Java代码 -
private float mLatitude;
private float mLongitude;
private static final UUID LOCATION_AND_NAVIGATION =
UUID.fromString("00001819-0000-1000-8000-00805f9b34fb");
private static final UUID LOCATION_AND_SPEED =
UUID.fromString("00002a67-0000-1000-8000-00805f9b34fb");
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;
private BluetoothGattService mVehicleInfoService;
private BluetoothGattCharacteristic mLocationAndSpeedChar;
这是在设备连接上调用的回调:
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt,
int status, int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED)
gatt.discoverServices();
}
发现服务后,我尝试创建服务和特征对象(并验证它们在调试器中不是null
):
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
mVehicleInfoService =
mBluetoothGatt.getService(LOCATION_AND_NAVIGATION);
mLocationAndSpeedChar =
mVehicleInfoService.getCharacteristic(LOCATION_AND_SPEED);
if (mLocationAndSpeedChar != null)
mBluetoothGatt.readCharacteristic(mLocationAndSpeedChar);
}
这是在特征值读取时调用的回调:
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic ch, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
mLatitude = ch.getIntValue(
BluetoothGattCharacteristic.FORMAT_SINT32,
WHICH_OFFSET_TO_USE_FOR_LAT);
mLongitude = ch.getIntValue(
BluetoothGattCharacteristic.FORMAT_SINT32,
WHICH_OFFSET_TO_USE_FOR_LNG);
}
}
};
请提出两个问题:
答案 0 :(得分:0)
虽然我认为它不适合阅读,但您需要通知。
UUID CCCD = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
mVehicleInfoService = bluetoothGatt.getService(LOCATION_AND_NAVIGATION);
mLocationAndSpeedChar = mVehicleInfoService.getCharacteristic(LOCATION_AND_SPEED);
bluetoothGatt.setCharacteristicNotification(mLocationAndSpeedChar,true);
BluetoothGattDescriptor descriptor = mLocationAndSpeedChar.getDescriptor(CCCD);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);