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 (377 KB)

好例子

謝謝分享 感謝~~~

请问如何把fireprime作为周边设备,可以做到吗?

05-09 09:53:41.214: E/BluetoothAdapter(20496): bluetooth le advertising not supported
为什么会说不支持? 我是3288的firefly,ap6335的模组。

请问如何把fireprime作为周边设备,可以做到吗?

赞,谢谢楼主