Last active
January 31, 2021 06:55
-
-
Save chyiiiiiiiiiiii/8e69574a6f7dd49cb1569eca755428c9 to your computer and use it in GitHub Desktop.
Android - Broadcast as iBeacon - 原生
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void startAdvertise(String uuid, int major, int minor) { | |
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser(); | |
AdvertiseSettings settings=createAdvertiseSettings(1000); | |
AdvertiseData data = createAdvertiseData( uuid , major, minor, -59); | |
// | |
advertiser.startAdvertising(settings, data, new AdvertiseCallback() { | |
@Override | |
public void onStartSuccess(AdvertiseSettings settingsInEffect) { | |
super.onStartSuccess(settingsInEffect); | |
} | |
@Override | |
public void onStartFailure(int errorCode) { | |
super.onStartFailure(errorCode); | |
} | |
}); | |
} | |
//--------------------------------------------------------------------------------- | |
public AdvertiseSettings createAdvertiseSettings( int timeoutMillis) { | |
AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder(); | |
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY); | |
builder.setConnectable(true); | |
builder.setTimeout(timeoutMillis); | |
builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH); | |
return builder.build(); | |
} | |
public AdvertiseData createAdvertiseData(String uuidString, int major, | |
int minor, int txPower) { | |
AdvertiseData.Builder mDataBuilder = new AdvertiseData.Builder(); | |
String beaconType = "0215"; | |
String uuid = uuidString.replace("-", ""); | |
String majorStr = formatStringLenth(4, Integer.toHexString(major), '0'); | |
String minorStr = formatStringLenth(4, Integer.toHexString(minor), '0'); | |
String measuredPower = formatStringLenth(2, Integer.toHexString(-59), '0'); | |
// | |
String dataStr = beaconType + uuid + majorStr + minorStr + measuredPower; | |
// | |
byte[] data = hexStringToBytes(dataStr); | |
mDataBuilder.addManufacturerData(0x004C, data); | |
AdvertiseData mAdvertiseData = mDataBuilder.build(); | |
return mAdvertiseData; | |
} | |
//--------------------------------------------------------------------------------- | |
/** | |
* | |
* @param targetLenth 補齊後的長度 | |
* @param src 需要補齊的字串 | |
* @param leftCharacter 用來補齊長度的字元 | |
* @return 補齊字串 | |
*/ | |
public static String formatStringLenth(int targetLenth, String src, char leftCharacter) { | |
if(src.length()>targetLenth){ | |
return src.substring(src.length()-targetLenth); | |
}else{ | |
int delta=targetLenth-src.length(); | |
for (int i = 0; i <delta ; i++) { | |
src=leftCharacter+src; | |
} | |
return src; | |
} | |
} | |
/** | |
* 十六進制字串 轉 字元陣列 | |
* @param hexString "A0B102" | |
* @return new byte[]{(byte) 0xA0,(byte) 0xB1,2} | |
*/ | |
public static byte[] hexStringToBytes(String hexString) { | |
if (hexString == null || hexString.equals("")) { | |
return null; | |
} | |
if(hexString.length()%2!=0){ | |
hexString="0"+hexString; | |
} | |
hexString = hexString.toUpperCase(); | |
int length = hexString.length() / 2; | |
char[] hexChars = hexString.toCharArray(); | |
byte[] d = new byte[length]; | |
for (int i = 0; i < length; i++) { | |
int pos = i * 2; | |
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); | |
} | |
return d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment