android ble最低 版本,一、Android BLE开发踩坑全记录

前言

读到这篇文章的想必对BLE开发都有着一定的认知了,在这小编就不多做介绍了,直接开始正题。

问题1:权限配置

问题2:手机权限适配

/**

* 初始化蓝牙

* 支持返回适配器 不支持返回null

*/

public BluetoothAdapter initBLE() {

// 检查当前手机是否支持ble 蓝牙,如果不支持退出程序

if (BaseApplication.instance.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {

//支持BLE 打开蓝牙

BluetoothManager bluetoothManager = (BluetoothManager) BaseApplication.instance

.getSystemService(Context.BLUETOOTH_SERVICE);

bleAdapter = bluetoothManager.getAdapter();

return bleAdapter;

} else {

//不支持蓝牙BLE

return null;

}

}

问题3:手机版本适配

/**

* 手机版本是否需要打开定位

* true 需要定位 false 不需要

*/

public boolean isVersionSDK() {

//6.0以上需要打开定位

if (Build.VERSION.SDK_INT >= 6.0) {

//未开启定位权限

return ContextCompat.checkSelfPermission(BaseApplication.getInstance(), Manifest.permission.ACCESS_FINE_LOCATION)

!= PackageManager.PERMISSION_GRANTED;

} else {

return false;

}

}

问题4:蓝牙回调(重点)

/**

* 搜索蓝牙回调

*/

private BluetoothAdapter.LeScanCallback bleScanCallback = new BluetoothAdapter.LeScanCallback() {

@Override

public void onLeScan(BluetoothDevice device, int arg1, byte[] arg2) {

/** 注意:如果蓝牙回调慢的话就把这段代码注掉,因为很多蓝牙的设备名称是有问题的

String name = device.getName();

if (name == null)

return;

*/

// device就是搜索到的设备

if (BluetoothController.this.serviceHandler != null) {

Message msg = new Message();

msg.what = ConstantUtils.BLUETOOTH_DEVICE_INFO;

msg.obj = device;

BluetoothController.this.serviceHandler.sendMessage(msg);

}

}

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值