6

フォローしたサンプル

知っている:

  • 特性値の読み方

しかし、私は知りません:

  • ファームウェアにデータを書き込む方法。

何度か試しましたが、うまくいきませんでした。

これはコーディングです:

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);
        }
    }
4

3 に答える 3

2

書き込みたい値を指定する必要があります。この例で必要なものを見つけることができます

于 2015-10-12T10:02:13.290 に答える
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);
                    }
                }
            }
        }
    }
}
于 2016-09-28T08:45:18.813 に答える