BLE gatt从机端实现(Android端)

本文详细介绍了在Android平台上实现BLE(Bluetooth Low Energy)从机端的具体步骤,重点讲解了数据格式转换的工具类及其作用。同时,提到了在实现过程中必须添加的相关权限设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先说工具类吧,这个工具类主要功能是数据格式转换--------------------------------------

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从机接收数据 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值