Android API 20是否允许支持蓝牙LE的设备充当外围设备?

时间:2014-07-01 22:15:31

标签: android bluetooth-lowenergy

我使用以下代码将Android设备设置为外围设备,但它似乎不起作用。你知道API 20是否支持外设模式吗?

BluetoothGattServer mGattServer;
public void startPeripheralGattServer() {
        final BluetoothManager bluetoothManager =
                (BluetoothManager) this.getSystemService(Context.BLUETOOTH_SERVICE);

        mGattServer = bluetoothManager.openGattServer(getApplicationContext(), new BluetoothGattServerCallback() {

                @Override
                public void onCharacteristicReadRequest(BluetoothDevice device, int requestId,
                    int offset, BluetoothGattCharacteristic characteristic) {

                    if (mGattServer != null) {
                        mGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, new byte[] { '1' });
                    }
                }
            });

        UUID serviceUUID = UUID.randomUUID();
        UUID characteristicUUID = UUID.randomUUID();
        UUID descriptorUUID = UUID.randomUUID();

        BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUUID, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ);
        characteristic.setValue(77, BluetoothGattCharacteristic.FORMAT_UINT8, 0);

        BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(descriptorUUID,    
                BluetoothGattDescriptor.PERMISSION_READ);

        characteristic.addDescriptor(descriptor);

        BluetoothGattService service = new BluetoothGattService(serviceUUID, 
                BluetoothGattService.SERVICE_TYPE_PRIMARY);
        service.addCharacteristic(characteristic);

        mGattServer.addService(service);
    }

3 个答案:

答案 0 :(得分:3)

根据此Google I/O video for Bluetooth Low Energy,可以从Android API 20(Android L)及更高版本获得外设模式。

答案 1 :(得分:2)

API 20支持外设模式。但是,要作为外设模式进行传输,需要Android 5.0+和支持蓝牙低功耗外设模式的固件。截至2014年12月,只有Nexus 6和Nexus 9设备具有支持蓝牙低功耗外设模式的固件。

click here获取更多信息

答案 2 :(得分:0)

它们将从API Level 21

获得

BLE Peripheral APIs avaliable from API Level 21