使用BLE Android 4.3如何编写特性?

Nom*_*uks 19 android bluetooth-lowenergy android-4.3-jelly-bean

我正在使用Android 4.3 API开发BLE项目(Android应用程序),我使用了样本BLE应用程序,它只是在DeviceControlActivity.activity中读取特性,但我想编写特征并在点击按钮时将其发送到BLE芯片.如何在芯片cc2540上写入数据..基本上我不知道逐步编写特性的程序.

写我只能在DeviceControlActivity中看到以下代码片段的设备名称和ID

 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;
            }
};
Run Code Online (Sandbox Code Playgroud)

Nam*_*ham 39

以下代码是使用byte[]数据写入特征:

    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;
}
Run Code Online (Sandbox Code Playgroud)

  • 21 和 0xFF。这只是我想写入 BLE 设备的数据。因此,您应该用您的数据替换它。您可以写入的最大数据长度为 20 个字节。如果数据长度>20字节,则需要将数据分片成20字节主干并写入 (2认同)

小智 7

请注意,
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0)原始帖子中的逻辑OR 应该是一个逻辑AND,用于权限检查.第二个charaProp比较相同.否则,无论实际权限标志如何,bot语句都是真的.


Huy*_*wer 6

以下代码是使用格式的string数据的写入特征utf-8

public void writeCharacteristic(BluetoothGattCharacteristic characteristic,
            String data) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }

        Log.i(TAG, "characteristic " + characteristic.toString());
        try {
            Log.i(TAG, "data " + URLEncoder.encode(data, "utf-8"));

            characteristic.setValue(URLEncoder.encode(data, "utf-8"));

            // TODO
            mBluetoothGatt.writeCharacteristic(characteristic);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!