Android 5.0 BLE 周边(BluetoothLeAdvertiser)
来源:互联网 发布:淘宝商城香奈儿包包 编辑:程序博客网 时间:2024/05/16 07:35
转自 http://blog.csdn.net/songzeyang99/article/details/41727023
具有低功耗蓝牙模块的设备可以扮演2个角色,中心,周边。周边是数据提供者,中心是数据接收/处理者。IOS设备可以很好的扮演这2个角色,利用现成的API就能开发出具有周边和中心功能的应用,我大Android就有点悲催了,自Android 4.3的系统就规定了BLE的API,但是仅限于中心,至于周边一直没有API的支持。直到2014.6.26 Android Lollipop的面世,才带来了周边API的支持(BluetoothLeAdvertiser)。
利用空闲时间看了看文档写了个一段关于BLE 周边的代码,当我兴冲冲的运行在Nexus5上面时,瞬间就crash了空指针异常,看了下源代码
/** * Returns a {@link BluetoothLeAdvertiser} object for Bluetooth LE Advertising operations, or null if Bluetooth LE Advertising is not support on this device. * <p> * Use {@link #isMultipleAdvertisementSupported()} to check whether LE Advertising is supported on this device before calling this method. */public BluetoothLeAdvertiser getBluetoothLeAdvertiser() { if (getState() != STATE_ON) { return null; } if (!isMultipleAdvertisementSupported()) { return null; } synchronized(mLock) { if (sBluetoothLeAdvertiser == null) { sBluetoothLeAdvertiser = new BluetoothLeAdvertiser(mManagerService); } } return sBluetoothLeAdvertiser;}
使用方式
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter();BluetoothLeAdvertiser advertiser = mBluetoothAdapter.getBluetoothLeAdvertiser();AdvertiseData data = new AdvertiseData.Builder().addServiceUuid(ParcelUuid.fromString(ADVERTISER_SERVICE_UUID)).build();AdvertiseSettings settings = new AdvertiseSettings.Builder().setConnectable(true).build();advertiser.startAdvertising(settings , data, new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); }});
0 0
- Android 5.0 BLE 周边(BluetoothLeAdvertiser)
- Android5.0 BLE 周边(BluetoothLeAdvertiser)
- Android BLE 总结-源码篇(BluetoothLeAdvertiser)
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央和周边的实现
- Android BLE 蓝牙低功耗教程,中央和周边的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央和周边的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- Android BLE 蓝牙学习总结(一):手机作为周边BluetoothGattServer的实现
- Android模拟IBeacon周边
- android BLE
- 跳舞 纪中2545 dp
- eclipse开发c/c++程序
- char 字符串常量的神秘
- Java 数据库应用系统的实现
- Python基础教程第7章笔记
- Android 5.0 BLE 周边(BluetoothLeAdvertiser)
- 黄阶低级 - 《 Java 核心》- 3.5 运算符 - 修改0次
- Ojbect C中内存管理漫谈.
- Objective-C Runtime 运行时之六:拾遗
- tar命令解压与打包
- 应用JAVA实现WINDOWS中的dir/s查找实例
- Android Manifest 权限描述大全
- 网络协议Http, TCP/IP与UDP详解
- win7下IIS的安装和配置 图文教程