Firefly开源社区

打印 上一主题 下一主题

FirePrime 板上 BLE 做Peripheral模式样例

122

积分

1

威望

0

贡献

社区版主

Rank: 7Rank: 7Rank: 7

积分
122

FirePrime 板上 BLE 做Peripheral模式样例

发表于 2015-8-7 10:17:59      浏览:9032 | 回复:6        打印     [复制链接] 楼主
本帖最后由 暴走的阿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)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

友情链接 : 爱板网 电子发烧友论坛 云汉电子社区 粤ICP备14022046号-2
快速回复 返回顶部 返回列表