先说工具类吧,这个工具类主要功能是数据格式转换--------------------------------------
package com.wsh.ble.bleserver.bleserver; /** * Created by sheng-hui.wang on 2015/12/15. */ public class StringUtil { static final String HEX_FORMAT = "%02x";// 以十六进制输出,2为指定的输出字段的宽度.如果位数小于2,则左端补0 /** * 字节数组转换为16进制字符串 * * @param b * @return */ public static String bytesToHexStr(byte b[]) { return bytesToHexStr(b, 0, b.length); } /** * 字节数组转换为16进制字符串 * * @param b * @param start * @param len * @return */ public static String bytesToHexStr(byte b[], int start, int len) { StringBuffer str = new StringBuffer(); for (int i = start; i < start + len; i++) { str.append(String.format(HEX_FORMAT, b[i])); } return str.toString(); } /** * 将十六进制字符串转换为字节数组 * * @param str * @return */ public static byte[] hexStrToBytes(String str) { if (str.length() % 2 != 0) { str = "0" + str; } byte[] temp = new byte[str.length() / 2]; for (int i = 0; i < str.length(); i = i + 2) { temp[i / 2] = (byte) (Byte.parseByte(str.substring(i, i + 1), 16) * 16 + Byte .parseByte(str.substring(i + 1, i + 2), 16)); } return temp; } /** * 将十六进制字符串转换为字节数组,并写入已有数组 * * @param str * @param b * @param from */ public static void hexStrToBytes(String str, byte[] b, int from) { if (str.length() % 2 != 0) { str = "0" + str; } hexStrToBytes(str, b, from, str.length() / 2); } /** * 将十六进制字符串转换为字节数组 * * @param str * @return */ public static void hexStrToBytes(String str, byte[] b, int from, int length) { if (str.length() % 2 != 0) { str = "0" + str; } for (int i = 0; i < Math.min(str.length(), length * 2) && (from + i / 2) < b.length; i = i + 2) { b[from + i / 2] = (byte) (Byte.parseByte(str.substring(i, i + 1), 16) * 16 + Byte.parseByte(str.substring(i + 1, i + 2), 16)); } } /** * 将hexStr转换成逆向的byte数组,特地为南开系统使用 * * @param str * @return */ public static byte[] hexStrToInverseBytes(String str) { if (str.length() % 2 != 0) { str = "0" + str; } byte[] temp = new byte[str.length() / 2]; for (int i = 0; i < str.length(); i = i + 2) { temp[temp.length - 1 - i / 2] = (byte) (Byte.parseByte( str.substring(i, i + 1), 16) * 16 + Byte.parseByte( str.substring(i + 1, i + 2), 16)); } return temp; } /** * 将逆向的byte数组转换成hexStr,特地为南开系统使用 * * @param str * @return */ public static String inverseBytesToHexStr(byte b[], int start, int len) { StringBuffer str = new StringBuffer(); for (int i = start + len - 1; i >= start; i--) { str.append(String.format(HEX_FORMAT, b[i])); } return str.toString(); } /** * 根据条件修剪String * * @param text * 原始数据 * @param length * 需要的长度 * @param ch * 添加的字符 * @param end * true在末尾,false在头部 * @return 符合要求的String * * 20050301 update by guty 汉字算作长度为2 */ public static String lengthFix(String text, int length, char ch, boolean end) { if (text == null) text = ""; int tempLength = text.getBytes().length; if (length == tempLength) return text; if (length > tempLength) { char[] fix = new char[length - tempLength]; for (int i = 0; i < fix.length; i++) { fix[i] = ch; } StringBuffer buffer = new StringBuffer(text); if (end) { buffer = buffer.append(fix); } else { buffer = buffer.insert(0, fix); } return buffer.toString(); } else { if (end) return new String(text.getBytes(), 0, length); else return new String(text.getBytes(), tempLength - length, length); } } /** * 将字符串转换为固定长度 * * @param text * @param length * @param ch * 用来补长度的char * @param end * 是在结尾补还是在开头补 * @return */ public static String lengthFix(String text, int length, String ch, boolean end) { return lengthFix(text, length, ch.charAt(0), end); } }这个类是界面类------------------------------------------------------------------------------------------------
package com.wsh.ble.bleserver.bleserver;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ParcelUuid;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.UUID;
import static android.bluetooth.le.AdvertiseData.Builder;
/**
* BLE从机接收数据