티스토리 뷰
728x90
출처 : Raspberry Pi: Control WS2812B (NeoPixels) With Bluetooth LE
Eclipse Kura™ Documentation
The Pi4J Project
Bluetooth 모듈이 내장된 블루이노2로 BLE 송신하고, 라즈베리파이3의 Bluetooth 모듈로 수신 받아 데이터처리 해보았습니다.
블루이노2 코드
#include <RFduinoBLE.h> int Potent=2; boolean is_connect; void setup() { // put your setup code here, to run once: RFduinoBLE.deviceName = "bluexmas"; // this is the data we want to appear in the advertisement //RFduinoBLE.advertisementData = "My BLE LED"; //RFduinoBLE.advertisementInterval = MILLISECONDS(300); //RFduinoBLE.txPowerLevel = -20; // (-20dbM to +4 dBm) Serial.begin(9600); Serial.println("Start Test"); // With more than 15 characters thie Start Test keeps happening // start the BLE stack RFduinoBLE.begin(); } void loop() { char BPM_lcd[10]; RFduino_ULPDelay(100); // Switch to lower power mode //RFduino_ULPDelay(INFINITE); //RFduino_ULPDelay( SECONDS(1) ); //sample once per second if (is_connect) { int sensorValue = analogRead(Potent); Serial.println(sensorValue); // debug value //sprintf(BPM_lcd, "%10d", sensorValue); //lcd.println(BPM_lcd); RFduinoBLE.sendInt(sensorValue); //sends the char array } } void RFduinoBLE_onConnect() { // Debug message printed to Serial interface Serial.println("RFduino connected"); is_connect = true; } void RFduinoBLE_onDisconnect() { // Debug message printed to Serial interface Serial.println("RFduino disconnected"); is_connect = false; } void RFduinoBLE_onReceive(char *data, int len) { // Debug message printed to Serial interface Serial.println("Data received: "); for(int i=0;i<len;i++) Serial.print(data[i]); Serial.println(); Serial.println(data); }
Blueinno.java
BLE 관련 코드만 보기 편하게 하기 위해서 모터 제어 관련 코드는 빼고 블루이노2와 통신하는 부분만 공개
package ble.blueinno2; import java.util.List; import java.util.Scanner; import org.eclipse.kura.KuraException; import org.eclipse.kura.bluetooth.BluetoothDevice; import org.eclipse.kura.bluetooth.BluetoothGatt; import org.eclipse.kura.bluetooth.BluetoothGattService; import org.eclipse.kura.bluetooth.BluetoothLeNotificationListener; import org.eclipse.kura.bluetooth.BluetoothLeScanListener; import org.eclipse.kura.linux.bluetooth.util.BluetoothProcess; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Blueinno implements BluetoothLeNotificationListener, BluetoothLeScanListener { private static final Logger logger = LoggerFactory.getLogger(BluetoothProcess.class); private BluetoothGatt m_bluetoothGatt; private BluetoothDevice m_device; private boolean m_connected; public Blueinno(BluetoothDevice bluetoothDevice) { // m_device = bluetoothDevice; m_connected = false; } public BluetoothDevice getBluetoothDevice() { return m_device; } public void setBluetoothDevice(BluetoothDevice device) { m_device = device; } public boolean isConnected() { return m_connected; } public boolean connect(BluetoothLeNotificationListener listener) throws KuraException { m_bluetoothGatt = m_device.getBluetoothGatt(); logger.info("접속시도"); boolean connected = m_bluetoothGatt.connect(); if (connected) { m_bluetoothGatt.setBluetoothLeNotificationListener(listener); logger.info("리스너등록"); m_connected = true; return true; } else { // If connect command is not executed, close gatttool m_bluetoothGatt.disconnect(); m_connected = false; return false; } } public void disconnect() { if (m_bluetoothGatt != null) { m_bluetoothGatt.disconnect(); m_connected = false; } } /* * Discover services */ public List<BluetoothGattService> discoverServices() { return m_bluetoothGatt.getServices(); } @Override public void onScanFailed(int errorCode) { // TODO Auto-generated method stub System.out.println("error = " + errorCode); } @Override public void onScanResults(List<BluetoothDevice> devices) { logger.info("onScanResults = " + devices.size()); for (BluetoothDevice device : devices) { logger.info("Found Bluetooth Device " + device.getName() + " [" + device.getAdress() + "]"); } System.out.print("ble address > "); Scanner scanner = new Scanner(System.in); String blc_address = scanner.next(); BluetoothDevice nurum_device = null; for (BluetoothDevice device : devices) { if (device.getAdress().equals(blc_address)) { nurum_device = device; } } if (nurum_device != null) { //ble_nurum = new Blueinno(nurum_device); this.setBluetoothDevice(nurum_device); try { if (this.connect(this)) { System.out.println("ble connect"); Runnable aa = new Runnable() { @Override public void run() { while (true) { try { String sensorValue = m_bluetoothGatt.readCharacteristicValue("0x000e"); System.out.println(sensorValue); // 모터제어 // 생략 } catch (KuraException e1) { e1.printStackTrace(); } try { Thread.sleep(100); } catch (Exception e) { } } } }; Thread bb = new Thread(aa); bb.start(); } } catch (KuraException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void onDataReceived(String handle, String value) { System.out.println("handle: " + handle + " value: " + value); } }
BlueinnoMain.java
package ble.blueinno2; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.eclipse.kura.linux.bluetooth.le.BluetoothLeScanner; public class BlueinnoMain { Logger logger = Logger.getLogger(this.getClass()); private Blueinno blueinno = null; public static void main(String[] args) throws Exception { BlueinnoMain bletest = new BlueinnoMain(); bletest.init(); bletest.startScan(); } public void init() { Logger.getRootLogger().setLevel(Level.DEBUG); logger.info("초기화"); blueinno = new Blueinno(); } public void startScan() throws InterruptedException { BluetoothLeScanner m_bls = new BluetoothLeScanner(); m_bls.startScan("hci0", blueinno); Thread.sleep(10000); m_bls.killScan(); } }
실행
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Java
- 일본여행
- JavaScript
- KOBA
- Spring
- android
- Mac
- 레이싱모델 익스트림 포토 페스티벌
- 지스타2007
- flex
- 동경
- oracle
- NDK
- Spring MVC
- BPI-M4
- ubuntu
- Xcode
- Delphi Tip
- MySQL
- Delphi
- Linux
- ble
- 서울오토살롱
- SAS
- sas2009
- 튜닝쇼 2008
- koba2010
- ffmpeg
- 전예희
- 송주경
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함