蓝牙功能模块(实现了读取地址和温度的功能,附实现CRC校验代码)

BleActivity中的数据显示代码修改:
从而实现接收的数据
实现读取温度和读取地址的功能
其他功能也可以在此实现

/**
     * @Title: displayData
     * @Description: TODO(接收到的数据在TextView上显示)
     * @param @param rev_string(接受的数据)
     * @return void
     * @throws
     */
    private void displayData(final String rev_string)
    {
        rev_str = rev_string;//接收的数据
        rev_tv.setText("接收:"+rev_str+"\r\n");
        System.out.println("rev:" + rev_str);
        runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {

                switch (sendType){
                    case 0://读地址
                    {
                        byte[] buff = SendRevInfo.hexStrToByteArray(rev_str);
                        String straddr = "";
                        if (buff.length == 8) {
                            straddr = Integer.toString(buff[5] & 0xFF);
                            if (straddr.length() == 1) {
                                straddr = "0" + straddr;
                            }
                            straddr="设备地址-"+straddr;
                        }
                        Message msg = new Message();
                        msg.what = 2;
                        Bundle ble = new Bundle();
                        ble.putString("revdata", straddr);
                        msg.setData(ble);
                        myComHandler.sendMessage(msg);
                        break;
                    }
                    case 1: //读温度
                    {
                        byte[] buff = SendRevInfo.hexStrToByteArray(rev_str);
                        String straddr = "";
                        if (buff.length == 8) {
                            short itemp=(short)((buff[4]& 0xff)<<8 | (buff[5] & 0xff));
                            DecimalFormat df=new DecimalFormat("###.##");
                            straddr=df.format(itemp);
                            straddr="温度-"+straddr;
                        }
                        Message msg = new Message();
                        msg.what = 2;
                        Bundle ble = new Bundle();
                        ble.putString("revdata", straddr);
                        msg.setData(ble);
                        myComHandler.sendMessage(msg);
                        break;
                    }
                    default:
                    {

                    }

                }
            }
        });

    }
    public void readDevInfo(int sendtype){
        sendType=sendtype;
        byte[] buff;
        switch(sendtype){
            case 0://读地址
                buff = SendRevInfo.ReadDataFun((byte)0x01,(byte)0x03,
                        0x01,0x01).getBytes();
                break;
            case 1://读温度
                buff = SendRevInfo.ReadDataFun((byte)0x01,(byte)0x03,
                        0x02,0x01).getBytes();
                break;

            default:
                buff = SendRevInfo.ReadDataFun((byte)0x01,(byte)0x03,
                        0x01,0x01).getBytes();
        }
        int len = buff.length;
        int[] lens = dataSeparate(len);
        for(int i =0;i<lens[0];i++)
        {
            String strsend = new String(buff, 20*i, 20);
            Message msg=new Message();
            msg.what=1;
            Bundle ble=new Bundle();
            ble.putString("senddata",strsend);
            msg.setData(ble);
            myComHandler.sendMessage(msg);
            target_chara.setValue(strsend);//只能一次发送20字节,所以这里要分包发送
            //调用蓝牙服务的写特征值方法实现发送数据
            mBluetoothLeService.writeCharacteristic(target_chara);
        }
        if(lens[1]!=0)
        {
            String strsend = new String(buff, 20*lens[0], lens[1]);
            Message msg=new Message();
            msg.what=1;
            Bundle ble=new Bundle();
            ble.putString("senddata",strsend);
            msg.setData(ble);
            myComHandler.sendMessage(msg);
            target_chara.setValue(strsend);
            //调用蓝牙服务的写特征值方法实现发送数据
            mBluetoothLeService.writeCharacteristic(target_chara);

        }
    }

    @SuppressLint("HandlerLeak")
    private Handler myComHandler = new Handler()
    {
        // 2.重写消息处理函数
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 1:
                {
                    String sendData = msg.getData().getString("senddata");
                    sendData="发送:"+sendData;
                    sendData+="\r\n";
                    String str=rev_tv.getText().toString();
                    str+=sendData;
                    rev_tv.setText(str);
                    break;
                }
                case 2:
                    String sendData = msg.getData().getString("revdata");
                    sendData="接收:"+sendData;
                    sendData+="\r\n";
                    String str=rev_tv.getText().toString();
                    str+=sendData;
                    rev_tv.setText(str);
                    break;

            }
            super.handleMessage(msg);
        }

    };
/*
     * 发送按键的响应事件,主要发送文本框的数据
     */
    @Override
    public void onClick(View v)
    {


        readDevInfo(0);


    }

在bluetoothserver.java中
注释掉最后一行(目的是不让发送完数据之后,发送的数据再次显示到输出界面上)

 /*
         * 特征值的写
         * */
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt,
                                          BluetoothGattCharacteristic characteristic, int status)
        {

            Log.w(TAG, "--onCharacteristicWrite--: " + status);
            // 以下语句实现 发送完数据或也显示到界面上
            //broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }

补充说明:十六进制字符串转换字节数组
CRC校验码的实现

/******************
	 * 十六进制字符串转字节数组
	 * @param str
	 * @return
	 */
	public static byte[] hexStrToByteArray(String str)
	{
	    if (str == null) {
	        return null;
	    }
	    if (str.length() == 0) {
	        return new byte[0];
	    }
	    byte[] byteArray = new byte[str.length() / 2];
	    for (int i = 0; i < byteArray.length; i++){
	        String subStr = str.substring(2 * i, 2 * i + 2);
	        byteArray[i] = ((byte)Integer.parseInt(subStr, 16));
	    }
	    return byteArray;
	}

	/**********************
	 * CRC校验
	 * @param data
	 * @return
	 */
	public static  int crc16(byte[] data)
    {
        int crc = 0xFFFF;
        int length = data.length;
        int start = 0;
        while (length-- > 0)
        {
            crc^= data[start++];
            for (int i = 0; i < 8; i++)
            {
                if ((crc & 0x0001) == 1)
                {
                    crc >>= 1;
                    crc ^= 0xA001;
                }
                else
                {
                    crc >>= 1;
                }
            }
        }
        return crc;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值