1 /* 2 * Author: Stefan Andritoiu <stefan.andritoiu@intel.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 import upm_nrf24l01.Callback; 26 27 //NOT TESTED!!! 28 public class NRF24L01_receiverSample { 29 30 static private final byte[] local_address = {0x01, 0x01, 0x01, 0x01, 0x01}; 31 static private final byte[] broadcast_address = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 32 (byte) 0xFF, (byte) 0xFF}; 33 34 static { 35 try { 36 System.loadLibrary("javaupm_nrf24l01"); 37 } catch (UnsatisfiedLinkError e) { 38 System.err.println("error in loading native library"); 39 System.exit(-1); 40 } 41 } 42 main(String[] args)43 public static void main(String[] args) throws InterruptedException { 44 // ! [Interesting] 45 upm_nrf24l01.NRF24L01 comm = new upm_nrf24l01.NRF24L01((short) 7, (short) 8); 46 47 Callback callback = new ReceiverCallback(comm); 48 49 comm.setSourceAddress(local_address); 50 comm.setDestinationAddress(broadcast_address); 51 comm.setPayload((short) upm_nrf24l01.javaupm_nrf24l01Constants.MAX_BUFFER); 52 comm.configure(); 53 comm.setSpeedRate(upm_nrf24l01.speed_rate_t.NRF_250KBPS); 54 comm.setChannel((short) 99); 55 comm.setDataReceivedHandler(callback); 56 57 while (true) { 58 comm.pollListener(); 59 } 60 61 // ! [Interesting] 62 } 63 } 64 65 class ReceiverCallback extends Callback { 66 67 private upm_nrf24l01.NRF24L01 comm = null; 68 ReceiverCallback(upm_nrf24l01.NRF24L01 comm)69 public ReceiverCallback(upm_nrf24l01.NRF24L01 comm) { 70 super(); 71 this.comm = comm; 72 } run()73 public void run() { 74 if (comm != null) { 75 short[] rx_buffer = comm.getM_rxBuffer(); 76 System.out.print("Received: "); 77 for (int i = 0; i < rx_buffer.length; i++) 78 System.out.print(rx_buffer[i]); 79 System.out.println(); 80 } else { 81 System.out.println("No NRF24L01 instance given to callback"); 82 } 83 } 84 }