make Advertise Data via bluetooth - Android Bluetooth

Android examples for Bluetooth:Bluetooth Data Transfer

Description

make Advertise Data via bluetooth

Demo Code


//package com.book2s;

import android.bluetooth.le.AdvertiseData;

import android.os.ParcelUuid;

public class Main {
    public static AdvertiseData makeAdvertiseData(String message) {
        while (message.length() < 9) {
            message += " ";
        }//from  w  w  w . ja  va2s  .com
        byte[] data = message.substring(2).getBytes();
        ParcelUuid pu = ParcelUuid.fromString("0000"
                + asHex(message.substring(0, 2).getBytes())
                + "-0000-1000-8000-00805F9B34FB");
        AdvertiseData.Builder builder = new AdvertiseData.Builder();
        builder.addServiceData(pu, data);
        builder.addServiceUuid(pu);

        return builder.build();
    }

    public static String asHex(byte bytes[]) {
        if ((bytes == null) || (bytes.length == 0)) {
            return "";
        }

        StringBuffer sb = new StringBuffer(bytes.length * 2);

        for (int index = 0; index < bytes.length; index++) {
            int bt = bytes[index] & 0xff;

            if (bt < 0x10) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(bt).toUpperCase());
        }
        return sb.toString();
    }
}

Related Tutorials