如何从location_and_speed特征中提取纬度和经度?

时间:2015-08-12 13:57:43

标签: android bluetooth bluetooth-lowenergy android-bluetooth android-ble

在处于核心角色的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);
        }
    }
};

请提出两个问题:

  1. 我应该将哪些偏移参数传递给上面的getIntValue方法?
  2. 如何将整数转换为Google Maps Location所需的双打?

1 个答案:

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