ibeacon 제작
아두이노와 블루투스 모듈 HM-10를 이용해서ibeacon을 만든다
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(3, 2);
void setup()
{
Serial.begin(9600);
Serial.println("Hello!");
// set the data rate for the BT port
BTSerial.begin(9600);
}
void loop() {
while (BTSerial.available()) {
byte data = BTSerial.read();
Serial.write(data); // write it to serial(serial monitor)
}
while (Serial.available()) {
byte data = Serial.read();
BTSerial.write(data);
}
}
시리얼 모니터 명령
AT+RENEW //공장 초기화
AT //OK 사인이 오는지 확인
AT+MARJ0x1234 //iBeacon의 Major Number 설정
AT+MINO0xFA01 //iBeacon의 Minor Number설정
AT+ADVI5 //신호 송출 주기를 5로 설정
AT+NAMEBLETEST// BLE모듈 이름을 BLETEST으로 설정(원하는 이름으로 하세요)
AT+ADTY3 //non-connectable 상태로 만듦
AT+IBEA1 //iBeacon 활성화
AT+DELO2 // iBeacon 브로드캐스트 전용 모드로 설정
AT+PWRM0 //최소 절전 모드 설정
AT+RESET //Rebutting
Flutter 비콘 인식 코드
beacon_plugin 라이브러리를 사용하였다.
import 'dart:async';
import 'package:ucms/components/custom_screen.dart';
import 'package:ucms/components/texts.dart';
import 'package:beacons_plugin/beacons_plugin.dart';
import 'package:flutter/material.dart';
import 'dart:io' show Platform;
import 'package:flutter/services.dart';
class BeaconTest extends StatefulWidget {
const BeaconTest({Key? key}) : super(key: key);
@override
State<BeaconTest> createState() => _BeaconTestState();
}
class _BeaconTestState extends State<BeaconTest> {
String _beaconResult = "";
var isRunning = false;
final StreamController<String> beaconEventsController = StreamController<String>.broadcast();
Future<void> initPlatformState() async {
if (Platform.isAndroid) {
await BeaconsPlugin.setDisclosureDialogMessage(
title: "Need Location Permission",
message: "This app collects location data to work with beacons."
);
}
BeaconsPlugin.listenToBeacons(beaconEventsController);
// iBeacon uuid 등록
await BeaconsPlugin.addRegion(
"testBeacon", "74278BDA-B644-4520-8F0C-720EAF059935");
BeaconsPlugin.setForegroundScanPeriodForAndroid(
foregroundScanPeriod: 1000, foregroundBetweenScanPeriod: 10);
BeaconsPlugin.setBackgroundScanPeriodForAndroid(
backgroundScanPeriod: 1000, backgroundBetweenScanPeriod: 10);
beaconEventsController.stream.listen((data) {
if (data.isNotEmpty && isRunning) {
setState(() {
_beaconResult = data;
});
print(data);
// data 구조
// {
// "name": "testBeacon",
// "uuid": "74278bda-b644-4520-8f0c-720eaf059935",
// "macAddress": "34:14:B5:41:A2:7E", <- uuid가 아닌 mac주소로 비콘 구분
// "major": "4660",
// "minor": "64001",
// "distance": "0.32",
// "proximity": "Immediate",
// "scanTime": "02 October 2021 01:10:10 PM",
// "rssi": "-47",
// "txPower": "-59"
// }
}
}, onDone: () {}, onError: (error) {print("Error: $error");});
await BeaconsPlugin.runInBackground(true);
if (Platform.isAndroid) {
BeaconsPlugin.channel.setMethodCallHandler((call) async {
if (call.method == 'scannerReady') {
await BeaconsPlugin.startMonitoring();
setState(() {
isRunning = true;
});
}
});
} else if (Platform.isIOS) {
await BeaconsPlugin.startMonitoring();
setState(() {
isRunning = true;
});
}
if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: KScreen(
child: ListView(
children: [
const SizedBox(height: 100),
title("비콘 테스트"),
const SizedBox(height: 20),
Text(_beaconResult),
ElevatedButton(
onPressed: () async {
if (isRunning) {
await BeaconsPlugin.stopMonitoring();
} else {
initPlatformState();
await BeaconsPlugin.startMonitoring();
}
setState(() {
isRunning = !isRunning;
});
},
child:
Text(isRunning ? 'Stop Scanning' : 'Start Scanning',
style: TextStyle(fontSize: 20)),
)
],
),
),
);
}
}
결과
'대회, 프로젝트 > 2021 군장병 해커톤' 카테고리의 다른 글
[pm2] 502 Bad Gateway 해결 (0) | 2021.10.07 |
---|---|
[MYSQL] 가장 최근 삽입한 row 가져오기 (0) | 2021.10.06 |
[Nodejs / Socket.io] 소켓 통신 및 사용자 인증 (0) | 2021.10.01 |
[Node.js / API Server] jwt passport 로그인 (0) | 2021.09.30 |
[Node.js / API Server] passport session 로그인 구현 (0) | 2021.09.30 |
Comment