Firefly开源社区

标题: FirePrime 板上 BLE 做Peripheral模式样例 [打印本页]

作者: error结构体    时间: 2015-8-7 10:17
标题: FirePrime 板上 BLE 做Peripheral模式样例
本帖最后由 暴走的阿Sai 于 2015-8-19 16:55 编辑

google 在android L后增加了对BLE Peripheral模式的支持。我整理了一段可以在RK312x上实测可以正常广播,读写数据的例程供大家参考。

代码片段如下:
  1. private void initServer() {
  2.         BluetoothGattService service =new BluetoothGattService(DeviceProfile.SERVICE_UUID,
  3.                 BluetoothGattService.SERVICE_TYPE_PRIMARY);

  4.         BluetoothGattCharacteristic elapsedCharacteristic =
  5.                 new BluetoothGattCharacteristic(DeviceProfile.CHARACTERISTIC_ELAPSED_UUID,
  6.                 //Read-only characteristic, supports notifications
  7.                 BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
  8.                 BluetoothGattCharacteristic.PERMISSION_READ);
  9.         BluetoothGattCharacteristic offsetCharacteristic =
  10.                 new BluetoothGattCharacteristic(DeviceProfile.CHARACTERISTIC_OFFSET_UUID,
  11.                 //Read+write permissions
  12.                 BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
  13.                 BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);

  14.         service.addCharacteristic(elapsedCharacteristic);
  15.         service.addCharacteristic(offsetCharacteristic);

  16.         mGattServer.addService(service);
  17.     }
复制代码
  1.     private void startAdvertising() {
  2.         if (mBluetoothLeAdvertiser == null) return;

  3.         AdvertiseSettings settings = new AdvertiseSettings.Builder()
  4.                 .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
  5.                 .setConnectable(true)
  6.                 .setTimeout(0)
  7.                 .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
  8.                 .build();

  9.         AdvertiseData data = new AdvertiseData.Builder()
  10.                 .setIncludeDeviceName(true)
  11.                 .addServiceUuid(new ParcelUuid(DeviceProfile.SERVICE_UUID))
  12.                 .build();

  13.         mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
  14.     }
复制代码

完整代码在附件,是在android studio的工程,改成eclipse工程也简单。在IOS下用lightblue测试,安卓下用BLE Device Monitor来测试。

FBleServer.zip (376.76 KB, 下载次数: 41)


作者: carlinluo    时间: 2015-8-7 13:12
好例子
作者: ddh210.wst    时间: 2016-4-11 14:50
謝謝分享 感謝~~~
作者: sazuke00x    时间: 2016-5-9 16:37
请问如何把fireprime作为周边设备,可以做到吗?
作者: sazuke00x    时间: 2016-5-9 17:55
05-09 09:53:41.214: E/BluetoothAdapter(20496): bluetooth le advertising not supported
为什么会说不支持? 我是3288的firefly,ap6335的模组。
作者: 似水年华_will    时间: 2017-11-23 15:54
请问如何把fireprime作为周边设备,可以做到吗?
作者: huangkeyuan    时间: 2018-5-28 16:55
赞,谢谢楼主




欢迎光临 Firefly开源社区 (https://dev.t-firefly.com/) Powered by Discuz! X3.1