1

在一个扮演核心角色的 Android 应用程序中,您如何获取BLE 外围设备的org.bluetooth.service.location_and_navigation服务中的org.bluetooth.characteristic.location_and_speed特征中存储的纬度和经度?

我正在尝试以下 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所需的双精度数?
4

1 回答 1

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);
于 2016-02-04T00:38:42.850 回答