Java Interface rules

Let’s start with a short Java question: Bellow you can see the interface ‘Test’. Which lines in that interface will be rejected by the compiler? The answer is: lines: 7,8,9,10 I am sure that even many of the experienced java developers will not have a 100% success answering this question because it can be confusing. 1, 2, 3 and 4 are actually all the same – only constants are allowed and by default they are. For that reason, 10 is not allowed. 5, 6 are the same – only public and protected methods are allowed. By default they are public. In short these are the rules for interfaces: Member variables Can be only public and are by default. By default are static and always static By default are final and always final Methods Can be only public and are by default. Can NOT be static Can Not be Final

Weekend project: Bluetooth multisender

In my last post I have shown you how to communicate with Bluetooth devices using java + Bluecove.jar. Using this knowledge I have created this small java weekend project – Bluetooth multisender. This program will scan for Bluetooth devices in the area. For each Bluetooth device found, it will check whether it can receives text messages. Each supported device will be added to the list on the left. All you need to do is selected device/s, write a message and press the send button. The message will be sent to all selected devices at once. Yeh, I know it’s not the nicest looking gui there is, but it’s working 🙂 download project View on github

Connecting to Bluetooth devices with Java

In this post I will show you how to use java to connect to Bluetooth devices. To do that, I will use bluecove. Bluecove is a JSR-82 implementation. JSR-82 is a java specification for defining APIs for communicating with Bluetooth devices. To use bluecove you will have to download bluecove jar. You can download it from here. Of course, you will also need a bluetooth device connected to your computer and enabled. Discovering Bluetooth devices To discover devices we need to follow these steps: 1. Get the our local BT device using blucove code. 2. Get the discovery agent from our device. 3. Start a query to search remote Bluetooth devices. Notice that the inquiry is an asynchronous function. This is why we use the lock.wait(). Notice also that we give an object named MyDiscoveryListener as a parameter to the inquiry. This listener will be executed when a device is found and when the inquiry is finished. The functions deviceDiscovered() and inquiryCompleted() are the ones exeuted when a device is found and when the inquiry is finished. The other 2 functions servicesDiscovered and serviceSearchCompleted will be used in the next section. Inquiring a device for a service Now that we Continue reading Connecting to Bluetooth devices with Java

Create your own Java customized annotations

Annotations is a relatively new technology, it was added to the language in Java5. An annotation is a meta data that can be attached to a java class, member, function or a parameter. It gives information about the element that is annotated. An example is the Deprecated annotation that specifies this function is old and better not used. Java5 comes with a set of annotations but you can also can create your own. In this post I will show you how to create your own annotations, use them and also refer to them during runtime. Steps for creating customized annotations: 1. Creating the annotation interface First you create the annotation interface which has specific structure. In this example the name of the annotation is ‘InvokeMultiple’ and it has 1 field ‘numberOfTimesToInvoke’. Meta-Annotations Meta annotations are actually annotations being used by annotations. @Target – specifies the type pf element this annotation is attached to. ElementType.TYPE-can be applied to any element of a class ElementType.FIELD-can be applied to a field or property ElementType.METHOD-can be applied to a method level annotation ElementType.PARAMETER-can be applied to the parameters of a method ElementType.CONSTRUCTOR-can be applied to constructors ElementType.LOCAL_VARIABLE-can be applied to local variables ElementType.ANNOTATION_TYPE-indicates that Continue reading Create your own Java customized annotations