android蓝⽛Beacon开发
最近在做android 蓝⽛Beacon开发,记录⼀下⼀些个⼈⼼得:
1.蓝⽛Beacon主要的应⽤场景:
(1)⽤于室内定导航,如⼤型超市、博物馆、机场、校园等;
(2)信息推送(商场活动、景区游览、博物馆展品信息);
(3)和微信摇⼀摇结合,摇互动、摇签到、摇导航等。
2.蓝⽛beacon⼴播包⼴播出来的信息包括4个⽅⾯:
(1)蓝⽛beacon的MAC地址(⼴播包来⾃于哪⼀个 MAC 地址的从机设备);
(2)蓝⽛beacon的信号强度RSSI值(扫描者在搜索到此Beacon时的信号强度);
(3)还有⼴播出来的数据包的内容,该数据包内容最多可以包含 31 个字节的内容(超出会⼴播失败);
(4)蓝⽛beacon的UUID(服务ID,也可以携带数据包内容);
3.android设备硬件要求:
⽀持蓝⽛BLE功能
//判断⼿机是否⽀持BLE
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Log.i(TAG, "⽀持BLE");
} el {
得道多助 失道寡助翻译Log.i(TAG, "不⽀持BLE");
}
4.权限要求:
<us-permission android:name="android.permission.BLUETOOTH" />
<us-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<us-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<us-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
5.判断是否授权定位权限:
这⾥我使⽤的权限库是: implementation 'pub.devrel:easypermissions:1.1.0',实现⽅法不再赘述。
(1)⾸先检查权限:
private String[] perms = { Manifest.permission.ACCESS_FINE_LOCATION};
private static final int REQUEST_CODE = 0x01;
private void checkPerms() {
if(Build.VERSION.SDK_INT>=23){
if(EasyPermissions.hasPermissions(MainActivity.this,perms)){
ndBeacon();
}el {
}el {
ndBeacon();
}
online}
prema(2)打开蓝⽛:
private BluetoothLeAdvertir mBluetoothLeAdvertir;
private BluetoothAdapter bluetoothAdapter;
private void ndBeacon(){
BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = Adapter();
戴安娜王妃简介//实际项⽬需监听蓝⽛已开启,再执⾏后续操作,本demo不做详细阐述,只提供思路
if(bluetoothAdapter.isEnabled()){ // 判断蓝⽛是否可⽤
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeAdvertir = BluetoothLeAdvertir();
startAdvertising();
}
},2000);
}
cornflower
}
6.设置⼴播:
先来了解⼀下AdvertiSettings,它主要是设置⼴播模式,⽤来控制⼴播性能及延迟。
6.1 tAdvertiMode(设置频率):
ADVERTISE_MODE_LOW_LATENCY 100ms (低延迟)
ADVERTISE_MODE_BALANCED 250ms (平衡模式)
ADVERTISE_MODE_LOW_POWER 1s (低功耗)
6.2 tTxPowerLevel(设置⼴播发送功率,由⾼到低):
ADVERTISE_TX_POWER_HIGH = 3;
ADVERTISE_TX_POWER_MEDIUM = 2;
ADVERTISE_TX_POWER_LOW = 1;
ADVERTISE_TX_POWER_ULTRA_LOW = 0;
6.3 tConnectable(设置⼴播类型是否可连接)
true:可连接
fal:不可连接
6.4 tTimeout(设置⼴播超时时间):
int型,不得超过180000毫秒;如果设置值为0将禁⽤时间限制,⼀直⼴播;
private AdvertiSettings buildAdvertiSettings() {
AdvertiSettings.Builder ttingsBuilder = new AdvertiSettings.Builder();
ttingsBuilder.tAdvertiMode(AdvertiSettings.ADVERTISE_MODE_LOW_LATENCY);
party是什么意思
ttingsBuilder.tTxPowerLevel(AdvertiSettings.ADVERTISE_TX_POWER_HIGH);
ttingsBuilder.tConnectable(fal); // ⼀般设置不可连接,可根据实际场景设置
ttingsBuilder.tTimeout(0);
return ttingsBuilder.build();avon
}
7.设置UUID及数据包:
AdvertiData⽅法介绍:
addServiceUuid(ParcelUuid rviceUuid): // 添加服务UUID,也可以通过多次add的⽅式来组⾃定义数据包
addServiceData(ParcelUuid rviceDataUuid, byte[] rviceData): // 添加服务UUID及⾃定义数据包
addManufacturerData(int manufacturerId, byte[] manufacturerSpecificData): // 添加制造商特定UUI
D及⾃定义数据包tIncludeTxPowerLevel(boolean includeTxPowerLevel): // 发送数据包中是否应包括发射功率电平
tIncludeDeviceName(boolean includeDeviceName): // 是否包含设备名称,尽量不要设置为true,否则会导致可携带的⾃定义数据包⼤⼤减⼩;
这⾥⼏种发送数据包的⽅式type不同,具体可在接收数据包中查看。
private final static String Service_UUID = "00001234-0000-1000-8000-00805F9B34FB";
// 发送⾃定义数据包
private AdvertiData buildAdvertiData() {
AdvertiData.Builder dataBuilder = new AdvertiData.Builder();
// dataBuilder.tIncludeDeviceName(true);
dataBuilder.addServiceUuid(ParcelUuid.fromString(Service_UUID));
return dataBuilder.build();
}
8.蓝⽛Beacon⼴播结果回调
这⾥说⼀下返回的常见的⼏种errorCode:
public static final int ADVERTISE_FAILED_DATA_TOO_LARGE = 1; // 常见错误,⼴播的数据包太⼤,⽆法启动⼴播,检查⼀下是否有设置dataBuilder.tIncludeDeviceName(true);如果有,改为fal;如果未设置,那就是数据包超出了最⼤长度。
public static final int ADVERTISE_FAILED_TOO_MANY_ADVERTISERS = 2;// 没有可⽤的⼴播实例
public static final int ADVERTISE_FAILED_ALREADY_STARTED = 3; //⼴播已经开始,⽆法开始⼴播日文在线翻译器
public static final int ADVERTISE_FAILED_INTERNAL_ERROR = 4; // 由于内部错误,操作失败
public static final int ADVERTISE_FAILED_FEATURE_UNSUPPORTED = 5; //该平台不⽀持此功能
个⼈在实际开发的过程中,还遇到过errorCode=7的情况,⽽出现这种情况是由于ndDatas中的数据包太短,仅供参考。
public class MyAdvertiCallback extends AdvertiCallback {
private String TAG = getClass().getName();
@Override
public void onStartFailure(int errorCode) {
Log.i(TAG, "AdvertiCallback onStartFailure");
}
@Override
public void onStartSuccess(AdvertiSettings ttingsInEffect) {
Log.i(TAG, "AdvertiCallback onStartSuccess");
}
}
9.开始蓝⽛⼴播
private void startAdvertising() {
if (mAdvertiCallback == null) {
AdvertiSettings ttings = buildAdvertiSettings();
AdvertiData data = buildAdvertiData();
mAdvertiCallback = new MyAdvertiCallback();
爱词霸在线if (mBluetoothLeAdvertir != null) {
if(mAdvertiCallback != null){
mBluetoothLeAdvertir.stopAdvertising(mAdvertiCallback); // 如果之前未停⽌⼴播,先停⽌⼴播
}
mBluetoothLeAdvertir.startAdvertising(ttings, data, mAdvertiCallback);
}
免费日语翻译
}
}
10.停⽌⼴播:
if (mBluetoothLeAdvertir != null) {
mBluetoothLeAdvertir.stopAdvertising(mAdvertiCallback);
}
这⾥推荐⼀款软件-- nRF Connect ,可以接收到蓝⽛Beacon包,以验证⾃⼰发的包数据。以上即为本⼈在开发蓝⽛Beacon的⼀些⼼得,如有不⾜之处,欢迎指导~