1:手机蓝牙在4.0及以上 支持ble从模式,android 的最低版本5.0
2:
BluetoothLeAdvertiser mBluetoothLeAdvertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();// 做从广播mBluetoothLeAdvertiser.startAdvertising(makeAdvertiseSetting(),createIBeaconAdvertiseData(UUID.fromString(beaconUUID), (short) major, (short) minor, (byte) 0xc8),scanResponse(), mAdvertiseCallback);
private AdvertiseSettings makeAdvertiseSetting() {
AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
builder.setConnectable(CONNECTABLE);
builder.setTimeout(TIMEOUT);
return builder.build();
}
private AdvertiseSettings makeAdvertiseSetting() {
AdvertiseSettings.Builder builder = new AdvertiseSettings.Builder();
builder.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY);
builder.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH);
builder.setConnectable(CONNECTABLE);
builder.setTimeout(TIMEOUT);
return builder.build();
}
/**
* ================================================
*
*proximityUuid UUID.randomUUID()
* major major
* ================================================
*/
public static AdvertiseData createIBeaconAdvertiseData(UUID proximityUuid, short major, short minor, byte txPower) {
if (proximityUuid == null) {throw new IllegalArgumentException("proximitiUuid null");
}
byte[] manufacturerData = new byte[23];
ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
bb.order(ByteOrder.BIG_ENDIAN);
bb.put((byte) 0x02);
bb.put((byte) 0x15);
bb.putLong(proximityUuid.getMostSignificantBits());
bb.putLong(proximityUuid.getLeastSignificantBits());
bb.putShort(major);
bb.putShort(minor);
bb.put(txPower);
AdvertiseData.Builder builder = new AdvertiseData.Builder();
builder.addManufacturerData(0x004c, manufacturerData); //0x004c 这个是苹果认证的标识 更换之后就不能识别 必须得是苹果认证的标识
AdvertiseData adv = builder.build();
return adv;
}
public AdvertiseData scanResponse() {
Builder mDataBuilder = new Builder();
byte[] serviceData = { 11 };
AdvertiseData mAdvertiseData = mDataBuilder.build();
if (mAdvertiseData == null) {
}
return mAdvertiseData;
}
// 广播回调
private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
super.onStartSuccess(settingsInEffect);
// new UploadDevice().execute();
SharedPreferencesUtil.saveBooleanData(mContext, "bleupload", true);
if (settingsInEffect != null) {
Log.d(TAG, "onStartSuccess TxPowerLv=" + settingsInEffect.getTxPowerLevel() + " mode="
+ settingsInEffect.getMode() + " timeout=" + settingsInEffect.getTimeout());
} else {
Log.e(TAG, "onStartSuccess, settingInEffect is null");
}
Log.e(TAG, "onStartSuccess settingsInEffect" + settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
super.onStartFailure(errorCode);
Log.e(TAG, "onStartFailure errorCode" + errorCode);
if (errorCode == ADVERTISE_FAILED_DATA_TOO_LARGE) {
Toast.makeText(mContext,
"Failed to start advertising as the advertise data to be broadcasted is larger than 31 bytes.",
Toast.LENGTH_LONG).show();
Log.e(TAG,
"Failed to start advertising as the advertise data to be broadcasted is larger than 31 bytes.");
} else if (errorCode == ADVERTISE_FAILED_TOO_MANY_ADVERTISERS) {
Toast.makeText(mContext, "Failed to start advertising because no advertising instance is available.",
Toast.LENGTH_LONG).show();
Log.e(TAG, "Failed to start advertising because no advertising instance is available.");
} else if (errorCode == ADVERTISE_FAILED_ALREADY_STARTED) {
Toast.makeText(mContext, "Failed to start advertising as the advertising is already started",
Toast.LENGTH_LONG).show();
Log.e(TAG, "Failed to start advertising as the advertising is already started");
} else if (errorCode == ADVERTISE_FAILED_INTERNAL_ERROR) {
Toast.makeText(mContext, "Operation failed due to an internal error", Toast.LENGTH_LONG).show();
Log.e(TAG, "Operation failed due to an internal error");
} else if (errorCode == ADVERTISE_FAILED_FEATURE_UNSUPPORTED) {
Toast.makeText(mContext, "This feature is not supported on this platform", Toast.LENGTH_LONG).show();
Log.e(TAG, "This feature is not supported on this platform");
}
Intent stop = new Intent(mContext, FunctionBleService.class);
mContext.stopService(stop);
}
};