2013-10-13 13 views
6

Tiếp theo sampleAndroid 4.3 BLE cách viết đặc trưng

tôi biết:

  • Làm thế nào để đọc giá trị đặc trưng.

Nhưng tôi không biết:

  • Làm thế nào để ghi dữ liệu vào firmware.

Tôi đã thử nhiều lần nhưng không hoạt động.

Đây là mã hóa:

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() Không được gọi

@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

Bất cứ ai có thể giúp tôi? – steven

+0

Chỉ cần một lưu ý nhanh, không chắc chắn nó giải quyết vấn đề của bạn - nhưng mỗi báo cáo lỗi này (https://code.google.com/p/android/issues/detail?id=58979), mã mẫu bạn đang sử dụng không chính xác : kiểm tra bitwise "|" nên là "&". Vì vậy, nếu thuộc tính của bạn không hỗ trợ bất kỳ thuộc tính nào bạn đang kiểm tra thì thuộc tính đó vẫn sẽ xuất hiện. HTH. – jkraybill

Trả lời

3

Tôi nghĩ jkraybill đã có câu trả lời cho bạn.

Tôi đã sử dụng mã mẫu bluetooth le của google, mã này kiểm tra các thuộc tính bit bằng '|' hoạt động như sau:

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

Nhưng khi tôi nhìn vào mã BluetoothGatt, tôi thấy nó sử dụng thao tác '&' thay thế.

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

    ... 
} 

Vì vậy, rõ ràng là mẫu mã không chính xác, nếu bạn kiểm tra sâu hơn về định nghĩa của nó trên Google phát triển doc: https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html

2

Bạn phải xác định giá trị mà bạn muốn viết. bạn có thể tìm thấy những gì bạn cần trong này example

0

Đây là chức năng tôi đã viết để có được để viết thư cho một đặc tính, thô sơ của nó mặc dù nhưng nó hoạt động đặc biệt đối với UUIDs biết:

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

Nó cũng cho phép u nhận được UUID dịch vụ và UUID đặc trưng và thuộc tính và kiểu viết, tôi có thể tiếp tục, nhưng nó khá tẻ nhạt nhưng khá hiệu quả. –

Các vấn đề liên quan