|
FirePrime 板上 BLE 做Peripheral模式样例
发表于 2015-8-7 10:17:59
浏览:11516
|
回复:6
打印
只看该作者
[复制链接]
楼主
本帖最后由 暴走的阿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)
|
|