BLE with Android Studio

1

I'm doing a project in which I have to read a characteristic of a BLE module, the feature that I need to read is the Heart rate with U UID 00002a37-0000-1000-8000-00805f9b34fb del servicio con UUID 0000180d-0000-1000-8000-00805f9b34f . I have tried with the code that I show below but I do not get it. Someone can help me with ideas or if someone knows of a tutorial to follow I would appreciate them. I must note that this feature is like Notify and I am working with a Microchip BLE.

private static final String MLDP_PRIVATE_SERVICE = "0000180d-0000-1000-8000-00805f9b34fb"; //Private service for Microchip MLDP
private static final String MLDP_DATA_PRIVATE_CHAR = "00002a37-0000-1000-8000-00805f9b34fb"; //Characteristic for MLDP Data, properties - notify, write
private static final String MLDP_CONTROL_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a0003ff"; //Characteristic for MLDP Control, properties - read, write
private static final String CHARACTERISTIC_NOTIFICATION_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";    //Special UUID for descriptor needed to enable notifications

private void findMldpGattService(List<BluetoothGattService> gattServices) {
    if (gattServices == null) {                                                     //Verify that list of GATT services is valid
        Log.d(TAG, "findMldpGattService found no Services");
        return;
    }
    String uuid;                                                                    //String to compare received UUID with desired known UUIDs
    mDataMDLP = null;                                                               //Searching for a characteristic, start with null value

    for (BluetoothGattService gattService : gattServices) {                         //Test each service in the list of services
        uuid = gattService.getUuid().toString();                                    //Get the string version of the service's UUID
        if (uuid.equals(MLDP_PRIVATE_SERVICE)) {                                    //See if it matches the UUID of the MLDP service 
            List<BluetoothGattCharacteristic> gattCharacteristics = gattService.getCharacteristics(); //If so then get the service's list of characteristics
            for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { //Test each characteristic in the list of characteristics
                uuid = gattCharacteristic.getUuid().toString();                     //Get the string version of the characteristic's UUID
                if (uuid.equals(MLDP_DATA_PRIVATE_CHAR)) {                          //See if it matches the UUID of the MLDP data characteristic
                    mDataMDLP = gattCharacteristic;                                 //If so then save the reference to the characteristic 
                    Log.d(TAG, "Found MLDP data characteristics");
                } 
                else if (uuid.equals(MLDP_CONTROL_PRIVATE_CHAR)) {                  //See if UUID matches the UUID of the MLDP control characteristic
                    mControlMLDP = gattCharacteristic;                              //If so then save the reference to the characteristic 
                    Log.d(TAG, "Found MLDP control characteristics");
                }
                final int characteristicProperties = gattCharacteristic.getProperties(); //Get the properties of the characteristic
                if ((characteristicProperties & (BluetoothGattCharacteristic.PROPERTY_NOTIFY)) > 0) { //See if the characteristic has the Notify property
                    mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true); //If so then enable notification in the BluetoothGatt
                    BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG)); //Get the descripter that enables notification on the server
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); //Set the value of the descriptor to enable notification
                    mBluetoothGatt.writeDescriptor(descriptor);                     //Write the descriptor
                }
                if ((characteristicProperties & (BluetoothGattCharacteristic.PROPERTY_INDICATE)) > 0) { //See if the characteristic has the Indicate property
                    mBluetoothGatt.setCharacteristicNotification(gattCharacteristic, true); //If so then enable notification (and indication) in the BluetoothGatt
                    BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG)); //Get the descripter that enables indication on the server
                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); //Set the value of the descriptor to enable indication
                    mBluetoothGatt.writeDescriptor(descriptor);                     //Write the descriptor
                }
                if ((characteristicProperties & (BluetoothGattCharacteristic.PROPERTY_WRITE)) > 0) { //See if the characteristic has the Write (acknowledged) property
                    gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); //If so then set the write type (write with acknowledge) in the BluetoothGatt
                }
                if ((characteristicProperties & (BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) > 0) { //See if the characteristic has the Write (unacknowledged) property
                    gattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); //If so then set the write type (write with no acknowledge) in the BluetoothGatt
                }
            }
            break;                                                                  //Found the MLDP service and are not looking for any other services
        }
    }
    if (mDataMDLP == null) {                                                        //See if the MLDP data characteristic was not found
        Toast.makeText(this, R.string.mldp_not_supported, Toast.LENGTH_SHORT).show(); //If so then show an error message
        Log.d(TAG, "findMldpGattService found no MLDP service");
        finish();                                                                   //and end the activity
    }
    mHandler.postDelayed(new Runnable() {                                           //Create delayed runnable that will send a roll of the die after a delay
        @Override
        public void run() {
            updateDieState();                                                       //Update the state of the die with a new roll and send over BLE
            byte[]dato=mDataMDLP.getValue();
        }
    }, 200);


private void updateDieState() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mDataMDLP.setValue("=>R" + redDie.Roll() + "\r\n");                     //Set value of MLDP characteristic to send die roll information
            writeCharacteristic(mDataMDLP);                                         //Call method to write the characteristic
            redDieText.setText(Byte.toString(redDie.View()));                       //Set the die text to show the last die roll
            int height = redDieText.getHeight();                                    //Get the available height for the text object
            redDieText.setTextSize(TypedValue.COMPLEX_UNIT_PX, (height * 4 / 5));   //Set the size of the text to take up 80% available space
        }
    });
}


@Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //A request to Read has completed
        if (status == BluetoothGatt.GATT_SUCCESS) {                                 //See if the read was successful
        String dataValue = characteristic.getStringValue(0);                        //Get the value of the characteristic
        processIncomingPacket(dataValue);                                           //Process the data that was received
        }
    }
    
asked by Kevtho 16.11.2016 в 14:35
source

0 answers