How to extract latitude and longitude from location_and_speed characteristic?

那年仲夏 提交于 2020-02-06 04:26:24

问题


In an Android app in central role - how do you please get a hold of the latitude and longitude stored in the org.bluetooth.characteristic.location_and_speed characteristic in the org.bluetooth.service.location_and_navigation service of a BLE peripheral?

I am trying the following Java-code -

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;

Here is the callback called on device connect:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, 
        int status, int newState) {

        if (newState == BluetoothProfile.STATE_CONNECTED)
            gatt.discoverServices();
    }

After the services have been discovered I try to create the service and characteristic objects (and verify they are not null in debugger):

    @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);
    }

Here is the callback called on characteristic value read:

    @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);
        }
    }
};

Two questions please:

  1. What offset parameters should I pass to the getIntValue method above?
  2. How to convert the integers to doubles needed by Google Maps Location?

回答1:


Although I think it won't work for a read you need a notify.

            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);


来源:https://stackoverflow.com/questions/31967357/how-to-extract-latitude-and-longitude-from-location-and-speed-characteristic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!