티스토리 뷰
728x90
출처 : Eclipse Kura™ Documentation
The Pi4J Project
라즈베리파이2에 BLE 동글이와 BLE 스마트 버튼인 nurum으로 클릭 이벤트를 받아서
GPIO를 이용해서 LED를 컨트롤 해보았습니다.
BleNurum class
package bletest; import java.util.List; 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; public class BleNurum { private BluetoothGatt m_bluetoothGatt; private BluetoothDevice m_device; private boolean m_connected; public BleNurum(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(); boolean connected = m_bluetoothGatt.connect(); if (connected) { m_bluetoothGatt.setBluetoothLeNotificationListener(listener); 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(); } }
BleTestMain class
package bletest; import java.nio.ByteBuffer; import java.util.List; import java.util.Scanner; import org.eclipse.kura.KuraException; import org.eclipse.kura.bluetooth.BluetoothDevice; import org.eclipse.kura.bluetooth.BluetoothLeNotificationListener; import org.eclipse.kura.bluetooth.BluetoothLeScanListener; import org.eclipse.kura.linux.bluetooth.le.BluetoothLeScanner; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.RaspiPin; public class BleTestMain implements BluetoothLeScanListener, BluetoothLeNotificationListener { private BleNurum ble_nurum = null; private GpioController gpio = null; private GpioPinDigitalOutput led_01 = null; private GpioPinDigitalOutput led_02 = null; public static void main(String[] args) throws Exception { BleTestMain bletest = new BleTestMain(); bletest.init(); bletest.startScan(); } public void init() { // create gpio controller gpio = GpioFactory.getInstance(); led_01 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_08, "Red LED", PinState.HIGH); led_02 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_09, "Blue LED", PinState.HIGH); // set shutdown state for this pin led_01.setShutdownOptions(true, PinState.LOW); led_02.setShutdownOptions(true, PinState.LOW); led_01.low(); led_02.low(); } public void startScan() throws InterruptedException { BluetoothLeScanner m_bls = new BluetoothLeScanner(); m_bls.startScan("hci0", this); Thread.sleep(3000); m_bls.killScan(); } @Override public void onScanFailed(int errorCode) { // TODO Auto-generated method stub System.out.println("error = " + errorCode); } @Override public void onScanResults(List<BluetoothDevice> devices) { System.out.println("onScanResults = " + devices.size()); for (BluetoothDevice device : devices) { System.out.println("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 BleNurum(nurum_device); try { if (ble_nurum.connect(this)) { System.out.println("ble connect"); } } catch (KuraException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static byte[] decode(String hex) { String[] list = hex.split("(?<=\\G.{2})"); ByteBuffer buffer = ByteBuffer.allocate(list.length); System.out.println(list.length); for (String str : list) buffer.put(Byte.parseByte(str, 16)); return buffer.array(); } @Override public void onDataReceived(String handle, String value) { String val = new String( decode(value.replaceAll("\\p{Z}", "")) ); System.out.println("handle: " + handle + " value: " + value + " / " + val); if (val.equals("!1")) { led_01.toggle(); } if (val.equals("!2")) { led_02.toggle(); } if (val.equals("!3")) { led_01.low(); led_02.low(); } if (val.equals("!H")) { ble_nurum.disconnect(); gpio.shutdown(); System.exit(1); } } }
실행
$ sudo java -cp .:pi4j-core.jar:org.apache.commons.io_2.4.0.jar:org.eclipse.equinox.io_1.0.400.v20120522-2049.jar:org.eclipse.osgi_3.8.1.v20120830-144521.jar:org.eclipse.osgi.services_3.3.100.v20120522-1822.jar:org.eclipse.osgi.util_3.2.300.v20120522-1822.jar:org.eclipse.soda.dk.comm_1.2.1.jar:slf4j.api_1.6.4.jar:slf4j.jdk14_1.6.4.jar:slf4j.log4j12_1.6.0.jar:usb4java-javax_1.0.0.jar bletest.BleTestMain
댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 레이싱모델 익스트림 포토 페스티벌
- 일본여행
- Java
- Spring MVC
- Mac
- NDK
- 전예희
- sas2009
- 지스타2007
- 서울오토살롱
- flex
- Xcode
- KOBA
- Linux
- koba2010
- ubuntu
- android
- 송주경
- oracle
- Delphi
- Delphi Tip
- SAS
- ffmpeg
- Spring
- MySQL
- ble
- BPI-M4
- JavaScript
- 튜닝쇼 2008
- 동경
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함