-
-
Save Arunshaik2001/37aaf18a7fd474e5269a201ac53f455e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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