首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过android应用程序执行BLE无线固件更新

如何通过android应用程序执行BLE无线固件更新
EN

Stack Overflow用户
提问于 2019-03-11 12:40:59
回答 1查看 1.6K关注 0票数 2

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

EN

回答 1

Stack Overflow用户

发布于 2019-03-11 15:48:40

使用从文件读取数据

代码语言:javascript
复制
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方法

代码语言:javascript
复制
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设备。

代码语言:javascript
复制
     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);
    }
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55095318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档