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;
}