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上实测可以正常广播,读写数据的例程供大家参考。
代码片段如下:
private void initServer() {
BluetoothGattService service =new BluetoothGattService(DeviceProfile.SERVICE_UUID,
BluetoothGattService.SERVICE_TYPE_PRIMARY);
BluetoothGattCharacteristic elapsedCharacteristic =
new BluetoothGattCharacteristic(DeviceProfile.CHARACTERISTIC_ELAPSED_UUID,
//Read-only characteristic, supports notifications
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY,
BluetoothGattCharacteristic.PERMISSION_READ);
BluetoothGattCharacteristic offsetCharacteristic =
new BluetoothGattCharacteristic(DeviceProfile.CHARACTERISTIC_OFFSET_UUID,
//Read+write permissions
BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,
BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);
service.addCharacteristic(elapsedCharacteristic);
service.addCharacteristic(offsetCharacteristic);
mGattServer.addService(service);
}
复制代码
private void startAdvertising() {
if (mBluetoothLeAdvertiser == null) return;
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
.setConnectable(true)
.setTimeout(0)
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
.build();
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(new ParcelUuid(DeviceProfile.SERVICE_UUID))
.build();
mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback);
}
复制代码
完整代码在附件,是在android studio的工程,改成eclipse工程也简单。在IOS下用lightblue测试,安卓下用BLE Device Monitor来测试。
FBleServer.zip
(376.76 KB, 下载次数: 41)
2015-8-7 10:14 上传
点击文件名下载附件
阅读权限: 10
下载积分: 灯泡 -1 , 经验 -1
作者:
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