Skip to content

Instantly share code, notes, and snippets.

@kbrandwijk
Last active February 3, 2022 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbrandwijk/5e72eefc102a70b0d8c1e506f0f5e407 to your computer and use it in GitHub Desktop.
Save kbrandwijk/5e72eefc102a70b0d8c1e506f0f5e407 to your computer and use it in GitHub Desktop.
{
"expo": {
"plugins": [
[
"./withStarPrinter.js",
{
"minSdkVersion": 23,
"bluetoothAlwaysPermission": "My permission description",
"localNetworkUsagePermission": "My other permission description"
}
],
]
}
}
const {
withProjectBuildGradle,
withPlugins,
withInfoPlist
} = require('@expo/config-plugins');
const BLUETOOTH_ALWAYS =
"Allow $(PRODUCT_NAME) to connect to bluetooth devices";
const LOCAL_NETWORK_USAGE =
"Allow $(PRODUCT_NAME) to discover devices on the network";
function setMinSdkVersion(buildGradle, minVersion) {
const regexpMinSdkVersion = /\bminSdkVersion\s*=\s*(\d+)/;
const match = buildGradle.match(regexpMinSdkVersion);
if (match) {
const version = parseInt(match[1], 10);
if (version < minVersion) {
buildGradle = buildGradle.replace(
/\bminSdkVersion\s*=\s*\d+/,
`minSdkVersion = ${minVersion}`
);
} else {
console.warn(`WARN: minSdkVersion is already >= ${version}`);
}
}
return buildGradle;
}
const withIosPermissions = (c, { bluetoothAlwaysPermission, localNetworkUsagePermission } = {}) => {
return withInfoPlist(c, (config) => {
if (bluetoothAlwaysPermission !== false) {
config.modResults.NSBluetoothAlwaysUsageDescription =
localNetworkUsagePermission ||
config.modResults.NSBluetoothAlwaysUsageDescription ||
BLUETOOTH_ALWAYS;
}
if (bluetoothPeripheralPermission !== false) {
config.modResults.NSLocalNetworkUsageDescription =
localNetworkUsagePermission ||
config.modResults.NSLocalNetworkUsageDescription ||
BLUETOOTH_PERIPHERAL_USAGE;
}
return config;
});
};
const withAccessoryProtocols = (c, props) => {
return withInfoPlist(c, (config) => {
const existingProtocols = config.modResults.NSBluetoothProtocols || [];
config.modResults.UISupportedExternalAccessoryProtocols = [...existingProtocols, 'jp.star-m.starpro']
return config;
});
}
const withMinSdkVersion = (config, { minSdkVersion } = {}) => {
return withProjectBuildGradle(config, (config) => {
if (config.modResults.language === 'groovy') {
config.modResults.contents = setMinSdkVersion(
config.modResults.contents,
minSdkVersion
);
} else {
throw new Error(
"Can't set minSdkVersion in the project build.gradle, because it's not groovy"
);
}
return config;
});
};
const withAndroidPermissions = (config, props) => {
config = AndroidConfig.Permissions.withPermissions(config, [
'android.permission.BLUETOOTH_CONNECT ',
]);
return config;
};
module.exports = (config, props) =>
withPlugins(config, [
[withMinSdkVersion, props],
[withIosPermissions, props],
[withAccessoryProtocols, props],
[withAndroidPermissions, props],
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment