首页 > 代码库 > Android5.0 BLE 周边(BluetoothLeAdvertiser)
Android5.0 BLE 周边(BluetoothLeAdvertiser)
具有低功耗蓝牙模块的设备可以扮演2个角色,中心,周边。周边是数据提供者,中心是数据接收/处理者。IOS设备可以很好的扮演这2个角色,利用现成的API就能开发出具有周边和中心功能的应用,我大Android就有点悲催了,自Android 4.3的系统就规定了BLE的API,但是仅限于中心,至于周边一直没有API的支持。直到2014.6.26 Android Lollipop的面世,才带来了周边API的支持(BluetoothLeAdvertiser)。
利用空闲时间看了看文档写了个一段关于BLE 周边的代码,当我兴冲冲的运行在Nexus5上面时,瞬间就crash了空指针异常,看了下源代码
<span style="font-size:18px;">/** * Returns a {@link BluetoothLeAdvertiser} object for Bluetooth LE Advertising operations, o<span style="white-space:pre"> </span>r * null if Bluetooth LE Advertising is not support on this device. * <p> * Use {@link #isMultipleAdvertisementSupported()} to check whether LE Advertising is suppor<span style="white-space:pre"> </span>ted * 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; } </span>
后来在官网上看了下,现在支持周边的设备只有Nexus6和Nexus9,虽然我很想入手Nexus6,但又不想转运和淘宝,无奈等吧.....说真的在中国开发Android真蛋疼~
最后贴下小代码,欢迎交流指导
<span style="font-size:18px;"> final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); BluetoothAdapter mBluetoothAdapter = bluetoothManager.getAdapter(); BluetoothLeAdvertiser advertiser = mBluetoothAdapter.getBluetoothLeAdvertiser(); AdvertiseData data = http://www.mamicode.com/new AdvertiseData.Builder()>转载请标明出处,多谢
Android5.0 BLE 周边(BluetoothLeAdvertiser)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。