2.0蓝牙设备的固件升级

版权声明:转载请声明 https://blog.csdn.net/qq_34457684/article/details/83819818

首先针对2.0的蓝牙设备对于数据的传送很有局限,每次只能传输20个字节,如果传输大数据还会特别不稳定,所以关于固件升级,能不通过蓝牙升级就不要通过蓝牙升级,但是有时候必须实现这种需求,就没办法,一定得入坑去研究了,最近在做的一个项目,设备需要两个空中升级的功能,一个是给CPU芯片软件升级,一个是给蓝牙芯片软件升级,但是升级方式是相同的,下面是一些示例代码:
首先还是得读懂文档看明白如何通信

1有关蓝牙的配对连接请参考蓝牙配对githubDemo 这个库对于蓝牙连接配对描述的很详细,也很好用。首先是蓝牙连接:

private void connectDevice() {

BleConnectOptions options =new BleConnectOptions.Builder().setConnectRetry(3).setConnectTimeout(10000).setServiceDiscoverRetry(3)
.setServiceDiscoverTimeout(10000).build();
  ClientManager.getClient().connect(mBluetoothDevice.getAddress(), options, new BleConnectResponse() {
@Override
   public void onResponse(int code, BleGattProfile profile) {
		   hideConnectDialog();
        if (code ==REQUEST_SUCCESS) {
			mBluetoothDetailInfo =null;
            //mCustomToast.ShowToast(R.string.connect_success);
            if(mHandler !=null && profile !=null){
			  Listservices = profile.getServices();
              for (BleGattService service :services) {
		         if(Constant.BULETOOTH_SERVICE_UUID.equalsIgnoreCase(service.getUUID().toString())) {
			        Listcharacters =service.getCharacters();
                    for (BleGattCharacter character :characters) {
		            if(Constant.BULETOOTH_CHARACTER_UUID.equalsIgnoreCase(character.getUuid().toString()))							{
						  mBluetoothDetailInfo =new BluetoothDetailInfo();
                          mBluetoothDetailInfo.setBlueName(mBluetoothDevice.getName());
                          mBluetoothDetailInfo.setBlueMac(mBluetoothDevice.getAddress());
                          mBluetoothDetailInfo.setType(BluetoothDetailInfo.TYPE_CHARACTER);
                          mBluetoothDetailInfo.setCharacterUuid(character.getUuid());
                          mBluetoothDetailInfo.setServiceUuid(service.getUUID());
                          break;
                        }
		}
			break;
                  }
		}
			mHandler.sendEmptyMessage(ENTER_SATELLITE_ACTIVITY);
            }
		}else{
			mCustomToast.ShowToast(R.string.connect_failed);
       }
}
});
}

2,连接成功之后开始升级,
第一步:获取bin文件的长度,从服务器下载下来一般都存在sd卡中

public static int getCpuBinLength(String name) {
	int length = 0;
  try {
	  File file = new File(Constant.FILE_PATH,name);
      FileInputStream is = new FileInputStream(file);
      length = is.available();
      is.close();
  }catch (IOException e) {
	e.printStackTrace();
  }
	return length;
}

第二步,开始组装数据,我的项目通信协议是吧数据转成byte进行传输,
//下面方法是读取指定长度的byte数据

public static byte[]getBleBinToByte(String name, long start, int len,int allLength ) {

try {

File file =new File(Constant.FILE_PATH,name);

      if (file.exists()&&len>0){

//拿出bin文件

FileInputStream inputStream =new FileInputStream(file);

//读到指定长度

        long curPos =inputStream.skip(start);

//判断最后一个数据是否满足指定的len,不够的话不用0补齐,剩下多少使用多少

        if ((curPos+len)>allLength){

len= (int) (allLength-curPos);

        }

byte[]bytes =new byte[len];

//从流中独处文件,转成byte

        inputStream.read(bytes);

        inputStream.close();

        if (start ==curPos)

{

return bytes;

        }

}

}catch (IOException e) {

e.printStackTrace();

  }

return null;

}

第三步:拿出指定长度的bin文件并转化成byte数组之后就可以进行发送了,在发送过程中对传输失败,以及超时未返回都要做相应的重发处理,但是不能无限制的重发,一般重发十次就可以了。

自己的一点小经验,谢谢大家浏览,欢迎指教

猜你喜欢

转载自blog.csdn.net/qq_34457684/article/details/83819818