Example Peripheral of emulating a vending machine

import java.io.IOException;
import java.util.ArrayList;

import com.fazecast.jSerialComm.SerialPort;

import cybervelia.sdk.controller.BLECharacteristic;
import cybervelia.sdk.controller.BLEService;
import cybervelia.sdk.controller.pe.AdvertisementData;
import cybervelia.sdk.controller.pe.AuthorizedData;
import cybervelia.sdk.controller.pe.PEBLEDeviceCallbackHandler;
import cybervelia.sdk.controller.pe.PEConnectionParameters;
import cybervelia.sdk.controller.pe.PEController;
import cybervelia.sdk.controller.pe.callbacks.PEConnectionCallback;
import cybervelia.sdk.controller.pe.callbacks.PEReadCallback;
import cybervelia.sdk.controller.pe.callbacks.PEWriteEventCallback;
import cybervelia.sdk.types.BLEAttributePermission;
import cybervelia.sdk.types.ConnectionTypesCommon;
import cybervelia.sdk.types.ConnectionTypesCommon.AddressType;
import cybervelia.sdk.types.ConnectionTypesCommon.BITAddressType;
import cybervelia.server.CryptoHelper;

public class VirtualVendingMachine {

    static boolean debugging = false;

    public static void main(String ...args) throws IOException {
        String port = findPorts()[0];

        /** Step 1: Create the BLE:Bit Peripheral Object **/
        // Create the Peripheral Object
        PEBLEDeviceCallbackHandler handler = createCallbackHandler();
        PEController pe = new PEController(port, handler);

        // Check if this is a BLEbit PE
        if (!pe.isInitializedCorrectly()) {
            System.err.println("Not a blebit peripheral");
            System.exit(1);
        }

        /** Step 2: Initialize the B:E:Bit device **/
        // Initialize connection parameters
        PEConnectionParameters con_params = new PEConnectionParameters();
        con_params.setMinConnectionIntervalMS(50);
        con_params.setMaxConnectionIntervalMS(60);
        pe.sendConnectionParameters(con_params);
        // Set bluetooth address
        pe.sendBluetoothDeviceAddress("ea:bb:cc:11:33:12", BITAddressType.PUBLIC);

        /** Step 3: Set the service & the characteristics **/
        BLEService service = new BLEService("83670001-d111-3a63-e0da-fe66000001e9");

        BLECharacteristic rx = new BLECharacteristic("83670002-d111-3a63-e0da-fe66000001e9", new byte[] {0});
        rx.setAttributePermissions(BLEAttributePermission.OPEN, BLEAttributePermission.OPEN);
        rx.setMaxValueLength(27);
        rx.setValueLengthVariable(true);
        rx.enableRead();
        rx.enableWrite();
        rx.enableWriteCMD();

        BLECharacteristic tx = new BLECharacteristic("83670003-d111-3a63-e0da-fe66000001e9", new byte[] {0});
        tx.setAttributePermissions(BLEAttributePermission.OPEN, BLEAttributePermission.OPEN);
        tx.setMaxValueLength(27);
        tx.setValueLengthVariable(true);
        tx.enableRead();
        tx.enableWrite();
        tx.enableWriteCMD();
        tx.enableNotification();

        service.addCharacteristic(rx);
        service.addCharacteristic(tx);

        pe.sendBLEService(service);

        /** Step 4: Set the advertisement data **/
        pe.sendDeviceName("my-device-name");
        AdvertisementData adv_data = new AdvertisementData();
        adv_data.setFlags(AdvertisementData.FLAG_LE_GENERAL_DISCOVERABLE_MODE | AdvertisementData.FLAG_ER_BDR_NOT_SUPPORTED);
        adv_data.includeDeviceName();
        pe.sendAdvertisementData(adv_data);

        /** Step 5: Configure pairing method **/
        pe.configurePairing(ConnectionTypesCommon.PairingMethods.NO_IO);

        /** Step 6: finish setup and start advertising **/
        pe.finishSetup();

    }



    /* Install event callbacks */
    private static PEBLEDeviceCallbackHandler createCallbackHandler() {
        PEBLEDeviceCallbackHandler handler = new PEBLEDeviceCallbackHandler();

        handler.installReadCallback(new PEReadCallback() {

            @Override
            public boolean authorizeRead(BLECharacteristic characteristic, byte[] data, int data_len,
                    AuthorizedData authorized_Data) {
                return false;
            }

            @Override
            public void readEvent(BLECharacteristic characteristic, byte[] data, int data_len) {
                System.out.println("[!R] " + characteristic.getUUID().toString() + ": " + CryptoHelper.bytesToSpacedHex(data));
            }

        });

        handler.installWriteCallback(new PEWriteEventCallback() {

            @Override
            public void writeEvent(BLECharacteristic characteristic, byte[] data, int data_size, boolean is_cmd,
                    short handle) {
                System.out.println("[!W] " + characteristic.getUUID().toString() + ": " + CryptoHelper.bytesToSpacedHex(data));
            }

        });

        handler.installConnectionCallback(new PEConnectionCallback() {

            @Override
            public void disconnected(int reason) {
                System.out.println("Disconnected");
            }

            @Override
            public void connected(AddressType address_type, String address) {
                System.out.println("Connected: " + address);
            }

        });

        return handler;
    }



    /* Identify BLE:Bit devices */
    private static String[] findPorts() {
        ArrayList<String> portsFound = new ArrayList<String>();

        SerialPort[] sp = SerialPort.getCommPorts();
        for(SerialPort s : sp)
        {
            if (!debugging && (s.getDescriptivePortName().toLowerCase().contains("cp210x") && System.getProperty("os.name").startsWith("Windows")))
                portsFound.add(s.getSystemPortName());
            else if (!debugging && (s.getDescriptivePortName().toLowerCase().contains("cp210x") && System.getProperty("os.name").startsWith("Linux")))
                portsFound.add(s.getSystemPortName());
            else if (debugging  && System.getProperty("os.name").startsWith("Windows") && (s.getDescriptivePortName().contains("Prolific") || s.getDescriptivePortName().contains("USB Serial Port")))
                portsFound.add(s.getSystemPortName());
            else if (debugging  && System.getProperty("os.name").startsWith("Linux") && (s.getDescriptivePortName().contains("pl2303") || s.getDescriptivePortName().contains("ftdi_sio")))
                portsFound.add(s.getSystemPortName());
        }

        String[] ports = new String[portsFound.size()];
        for(int i = 0; i<portsFound.size(); ++i)
        {
            ports[i] = portsFound.get(i);
            System.out.println("ADDED: " + ports[i]);
        }

        return ports;
    }
}