티스토리 뷰
[MainActivity]
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import java.io.IOException
import java.util.*
val deviceAddress = "00:00:00:00:00:00" // Replace with the address of your nano33iot device
val uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") // UUID for serial communication
var bluetoothSocket: BluetoothSocket? = null
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)
// Connect to the device using a Bluetooth socket
try {
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid)
bluetoothSocket.connect()
} catch (e: IOException) {
// Connection failed
}
// Send the text "Hello" to the device
val outputStream = bluetoothSocket.outputStream
val bytes = "Hello".toByteArray()
outputStream.write(bytes)
}
// Close the Bluetooth socket when finished
try {
bluetoothSocket?.close()
} catch (e: IOException) {
// Error occurred while closing the socket
}
반응형
'SWDesk > App' 카테고리의 다른 글
[Kotlin] 블루투스 페어링을 위한 소스 코드 (0) | 2023.02.23 |
---|---|
[Kotlin] Nano33 IoT 기기와 데이터 통신하기 (2) | 2023.02.20 |
[Kotlin] Bluetooth Application in Android (0) | 2023.02.14 |
[Python] msedge 드라이버 및 모듈 설치 (0) | 2022.09.03 |
[Android] 테두리 만들기 (0) | 2021.11.28 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- image
- Hurdles
- 빌리언트
- 둎
- Innovations&Hurdles
- 혁신과허들
- 아두이노
- Video
- 절연형
- 전압전류모니터링
- 허들
- 치매방지
- arduino
- Innovations
- 심심풀이치매방지기
- 배프
- 전압
- 전류
- 치매
- Decorator
- 오블완
- badp
- DYOV
- bilient
- ServantClock
- Innovation&Hurdles
- 심심풀이
- 혁신
- 티스토리챌린지
- BSC
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
글 보관함