关于2.0蓝牙设备的空中升级

关于2.0蓝牙设备的空中升级

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

首先还是得读懂文档看明白如何通信

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

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数组之后就可以进行发送了,在发送过程中对传输失败,以及超时未返回都要做相应的重发处理,但是不能无限制的重发,一般重发十次就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值