2013-10-13 55 views
6

其次sampleAndroid 4.3的BLE如何写特性

我知道:

  • 如何阅读特征值。

但我不知道:

  • 如何将数据写入固件。

我试了好几次,但没有奏效。

这是编码:

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) { 
       System.out.println("read!!!!!!"); 
       // 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); 
      } 
      if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { 
       System.out.println("notify!!!!!!"); 
       mNotifyCharacteristic = characteristic; 
       mBluetoothLeService.setCharacteristicNotification(
         characteristic, true); 
      } 
      if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) { 
       if (SampleGattAttributes.AppConfigToBongCharacteristicUUID 
         .equals(characteristic.getUuid())) { 
        System.out.println("write!!!!!!"); 
        mBluetoothLeService.writeCharacteristic(characteristic); 
       } 
      } 



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

     Calendar date = Calendar.getInstance(); 
     StringBuilder data = new StringBuilder(); 
     String data_date_y = String.format("%4s", date.get(Calendar.YEAR)) 
       .replace(' ', '0'); 
     String data_date_m = String.format("%2s", date.get(Calendar.MONTH)) 
       .replace(' ', '0'); 
     String data_date_d = String.format("%2s", date.get(Calendar.DATE)) 
       .replace(' ', '0'); 
     String data_date_h = String.format("%2s", date.get(Calendar.HOUR)) 
       .replace(' ', '0'); 
     String data_date_min = String.format("%2s", 
       date.get(Calendar.MINUTE)).replace(' ', '0'); 

     data.append("10FFFF"); 
     data.append(data_date_y); 
     data.append(data_date_m); 
     data.append(data_date_d); 
     data.append(data_date_h); 
     data.append(data_date_min); 
     System.out.println(data); 
     byte[] dataByte = data.toString().getBytes(); 

     characteristic.setValue(dataByte); 
     mBluetoothGatt.writeCharacteristic(characteristic); 
    } 

} 

// onCharacteristicWrite()不会被调用

@Override 
    public void onCharacteristicWrite(BluetoothGatt gatt, 
      BluetoothGattCharacteristic characteristic, int status) { 
     System.out.println("writeCharacteristic111"); 
     System.out.println("status:" + status); 
     System.out.println(BluetoothGatt.GATT_SUCCESS); 
     if (status == BluetoothGatt.GATT_SUCCESS) { 
      broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); 
     } 
    } 
+0

任何人都可以帮我吗? – steven

+0

只是一个快速的笔记,不知道它解决您的问题 - 但每次这个错误报告(https://code.google.com/p/android/issues/detail?id=58979),您使用的示例代码是不正确:按位检查“|”应该 ”&”。因此,如果你的属性不支持你正在检查的任何属性,它仍然会出现它。 HTH。 – jkraybill

回答

3

我觉得jkraybill已经得到了你答案。

我使用谷歌的蓝牙LE示例代码,检查性能随位“|”操作如下:

if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) { 
    ... 
} 

但是,当我看着BluetoothGatt代码本身,我发现它使用“&”操作,而不是。

public boolean writeCharacteristic(BluetoothGattCharacteristic characteristic) { 
    if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 
     && (characteristic.getProperties() & 
      BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) return false; 

    ... 
} 

如此明显的示例代码是不正确的,如果进一步就其定义在谷歌检查开发文档: https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html

2

您必须指定要写入值。 你可以找到你在这个example

0

这需要的是我写的,以获得函数写的特征是什么,它的基本的,虽然,但它的作品尤其是对未知的UUID:

public void getACharXandWriteToIt(int value, ScanResult result) { 
    Log.i(TAG, "This is result:: " + result.toString()); 
    List<ParcelUuid> serviceUUIDs = result.getScanRecord().getServiceUuids(); 
    ArrayList<BluetoothGattService> services = new ArrayList<>(); 
    List<BluetoothGattCharacteristic> characteristics; 

    ArrayList<UUID> uuidServiceList = new ArrayList<>(); 
    for (ParcelUuid pUUIDx : serviceUUIDs){ 
     uuidServiceList.add(pUUIDx.getUuid()); 
     Log.i(TAG, uuidServiceList.toString()); 
    } 
    for (UUID serviceUUIDx: uuidServiceList){ 
     Log.i(TAG, "services are: "+ serviceUUIDx.toString()); 
     MyLogData +="services are: "+ serviceUUIDx.toString()+"\n"; 
     BluetoothGattService service = bleGatt.getService(serviceUUIDx); 
     if (service!=null) { 
      characteristics = service.getCharacteristics(); 
      services.add(service); 
      for (BluetoothGattCharacteristic characteristic : characteristics) { 
       if(characteristic!=null){ 
        Log.i(TAG, "CharX:: " + characteristic.toString()); 
        MyLogData += "CharX:: " + characteristic.toString()+"\n"; 
        Log.i(TAG, "Charx UUID:: " + characteristic.getUuid().toString()); 
        MyLogData += "Charx UUID:: " + characteristic.getUuid().toString(); 
         Log.i(TAG, "Charx write type:: " + characteristic.getProperties()); 
        /* Log.i(TAG, "Write no response:: " + 
          BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE); 
        Log.i(TAG, "Write:: " + 
          BluetoothGattCharacteristic.PROPERTY_WRITE); 
        Log.i(TAG, "Notify:: " + 
          BluetoothGattCharacteristic.PROPERTY_NOTIFY); 
        Log.i(TAG, "Read:: " + 
          BluetoothGattCharacteristic.PROPERTY_READ);*/ 
        if (characteristic.getWriteType() == 1){ 
         MyLogData += "Write data: " + value + "\n"; 
         characteristic.setValue(value,BluetoothGattCharacteristic.FORMAT_SINT8, 0); 
         writeCharacteristic(characteristic); 
        } 
       } 
      } 
     } 
    } 
} 
+0

它还允许你得到服务UUID和特征UUID和属性和写入类型,我可以继续下去,它很单调但很高效。 –