Skip to content

Instantly share code, notes, and snippets.

@Arunshaik2001
Created October 26, 2022 14:36
Show Gist options
  • Save Arunshaik2001/37aaf18a7fd474e5269a201ac53f455e to your computer and use it in GitHub Desktop.
Save Arunshaik2001/37aaf18a7fd474e5269a201ac53f455e to your computer and use it in GitHub Desktop.
fun startServer(app: Application, activity: ComponentActivity) {
bluetoothManager = app.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
adapter = bluetoothManager.adapter
if (!adapter.isEnabled) {
_requestEnableBluetooth.value = true
val takeResultListener =
activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == -1) {
Toast.makeText(activity, "Bluetooth ON", Toast.LENGTH_LONG).show()
setupGattServer(app)
startAdvertisement()
} else {
Toast.makeText(activity, "Bluetooth OFF", Toast.LENGTH_LONG).show()
}
}
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
takeResultListener.launch(intent)
} else {
_requestEnableBluetooth.value = false
setupGattServer(app)
startAdvertisement()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment