writeCharacteristic to BLE device打开/关闭LED

时间:2015-04-28 18:38:08

标签: java android bluetooth-lowenergy

当我尝试将两个不同的值写入特征时,我遇到了问题。只写了我发送的第一个值。我不太清楚如何实现这一目标。打开LED的值为0x07,关闭LED的值为0x05。

DeviceControlActivity.java:

private final ExpandableListView.OnChildClickListener servicesListClickListner =
    new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                    int childPosition, long id) {
            byte[] write_data = new byte[1];
            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);
                    temp = characteristic.getValue();
                }
                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;
                    }
                    if (temp[0] == 0x05)
                    {
                        write_data[0] = 0x07;
                    }
                    else
                    {
                        write_data[0] = 0x05;
                    }
                    mBluetoothLeService.writeCharacteristic(characteristic, write_data);
                    //characteristic.setValue("testing");
                    //characteristic.setWriteType(BluetoothGattCharacteristic.PERMISSION_WRITE);
                }    

BluetoothLeGatt.java:

public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic, byte[] data) {
    characteristic.setValue(data);

    boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
    return status;
}

0 个答案:

没有答案