转自 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. * 

* 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);    }});

更多相关文章

  1. Android(安卓)蓝牙学习笔记(一)
  2. Android连接、断开蓝牙扫描枪时屏幕刷新
  3. Android中bluetooth的架构
  4. Android(蓝牙)
  5. Android:基于ibeacon实现蓝牙考勤
  6. [Android源码解析]蓝牙扫描结果反馈的分析
  7. Android提高第十三篇之探秘蓝牙隐藏API
  8. Android(安卓)3D旋转动画——Rotate3dAnimation
  9. Android(安卓)BLE的总结-概念篇

随机推荐

  1. android获取手机型号和系统版本号
  2. 自定义圆形进度条ProgressBar的三种方式
  3. Android去掉标题,强制设置横屏或竖屏
  4. Android(安卓)HorizontalScrollView 水平
  5. Android设置权限问题
  6. 如果,编译Android时新添加的应用程序没有
  7. Android(安卓)使用Gallery仿3D画廊效果实
  8. Android下拉/上拉刷新ListView之Android-
  9. Android媒体文件metadata是如何实现跟解
  10. Android中的DownloadManager