Nordic 硬件升级很乱,我整理了一下,把关于Nordic 的文件整理成jar包,可直接调用就好。
在安卓app里,新建java文件DfuService如下:
public class DfuService extends DfuBaseService {
protected Class<? extends Activity> getNotificationTarget() {
// TODO Auto-generated method stub
return NotificationActivity.class;
}
}
好了,就可用了。
在要升级的界面onresum里:
protected void onResume() {
super.onResume();
DfuServiceListenerHelper.registerProgressListener(this,
mDfuProgressListener);
}
下面就将如何调用了,这里
void startOTA(String mac) {
final Intent service = new Intent(this, DfuService.class);
service.putExtra(DfuService.EXTRA_DEVICE_ADDRESS, mac);//硬件mac地址
service.putExtra(DfuService.EXTRA_DEVICE_NAME,
application.swDevicesList.get(0).getDeviceName());//硬件的名字
service.putExtra(DfuService.EXTRA_FILE_MIME_TYPE,
mFileType == DfuService.TYPE_AUTO ? DfuService.MIME_TYPE_ZIP
: DfuService.MIME_TYPE_OCTET_STREAM);//下载文件的类型
service.putExtra(DfuService.EXTRA_FILE_TYPE, mFileType);// 下载文件的类型
service.putExtra(DfuService.EXTRA_FILE_PATH, mFilePath);// 下载文件的本地路径
service.putExtra(DfuService.EXTRA_KEEP_BOND, mKeepBond);//是否要绑定
startService(service);
}
只要把对应的参数填进去就好,就开始升级了,升级回调为:
private final DfuProgressListener mDfuProgressListener = new DfuProgressListenerAdapter() {
public void onDeviceConnecting(final String deviceAddress) {
}
public void onDfuProcessStarting(final String deviceAddress) {
}
public void onEnablingDfuMode(final String deviceAddress) {
}
@Override
public void onFirmwareValidating(final String deviceAddress) {
}
@Override
public void onDeviceDisconnecting(final String deviceAddress) {
}
@Override
public void onDfuCompleted(final String deviceAddress) {
//升级完成接口
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(final String deviceAddress) {
//升级失败接口
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 onProgressChanged(final String deviceAddress,
final int percent, final float speed, final float avgSpeed,
final int currentPart, final int partsTotal) {
//升级进度 percent:百分比 如:46就代表46%
}
@Override
public void onError(final String deviceAddress, final int error,
final int errorType, final String message) {
//升级失败接口
// We have to wait a bit before canceling notification. This is
// called before DfuService creates the last notification.
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);
};
建了个交流群:416157653,欢迎大家加入讨论