Android BLE dfu升级
dfu升级适用于nordic nRF51 nRF52 的系统,github上提供了相关升级的库https://github.com/NordicSemiconductor/Android-DFU-Library 支持 Android 4.3+
gradle配置如下:
implementation 'no.nordicsemi.android:dfu:1.8.1'
在使用前请先配置好BLE的相关权限以及动态权限 读写权限 定位等可以使用EasyPermission配置
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
第一步 我们创建一个service集成DfuBaseService
public class DfuService extends DfuBaseService {
    public DfuService() {
    }
    @Override
    protected Class<? extends Activity> getNotificationTarget() {
        return NotificationActivity.class;
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
第二步 创建一个NotificationActivity
public class NotificationActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If this activity is the root activity of the task, the app is not running
        if (isTaskRoot()) {
            // Start the app before finishing
            final Intent intent = new Intent(this, UpdateZipActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtras(getIntent().getExtras()); // copy all extras
            startActivity(intent);
        }
        // Now finish, which will drop you to the activity at which you were at the top of the task stack
        finish();
    }
}
其中UpdateZipActivity即为处理升级业务的界面
在这里要根据你的业务需求来,可以把升级包打包在apk里,也可以做成下载后升级的情况
如果是打包在apk里可以按下面做,如果你是下载的情况只需要改starter.setZip(uri, path); 传入文件uri和路径即可
private DfuServiceInitiator starter; 
starter = new DfuServiceInitiator(mac).setDeviceName("Smart Lock").setKeepBond(false).setPacketsReceiptNotificationsEnabled(true).setPacketsReceiptNotificationsValue(10);
        // If you want to have experimental buttonless DFU feature supported call additionally:
        starter.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true);
        starter.setZip(R.raw.update24); //setDeviceName为设备过滤条件 setzip传入raw文件的路径即可
@Override
protected void onResume() {
DfuServiceListenerHelper.registerProgressListener(this, mDfuProgressListener); //监听升级进度
starter.start(this, DfuService.class); //启动升级服务
}
/**
* dfu升级监听
*/
private final DfuProgressListener mDfuProgressListener = new DfuProgressListener() {
@Override
public void onDeviceConnecting(String deviceAddress) {
Log.i("dfu", "onDeviceConnecting");
proBar.setIndeterminate(true);
airUpgradeTv.setText(R.string.dfu_status_connecting);
} @Override
public void onDeviceConnected(String deviceAddress) { } @Override
public void onDfuProcessStarting(String deviceAddress) {
proBar.setIndeterminate(true);
airUpgradeTv.setText(R.string.dfu_status_starting);
} @Override
public void onDfuProcessStarted(String deviceAddress) {
Log.i("dfu", "onDfuProcessStarted");
} @Override
public void onEnablingDfuMode(String deviceAddress) {
Log.i("dfu", "onEnablingDfuMode");
proBar.setIndeterminate(true);
airUpgradeTv.setText(R.string.dfu_status_switching_to_dfu);
} @Override
public void onProgressChanged(String deviceAddress, int percent, float speed, float avgSpeed, int currentPart, int partsTotal) {
Log.i("dfu", "onProgressChanged");
Log.i("dfu", "onProgressChanged" + percent);
proBar.setProgress(percent);
proBar.setIndeterminate(false);
airUpgradeTv.setText(percent + "%");
} @Override
public void onFirmwareValidating(String deviceAddress) {
Log.i("dfu", "onFirmwareValidating");
proBar.setIndeterminate(true);
airUpgradeTv.setText(R.string.dfu_status_validating);
} @Override
public void onDeviceDisconnecting(String deviceAddress) {
Log.i("dfu", "onDeviceDisconnecting");
} @Override
public void onDeviceDisconnected(String deviceAddress) {
Log.i("dfu", "onDeviceDisconnected");
proBar.setIndeterminate(true);
airUpgradeTv.setText(R.string.dfu_status_disconnecting); } @Override
public void onDfuCompleted(String deviceAddress) { airUpgradeTv.setText(R.string.dfu_status_completed);
proBar.setProgress(100);
proBar.setIndeterminate(false);
//升级成功
new Handler().postDelayed(new Runnable() {
@Override
public void run() { // if this activity is still open and upload process was completed, cancel the notification
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(DfuService.NOTIFICATION_ID); }
},200); } @Override
public void onDfuAborted(String deviceAddress) {
Log.i("dfu", "onDfuAborted");
//升级流产,失败
airUpgradeTv.setText(R.string.dfu_status_aborted);
// let's wait a bit until we cancel the notification. When canceled immediately it will be recreated by service again.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(UpdateZipActivity.this, "升级出错", Toast.LENGTH_SHORT).show();
// if this activity is still open and upload process was completed, cancel the notification
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(DfuService.NOTIFICATION_ID);
}
}, 200); } @Override
public void onError(String deviceAddress, int error, int errorType, String message) {
Log.i("dfu", "onError"); airUpgradeTv.setText(getString(R.string.dfu_status_aborted));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// if this activity is still open and upload process was completed, cancel the notification
final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(DfuService.NOTIFICATION_ID); }
}, 200); }
@Override
protected void onPause() {
//取消监听
DfuServiceListenerHelper.unregisterProgressListener(this, mDfuProgressListener);
super.onPause();
}
Android BLE dfu升级的更多相关文章
- BLE空中升级 谈(二)
		
