Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

bluetooth lowenergy - Working with BLE Android 4.3 how to write characteristics?

I am working on a BLE project (Android application) using Android 4.3 API, i have used sample BLE app it is only reading characteristics in DeviceControlActivity.activity, but i want to write characteristics and send it to BLE chip on clicking a button. How can I write data on chip cc2540 .. Basically i don't know the step by step procedure to write characteristics.

write i can only see the name and id of device with following piece of code in DeviceControlActivity

 private final ExpandableListView.OnChildClickListener servicesListClickListner =
        new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {
                if (mGattCharacteristics != null) {
                    final BluetoothGattCharacteristic characteristic =
                            mGattCharacteristics.get(groupPosition).get(childPosition);
                    final int charaProp = characteristic.getProperties();
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
                        // If there is an active notification on a characteristic, clear
                        // it first so it doesn't update the data field on the user interface.
                        if (mNotifyCharacteristic != null) {
                            mBluetoothLeService.setCharacteristicNotification(
                                    mNotifyCharacteristic, false);
                            mNotifyCharacteristic = null;
                        }
                        mBluetoothLeService.readCharacteristic(characteristic);
                        showDialog("reading");
                    }

                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
                        // If there is an active notification on a characteristic, clear
                        // it first so it doesn't update the data field on the user interface.
                        if (mNotifyCharacteristic != null) {
                            mBluetoothLeService.setCharacteristicNotification(
                                    mNotifyCharacteristic, false);
                            mNotifyCharacteristic = null;
                        }
                        mBluetoothLeService.writeCharacteristic(characteristic);
                        showDialog("writing");
                        //characteristic.setValue(bytes);
                        //characteristic.setValue("testing");
                        //characteristic.setWriteType(BluetoothGattCharacteristic.PERMISSION_WRITE);
                    }
                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        mNotifyCharacteristic = characteristic;
                        mBluetoothLeService.setCharacteristicNotification(
                                characteristic, true);
                    }

                    byte[] value = {0x00, (byte) (0xB9) , 0x0D, (byte) (0x90), 0x2F};
                    if(!characteristic.setValue(value)) 
                    {
                        Log.w(TAG, "Couldn't set characteristic's local value"); 
                        //return;
                    }

                    characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
                    /*if(!writeCharacteristic.writeCharacteristic(characteristic))
                    { 
                        Log.w(TAG, "Couldn't write characteristic");
                    }*/

                    return true;
                }
                return false;
            }
};
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The following code is write characteristic using byte[] data:

    public boolean writeCharacteristic(){

    //check mBluetoothGatt is available
    if (mBluetoothGatt == null) {
        Log.e(TAG, "lost connection");
        return false;
    }
    BluetoothGattService Service = mBluetoothGatt.getService(your Services);
    if (Service == null) {
        Log.e(TAG, "service not found!");
        return false;
    }
    BluetoothGattCharacteristic charac = Service
            .getCharacteristic(your characteristic);
    if (charac == null) {
        Log.e(TAG, "char not found!");
        return false;
    }

    byte[] value = new byte[1];
    value[0] = (byte) (21 & 0xFF);
    charac.setValue(value);
    boolean status = mBluetoothGatt.writeCharacteristic(charac);
    return status;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...