13762975eSMatthias Ringwald package com.bluekitchen; 23762975eSMatthias Ringwald 33762975eSMatthias Ringwald import com.bluekitchen.btstack.BD_ADDR; 43762975eSMatthias Ringwald import com.bluekitchen.btstack.BTstack; 53762975eSMatthias Ringwald import com.bluekitchen.btstack.Packet; 63762975eSMatthias Ringwald import com.bluekitchen.btstack.PacketHandler; 73762975eSMatthias Ringwald import com.bluekitchen.btstack.Util; 83762975eSMatthias Ringwald import com.bluekitchen.btstack.event.BTstackEventState; 93762975eSMatthias Ringwald import com.bluekitchen.btstack.event.GAPEventInquiryResult; 103762975eSMatthias Ringwald import com.bluekitchen.btstack.event.GAPEventInquiryComplete; 113762975eSMatthias Ringwald 123762975eSMatthias Ringwald import java.nio.charset.StandardCharsets; 133762975eSMatthias Ringwald 14fc7ba75fSMatthias Ringwald public class GAPInquiry implements PacketHandler { 153762975eSMatthias Ringwald 163762975eSMatthias Ringwald private enum STATE { 173762975eSMatthias Ringwald w4_btstack_working, w4_query_result, 183762975eSMatthias Ringwald }; 193762975eSMatthias Ringwald 203762975eSMatthias Ringwald private BTstack btstack; 213762975eSMatthias Ringwald private STATE state; 223762975eSMatthias Ringwald startInquiry()233762975eSMatthias Ringwald private void startInquiry(){ 243762975eSMatthias Ringwald state = STATE.w4_query_result; 253762975eSMatthias Ringwald int duration_in_1_28s = 5; 263762975eSMatthias Ringwald btstack.GAPInquiryStart(duration_in_1_28s); 273762975eSMatthias Ringwald } 283762975eSMatthias Ringwald handlePacket(Packet packet)293762975eSMatthias Ringwald public void handlePacket(Packet packet){ 303762975eSMatthias Ringwald switch (state){ 313762975eSMatthias Ringwald case w4_btstack_working: 323762975eSMatthias Ringwald if (packet instanceof BTstackEventState){ 333762975eSMatthias Ringwald BTstackEventState event = (BTstackEventState) packet; 343762975eSMatthias Ringwald if (event.getState() == 2) { 353762975eSMatthias Ringwald System.out.println("BTstack working. Start Inquiry"); 363762975eSMatthias Ringwald startInquiry(); 373762975eSMatthias Ringwald } 383762975eSMatthias Ringwald } 393762975eSMatthias Ringwald break; 403762975eSMatthias Ringwald 413762975eSMatthias Ringwald case w4_query_result: 423762975eSMatthias Ringwald if (packet instanceof GAPEventInquiryResult){ 433762975eSMatthias Ringwald GAPEventInquiryResult result = (GAPEventInquiryResult) packet; 443762975eSMatthias Ringwald String name = ""; 453762975eSMatthias Ringwald if (result.getNameAvailable() != 0){ 463762975eSMatthias Ringwald name = Util.getText(result.getName(), 0, result.getNameLen()); 473762975eSMatthias Ringwald System.out.println( "Device found " + result.getBdAddr() + ", name: '" + name + "'"); 483762975eSMatthias Ringwald } else { 493762975eSMatthias Ringwald System.out.println( "Device found " + result.getBdAddr()); 503762975eSMatthias Ringwald } 513762975eSMatthias Ringwald } 523762975eSMatthias Ringwald if (packet instanceof GAPEventInquiryComplete){ 533762975eSMatthias Ringwald // TODO: for devices without name, do a gap_remote_name_request 543762975eSMatthias Ringwald System.out.println("Inquiry done, restart"); 553762975eSMatthias Ringwald startInquiry(); 563762975eSMatthias Ringwald } 573762975eSMatthias Ringwald break; 583762975eSMatthias Ringwald default: 593762975eSMatthias Ringwald break; 603762975eSMatthias Ringwald } 613762975eSMatthias Ringwald } 623762975eSMatthias Ringwald test()633762975eSMatthias Ringwald void test(){ 643762975eSMatthias Ringwald System.out.println("Inquiry Test Application"); 653762975eSMatthias Ringwald 663762975eSMatthias Ringwald // connect to BTstack Daemon via default port on localhost 673762975eSMatthias Ringwald // start: src/BTdaemon --tcp 683762975eSMatthias Ringwald 693762975eSMatthias Ringwald btstack = new BTstack(); 703762975eSMatthias Ringwald btstack.setTcpPort(BTstack.DEFAULT_TCP_PORT); 713762975eSMatthias Ringwald btstack.registerPacketHandler(this); 723762975eSMatthias Ringwald boolean ok = btstack.connect(); 733762975eSMatthias Ringwald if (!ok) { 743762975eSMatthias Ringwald System.out.println("Failed to connect to BTstack Server"); 753762975eSMatthias Ringwald return; 763762975eSMatthias Ringwald } 773762975eSMatthias Ringwald 783762975eSMatthias Ringwald System.out.println("BTstackSetPowerMode(1)"); 793762975eSMatthias Ringwald 803762975eSMatthias Ringwald state = STATE.w4_btstack_working; 813762975eSMatthias Ringwald btstack.BTstackSetPowerMode(1); 823762975eSMatthias Ringwald } 833762975eSMatthias Ringwald main(String args[])843762975eSMatthias Ringwald public static void main(String args[]){ 85*43e99aeeSMatthias Ringwald new GAPInquiry().test(); 863762975eSMatthias Ringwald } 873762975eSMatthias Ringwald } 88