Android OTA蓝牙升级

首先我们需要往项目的build.gradle文件中导入我们空中升级所需要的第三方包:
compile 'no.nordicsemi.android:dfu:1.3.1'
如果是发布版的请添加反编译代码 
-keep class no.nordicsemi.android.dfu.** { *; }

然后 注册     DfuServiceListenerHelper

@Override
protected void onResume() {//取消监听升级回调  
    DfuServiceListenerHelper.registerProgressListener(this, dfuProgressListener);
    super.onResume();
}
//取消注册  
@Override
protected void onPause() {
    super.onPause();
    DfuServiceListenerHelper.unregisterProgressListener(this, dfuProgressListener);
}
// 这个函数是 开始升级的可以根据自己的业务来处理调用
private void startUpgrade(String mac) {

    new DfuServiceInitiator(mac)
            .setDisableNotification(true)
            .setKeepBond(true)
            .setZip(downloadPath)
            .start(PCRepairActivity.this, DfuService.class);
}

 
  
//空中升级时的监听
private final DfuProgressListener dfuProgressListener = new DfuProgressListener() {
    @Override
    public void onDeviceConnecting(String deviceAddress) {
        pbstate.setIndeterminate(true);
    }

    //设备开始连接
    @Override
    public void onDeviceConnected(String deviceAddress) {
        pbstate.setIndeterminate(true);
        tvstate.setText("设备开始连接");
    }

    //升级准备开始的时候调用
    @Override
    public void onDfuProcessStarting(String deviceAddress) {
        pbstate.setIndeterminate(true);
         //到这个函数的时候  mac地址最后一位数会增加1  所以要变成16进制最后一位数增加1  重新连接设备 重新开始升级 
        String newMac = deviceAddress.substring(0, 15) + DigitalTrans.algorismToHEXString(DigitalTrans.hexStringToAlgorism(deviceAddress.substring(15, 17)) + 1);
        startUpgrade(newMac);
    }

    //设备开始升级
    @Override
    public void onDfuProcessStarted(String deviceAddress) {
        pbstate.setIndeterminate(true);
 
 
  
       tvstate.setText("设备开始连接");
} @Override public void onEnablingDfuMode(String deviceAddress) { pbstate.setIndeterminate( true); } //升级过程中的回调 @Override public void onProgressChanged(String deviceAddress, int percent, float speed, float avgSpeed, int currentPart, int partsTotal) { pbstate.setIndeterminate( false); pbstate.setProgress(percent); tvstate.setText("升级过程中"); } //固件验证 @Override public void onFirmwareValidating(String deviceAddress) {
 
  
           tvstate.setText("固件验证");
} //设备正在断开 @Override public void onDeviceDisconnecting(String deviceAddress) {
 
  
 
  
 
  
        tvstate.setText("设备正在断开连接");
} //设备已经断开 @Override public void onDeviceDisconnected(String deviceAddress) { tvstate.setText("设备已经断开"); } //升级完成 @Override public void onDfuCompleted(String deviceAddress) { pbstate.setIndeterminate( false); tvstate.setText("设备升级完成"); CommonUtils. toast(getString(R.string. succeed)); new Handler().postDelayed( new Runnable() { public void run() { openStartActivity(PCMainActivity. class); finish(); } }, 10000); //延迟5秒跳转 } @Override //升级流产 public void onDfuAborted(String deviceAddress) { pbstate.setIndeterminate( false); tvstate.setText("升级流程"); } //升级失败 @Override public void onError(String deviceAddress, int error, int errorType, String message) { Log. e( "onError=", "deviceAddress=" + deviceAddress + " ,error=" + error + ",errorType=" + errorType + ",message=" + message); tvstate.setText("升级失败"); }};
好了 Activity 写完了 下面写服务

public class DfuService extends DfuBaseService {

    @Override
    protected Classextends Activity> getNotificationTarget() {
        return NotificationActivity.class;
    }
}

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 parentIntent = new Intent(this, PCRepairActivity.class);
            parentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            final Intent startAppIntent = new Intent(this, PCRepairActivity.class);
            startAppIntent.putExtras(getIntent().getExtras());
            startActivities(new Intent[]{parentIntent, startAppIntent});
        }
        finish();
    }
}

你可能感兴趣的:(Android OTA蓝牙升级)