BLE 空中升级谈 -- CC2541 的产品开发中OAD注意事项(续) TI CC2541支持多个硬件,多个软件对它进行空中升级,可以有不同的组合,硬件有 编号 名称 Hex 用法 1 Cc2540 ...
 - Android BLE蓝牙详细解读
		
代码地址如下:http://www.demodashi.com/demo/15062.html 随着物联网时代的到来,越来越多的智能硬件设备开始流行起来,比如智能手环.心率检测仪.以及各式各样的智能家 ...
 - nrf52——DFU升级USB/UART升级方式详解(基于SDK开发例程)
		
摘要:在前面的nrf52--DFU升级OTA升级方式详解(基于SDK开发例程)一文中我测试了基于蓝牙的OTA,本文将开始基于UART和USB(USB_CDC_)进行升级测试. 整体升级流程: 整个过程 ...
 - nrf52——DFU升级OTA升级方式详解(基于SDK开发例程)
		
在我们开始前,默认你已经安装好了一些基础工具,如nrfutil,如果你没有安装过请根据官方中文博客去安装好这些基础工具,连接如下:Nordic nRF5 SDK开发环境搭建(nRF51/nRF52芯片 ...
 - Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
		
http://blog.csdn.net/wave_1102/article/details/39271693 分类: Android(105) 作者同类文章X Android4.3 规范了BLE的A ...
 - 蓝牙4.0——Android BLE开发官方文档翻译
		
ble4.0开发整理资料_百度文库 http://wenku.baidu.com/link?url=ZYix8_obOT37JUQyFv-t9Y0Sv7SPCIfmc5QwjW-aifxA8WJ4iW ...
 - android开发 更新升级安装到一半自动闪退
		
如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /** * 安装APK文件 */ private void installApk( ...
 - 【转】蓝牙4.0——Android BLE开发官方文档翻译
		
原文网址:http://ricardoli.com/2014/07/31/%E8%93%9D%E7%89%9940%E2%80%94%E2%80%94android-ble%E5%BC%80%E5%8 ...
 - Android ble 蓝牙4.0 总结
		
本文介绍Android ble 蓝牙4.0,也就是说API level >= 18,且支持蓝牙4.0的手机才可以使用,如果手机系统版本API level < 18,也是用不了蓝牙4.0的哦 ...
 
随机推荐
- 初识ldap
			
什么是LDAP? LDAP的英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的, 但是简单多了并且可以根据需要定制.与 ...
 - Mongoose 'static' methods vs. 'instance' methods
			
statics are the methods defined on the Model. methods are defined on the document (instance). We may ...
 - [luogu P3953] [noip2017 d1t3] 逛公园
			
[luogu P3953] [noip2017 d1t3] 逛公园 题目描述 策策同学特别喜欢逛公园.公园可以看成一张$N$个点$M$条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,$N ...
 - ZedBoard前期准备工作
			
1. 资源下载 内核:https://github.com/Xilinx/linux-xlnx/releases uboot:https://github.com/Xilinx/u-boot-xlnx ...
 - 【转】Binlog 基本操作
			
MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日志是事务安全型的. 一般 ...
 - 略解ByteBuf
			
说到ByteBuf,我们并不陌生,官网给的解释为,一个可以进行随机访问或者是顺序访问的字节集合,它是NIO buffers缓冲的底层抽象.既然是底层抽象,那么我们就可以基于其衍生出很多的具体实现出来, ...
 - oracle数据库自学笔记(持续更新中……)
			
以前的项目都是使用mysql数据库开发的,如今进了新的公司,开始接触到了Oracle数据库,而自己以前没有接触过,就自己挤时间来学习一下. 一.关系型数据库的概念 关系型数据理论由E.F.Codd博士 ...
 - Saiku数据库迁移后的刷新脚本-Shell脚本读取数据库中的数据(二十三)
			
Saiku数据库迁移后的刷新脚本 之前有谈过对saiku中的数据进行刷新,因为saiku默认会从缓存中查询数据,但是配置不使用缓存又会效率低下... 所以这里就需要做一个数据刷新,每次ETL之后都需要 ...
 - [Linux] Extend space of root disk in Linux7
			
[root@node1 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 26G 17G 9.8G ...
 - 浅析Hashmap和Hashtable
			
一.Hashmap不是线程安全的,而Hashtable是线程安全的 通过查看源码可以发现,hashmap类中的方法无synchronized关键字,而hashtable类中的方法有synchroniz ...