SWDesk/App
[Kotlin] 블루투스 페어링을 위한 소스 코드
inhae
2023. 2. 23. 18:25
[MainActivity]
val deviceAddress = "00:00:00:00:00:00" // Replace with the address of your Bluetooth device
val bluetoothAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
// Check if device supports Bluetooth
if (bluetoothAdapter == null) {
// Device does not support Bluetooth
} else {
// Device supports Bluetooth
// Check if Bluetooth is enabled on device
if (!bluetoothAdapter.isEnabled) {
// Request to enable Bluetooth
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
// Get the Bluetooth device by its address
val device: BluetoothDevice = bluetoothAdapter.getRemoteDevice(deviceAddress)
// Check if the device is already paired
if (device.bondState == BluetoothDevice.BOND_BONDED) {
// Device is already paired, no need to pair again
} else {
// Device is not paired, initiate pairing
val pairingIntent = Intent(BluetoothDevice.ACTION_PAIRING_REQUEST)
pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device)
pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.PAIRING_VARIANT_PIN)
startActivityForResult(pairingIntent, REQUEST_PAIRING)
}
}
반응형