【问题标题】:How to perfom BLE over-the-air firmware update through android app如何通过 android 应用程序执行 BLE 无线固件更新
【发布时间】:2019-03-11 04:40:59
【问题描述】:

我正在尝试开发一个 android 应用程序来与 BLE 设备进行通信。我能够从 BLE 设备读取和写入特性。现在我想通过我的应用程序为设备执行 ble OTA 更新。我不知道该怎么办?如果我有 OTA 更新文件,如何在 ble 设备上安装它以及如何执行 OTA 升级?

【问题讨论】:

  • 您尝试对其执行 OTA 更新的远程 BLE 设备是什么?
  • 我正在使用对话半导体
  • @YoussifSaeed 你能告诉我如何使用我的应用更新该设备。
  • 不知道,我希望您使用的是 Nordic 的设备之一,该设备拥有大量的 OTA 源代码和文档。最好的办法是检查 dialog 是否提供他们的 Android 应用程序的源代码。请看这个:support.dialog-semiconductor.com/ota-update-apps-now-available.
  • @YoussifSaeed 好的。我会查的。谢谢

标签: java android bluetooth-lowenergy ota


【解决方案1】:

使用

从文件中读取数据
protected byte[] readFile(File file) {
    BufferedInputStream buf;
    int size = (int) file.length();
    byte[] mDataFile = new byte[size];
    //mDataFile = new byte[size];
    try {
        buf = new BufferedInputStream(new FileInputStream(file));
        buf.read(mDataFile, 0, size);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return mDataFile;
}

然后,开始设备扫描。使用特定的 macAddress 连接设备后 调用方法connectToDevice

public static BluetoothGatt connectToDevice(Context context,
                                   BluetoothDevice device,
                                   BluetoothGattCallback mGattCallBack){
    BluetoothGatt connectedGatt = null;
    if (device != null) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            connectedGatt = device.connectGatt(context, false,
                    mGattCallBack,
                    BluetoothDevice.DEVICE_TYPE_LE,
                    BluetoothDevice.PHY_LE_2M|BluetoothDevice.PHY_LE_1M);
        }else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            connectedGatt = device.connectGatt(context, false, mGattCallBack, BluetoothDevice.DEVICE_TYPE_LE);
        } else if (android.os.Build.VERSION.SDK_INT >= 21) {
            connectedGatt = connectGattApi21(context, device, mGattCallBack);
        } else {
            connectedGatt = device.connectGatt(context, false,
                    mGattCallBack);
        }
    } else {
        //WiSeSdkFileWritter.writeToFile(fileName, TAG + " FAILED could not find remote device/ remote device is null >>" + macAddress);
    }
    return connectedGatt;
}

然后处理BluetoothGattCallback回调方法——onServicesDiscovered,onCharacteristicWrite,onConnectionStateChange

在 onServicesDiscovered 方法中,您将获得 BluetoothGattService。从 gattService 中提取特征。最后将特征写入ble设备。

     BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        super.onConnectionStateChange(gatt, status, newState);
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);

    BluetoothGattService mService = gatt.getService(service);

        mCharacteristics = mService.getCharacteristic(characteristics);
        mCharacteristics.setValue(bytes);


        Logger.v(TAG, "data write count  ::" + writeCounter++);

        Timer t = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                gatt.writeCharacteristic(mCharacteristics);
            }
        }; 
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicRead(gatt, characteristic, status);
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        super.onCharacteristicChanged(gatt, characteristic);
    }
};

【讨论】:

  • 我已经做了ble特性读写但是不知道怎么用OTA升级
  • 我只是从设备中读取特性和服务,例如电池服务。但现在我想要OTA更新。但我不知道如何在我的代码中做到这一点
猜你喜欢
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多