【问题标题】:Working with BLE Android 4.3 how to write characteristics?使用 BLE Android 4.3 如何编写特性?
【发布时间】:2013-12-01 08:14:47
【问题描述】:

我正在使用 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;
            }
};

【问题讨论】:

    标签: android bluetooth-lowenergy android-4.3-jelly-bean


    【解决方案1】:

    以下代码是使用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;
    }
    

    【讨论】:

    • 谢谢@Nam Pham 请解释你为什么要这样做 21&0xFF ?
    • 21 & 0xFF。这只是我想写入 BLE 设备的数据。因此,您应该用您的数据替换它。您可以写入的最大数据长度为 20 个字节。如果数据长度> 20字节,则需要将数据分片成20字节的trunk并写入
    • 谢谢,但我问的是你在代码中使用的“&”为什么你在写之前使用“& 0xFF”..
    • @Nomi:这只是我的习惯。你可以使用“value[0] = (byte) 21;” .结果是一样的
    • @chetanmahajan, 请检查您要写入数据的服务和字符 UUID。您可以使用 LightBlue 应用程序检查它们。请确保您的设备上存在服务 UUID 并且角色 UUID 属于它
    【解决方案2】:

    请注意逻辑或:
    原帖中的if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0)" 应该是逻辑 AND 以使权限检查起作用。第二个 charaProp 比较也是如此。否则,无论实际权限标志如何,bot 语句都是正确的。

    【讨论】:

      【解决方案3】:
      public boolean writeCharacteristic(byte value[],int type){
          //check mBluetoothGatt is available
          if (mBluetoothGatt == null) {
              Log.e(TAG, "lost connection");
              return false;
          }
          BluetoothGattService Service = mBluetoothGatt.getService(UUID_SIMPLESERVICE);
          if (Service == null) {
              Log.e(TAG, "service not found!");
              //////////NO service found...........
               return false;
          }
          BluetoothGattCharacteristic charac1 = null;
          boolean status1 = false;
      
          if(type==1) {
              charac1 = Service.getCharacteristic(UUID_PORT1);
              charac1.setValue(value);
              status1 = mBluetoothGatt.writeCharacteristic(charac1);
              Log.v("________BLESERVICE____", "___WRITE CHARATERISTICS STATUS:_________"+status1);
              onReliableWriteCompleted(status1);
          }
          if (charac1 == null) {
              Log.e(TAG, "char not found!");
              return false;
          }
      
          Log.v("___TYPE___","______________________"+type);
          return status1;
      }
      

      【讨论】:

      【解决方案4】:

      以下代码是使用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();
              }
          }
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 2014-12-10
        • 2018-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多