I am using the below code to set the Android device act as peripheral, but it does not seem like working. Do you know if peripheral mode is supported with 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);
}