From b708eb2d59c45eadf897b6564f101ffffb73b0ad Mon Sep 17 00:00:00 2001 From: "mila@ringwald.ch" Date: Thu, 4 Dec 2014 16:22:19 +0000 Subject: [PATCH] basic spp client --- .../com/bluekitchen/SPPClientTest.java | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 java/example/com/bluekitchen/SPPClientTest.java diff --git a/java/example/com/bluekitchen/SPPClientTest.java b/java/example/com/bluekitchen/SPPClientTest.java new file mode 100644 index 000000000..8e7f85cd8 --- /dev/null +++ b/java/example/com/bluekitchen/SPPClientTest.java @@ -0,0 +1,153 @@ +package com.bluekitchen; + +import java.util.ArrayList; +import java.util.List; + +import com.bluekitchen.btstack.BD_ADDR; +import com.bluekitchen.btstack.BT_UUID; +import com.bluekitchen.btstack.BTstack; +import com.bluekitchen.btstack.Packet; +import com.bluekitchen.btstack.PacketHandler; +import com.bluekitchen.btstack.Util; +import com.bluekitchen.btstack.event.BTstackEventState; +import com.bluekitchen.btstack.event.HCIEventDisconnectionComplete; +import com.bluekitchen.btstack.event.RFCOMMEventOpenChannelComplete; +import com.bluekitchen.btstack.event.SDPQueryComplete; +import com.bluekitchen.btstack.event.SDPQueryRFCOMMService; + +public class SPPClientTest implements PacketHandler { + + private enum STATE { + w4_btstack_working, w4_query_result, w4_connected, active + }; + + private BTstack btstack; + private STATE state; + private int testHandle; + + private BD_ADDR remote = new BD_ADDR("84:38:35:65:D1:15"); + private int rfcommServiceUUID = 0x1002; + private int btIncomingChannelNr = 3; + + private int rfcommChannelID = 0; + private int mtu = 0; + + List services = new ArrayList(10); + private int counter; + // List service_name = new ArrayList(10); + + private BT_UUID uuid128(byte[] att_uuid) { + byte [] uuid = new byte[16]; + Util.flipX(att_uuid, uuid); + return new BT_UUID(uuid); + } + + public void handlePacket(Packet packet){ +// System.out.println(packet.toString()); + if (packet instanceof HCIEventDisconnectionComplete){ + HCIEventDisconnectionComplete event = (HCIEventDisconnectionComplete) packet; + testHandle = event.getConnectionHandle(); + System.out.println(String.format("Received disconnect, status %d, handle %x", event.getStatus(), testHandle)); + return; + } + + switch (state){ + case w4_btstack_working: + if (packet instanceof BTstackEventState){ + BTstackEventState event = (BTstackEventState) packet; + if (event.getState() == 2) { + System.out.println("BTstack working. Start SDP inquiry."); + state = STATE.w4_query_result; + byte[] serviceSearchPattern = Util.serviceSearchPatternForUUID16(rfcommServiceUUID); + +// StringBuilder sb = new StringBuilder(); +// for (byte b:serviceSearchPattern){ +// sb.append(String.format("%02X ", b)); +// } +// System.out.println(sb.toString()); + btstack.SDPClientQueryRFCOMMServices(remote, serviceSearchPattern); + } + } + + break; + + case w4_query_result: + if (packet instanceof SDPQueryRFCOMMService){ + SDPQueryRFCOMMService service = (SDPQueryRFCOMMService) packet; + services.add(service.getRFCOMMChannel()); + } + if (packet instanceof SDPQueryComplete){ + for (Integer channel_nr : services){ + System.out.println("Found rfcomm channel nr: " + channel_nr); + if (channel_nr == btIncomingChannelNr){ + state = STATE.w4_connected; + System.out.println("Connect to channel nr " + channel_nr); + btstack.RFCOMMCreateChannel(remote, 3); + } + } + + } + break; + + case w4_connected: + if (packet instanceof RFCOMMEventOpenChannelComplete){ + RFCOMMEventOpenChannelComplete e = (RFCOMMEventOpenChannelComplete) packet; + System.out.println("RFCOMMEventOpenChannelComplete with status " + e.getStatus()); + if (e.getStatus() != 0) { + System.out.println("RFCOMM channel open failed, status " + e.getStatus()); + } else { + state = STATE.active; + rfcommChannelID = e.getRFCOMMCid(); + mtu = e.getMaxFrameSize(); + System.out.println(String.format("RFCOMM channel open succeeded. New RFCOMM Channel ID %d, max frame size %d", rfcommChannelID, mtu)); + + counter = 0; + new Thread(new Runnable(){ + @Override + public void run() { + try { + while(state == STATE.active){ + Thread.sleep(1000); + byte [] data = String.format("BTstack SPP Counter %d\n", counter).getBytes(); + btstack.RFCOMMSendData(rfcommChannelID, data); + } + } catch (InterruptedException e) {} + } + }).start(); + } + } + break; + case active: + if (packet instanceof RFCOMMDataPacket){ + System.out.println("Received RFCOMM data packet: " + packet.toString()); + } + default: + break; + } + } + + void test(){ + System.out.println("SPP Test Application"); + + // connect to BTstack Daemon via default port on localhost + // start: src/BTdaemon --tcp + + btstack = new BTstack(); + btstack.setTcpPort(BTstack.DEFAULT_TCP_PORT); + btstack.registerPacketHandler(this); + boolean ok = btstack.connect(); + if (!ok) { + System.out.println("Failed to connect to BTstack Server"); + return; + } + + System.out.println("BTstackSetPowerMode(1)"); + + state = STATE.w4_btstack_working; + btstack.BTstackSetPowerMode(1); + } + + public static void main(String args[]){ + new SPPClientTest().test(); + } +}