Bluetooth с низким энергопотреблением android - PullRequest
0 голосов
/ 25 марта 2020

Я работаю с микробитальным микроконтроллером BB c и столкнулся с некоторыми проблемами, пытаясь собрать данные датчика с указанного устройства с помощью android. Мне нужно прочитать шесть различных характеристик по двум осям xyz служб. Поскольку это устройство такого низкого уровня, характеристики уведомлений c отсутствуют, поэтому мне необходимо постоянно считывать данные с устройства для сбора данных, но в настоящее время я просто получаю нулевые или неправильные значения.

 public void setupBluetooth() {

        mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();

        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceaddress);
        mBluetoothGatt = device.connectGatt(this,false,mGattCallback);

        BluetoothGattService accelService = new BluetoothGattService(Accel_Service_UUID,BluetoothGattService.SERVICE_TYPE_PRIMARY);

        BluetoothGattCharacteristic accelXChar = new BluetoothGattCharacteristic(Accel_X_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);
        BluetoothGattCharacteristic accelYChar = new BluetoothGattCharacteristic(Accel_Y_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);
        BluetoothGattCharacteristic accelZChar = new BluetoothGattCharacteristic(Accel_Z_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);

        accelService.addCharacteristic(accelXChar);
        accelService.addCharacteristic(accelYChar);
        accelService.addCharacteristic(accelZChar);

        //Log.d("scan","chars" +accelService.getCharacteristics());

    }
    //get callbacks when something changes
    private final BluetoothGattCallback mGattCallback=new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                Log.d("scan","CONNECTED");
                Log.d("scan","gatt device "+gatt.getDevice());
                gatt.discoverServices();

                //BluetoothGattCharacteristic accelXChar = new BluetoothGattCharacteristic(Accel_X_CHAR_UUID,BluetoothGattCharacteristic.PROPERTY_READ,BluetoothGattCharacteristic.PERMISSION_READ);

               // Log.d("scan","char "+gatt.readCharacteristic(accelXChar));
               // Log.d("scan","serv "+gatt.getServices());
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            List<BluetoothGattService> services = gatt.getServices();

                gatt.readCharacteristic(services.get(1).getCharacteristics().get(0));



            Log.d("scan","Number of Services "+  services.size());

           /* gatt.readCharacteristic(services.get(1).getCharacteristics().get(0));
            gatt.readCharacteristic(services.get(1).getCharacteristics().get(1));
            gatt.readCharacteristic(services.get(1).getCharacteristics().get(2));
            gatt.readCharacteristic(services.get(1).getCharacteristics().get(3));*/


            for(int x = 0;x < 4;x++){
                BluetoothGattService[] serviceArray = new BluetoothGattService[4];
                serviceArray[x] = services.get(x);
                Log.d("scan","service  "+ (x + 1) + " " + serviceArray[x].getUuid());
            }

            UUID uuid;
            List<BluetoothGattCharacteristic> gattCharacteristics;
            ArrayList<BluetoothGattCharacteristic> charas;
            for (BluetoothGattService gattService : services) {
                gattCharacteristics = gattService.getCharacteristics();
                charas = new ArrayList<>();
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                        gatt.readCharacteristic(gattCharacteristic);
                    charas.add(gattCharacteristic);
                    uuid = gattCharacteristic.getUuid();
                    //gattCharacteristic.;
                    //Log.d("scan","describe "+ gattCharacteristic.describeContents());
                    Log.d("scan","uuid "+uuid);

                    //setCharacteristicNotification(gattCharacteristic, true);




                    if (uuid.equals(Accel_X_CHAR_UUID)||uuid.equals(Accel_Y_CHAR_UUID)||uuid.equals(Accel_Z_CHAR_UUID)) {
                        Log.d("scan","value of char  " + gattCharacteristic.getValue().toString());


                    }
                }
            }
        }





        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);

            Log.d("scan","char read "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16,1));
            Log.d("scan","char read "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,1));
            Log.d("scan","char read "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8,1));

            //Log.d("scan","char read "+characteristic.getValue());
            byte[] bytes = characteristic.getValue();
            ByteBuffer buffer = ByteBuffer.wrap(bytes);
            Log.d("scan","char read "+buffer.getFloat());
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
            super.onCharacteristicChanged(gatt, characteristic);

            Log.d("scan","char changed "+characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16,0));



            Log.d("scan", "char changed"+ characteristic.getValue());


        }

    };

1 Ответ

0 голосов
/ 25 марта 2020

Это утверждение '.. такое низкоуровневое устройство, для которого нет характеристики уведомления c ..', НЕ верно.

Извлечь https://developer.android.com/guide/topics/connectivity/bluetooth-le


private BluetoothGatt bluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
bluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
        UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...