Android 4.3 BLE Characteristic 怎么写

标签 android bluetooth bluetooth-lowenergy

已关注 sample

我知道:

  • 如何阅读特征值。

但我不知道:

  • 如何将数据写入固件。

我试了几次,还是不行。

这是编码:

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

最佳答案

我认为 jkraybill 已经为您找到了答案。

我使用的是谷歌的蓝牙文件示例代码,它使用“|”检查属性位操作如下:

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

关于Android 4.3 BLE Characteristic 怎么写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19345803/

相关文章:

c - Linux 示例中的 BLE 桥接模式

asynchronous - Flutter 同步连接到多个 BLE 设备

Android Facebook登录失败无异常,只是 "Sorry something went wrong"

java - Android/Java异步图片下载任务不停运行

java - Android - 如何在蓝牙打印机 (X330) 上打印图像(光栅)?

android - 在设备发现开启时连接蓝牙套接字

ios - 获取 BLE 设备的响应

dart - 将 BLE 写入循环控制点 - 添加电阻

android - 如何加快访问 Android 上的联系人?

java - 从 Android HTTP Post 检索数据