티스토리 뷰
[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 a message to the device
val outputStream = bluetoothSocket.outputStream
val message = "Hello from Android phone"
val bytes = message.toByteArray()
outputStream.write(bytes)
// Receive a message from the device
val inputStream = bluetoothSocket.inputStream
val buffer = ByteArray(1024)
val numBytes = inputStream.read(buffer)
val receivedMessage = String(buffer, 0, numBytes)
}
// Close the Bluetooth socket when finished
try {
bluetoothSocket?.close()
} catch (e: IOException) {
// Error occurred while closing the socket
}
[Nano33 IoT] by Arduino-IDE
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
String receivedMessage = "";
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(9600);
BLEDevice::init("nano33iot");
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
pCharacteristic->setValue("Hello from nano33iot");
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0
반응형
'SWDesk > App' 카테고리의 다른 글
웹뷰 내 사용자 클릭 결과를 처리하기 위한 자바 소스 코드 (0) | 2023.02.26 |
---|---|
[Kotlin] 블루투스 페어링을 위한 소스 코드 (0) | 2023.02.23 |
[Kotlin] Nano33 IoT 기기에 "Hello" 보내기 (0) | 2023.02.17 |
[Kotlin] Bluetooth Application in Android (0) | 2023.02.14 |
[Python] msedge 드라이버 및 모듈 설치 (0) | 2022.09.03 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- badp
- Innovations&Hurdles
- Hurdles
- 심심풀이
- 둎
- ServantClock
- 아두이노
- 전압전류모니터링
- image
- 허들
- BSC
- Video
- 오블완
- 혁신
- DYOV
- Innovation&Hurdles
- 심심풀이치매방지기
- 전류
- 티스토리챌린지
- 혁신과허들
- 치매
- 치매방지
- Decorator
- arduino
- 절연형
- bilient
- 전압
- 빌리언트
- 배프
- Innovations
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함