Ejemplo n.º 1
0
  void test() {
    counter = 0;
    addMessage("SPP Test Application");

    btstack = new BTstack();
    btstack.registerPacketHandler(this);

    boolean ok = btstack.connect();
    if (!ok) {
      addMessage("Failed to connect to BTstack Server");
      return;
    }

    addMessage("BTstackSetPowerMode(1)");

    state = STATE.w4_btstack_working;
    btstack.BTstackSetPowerMode(1);
  }
Ejemplo n.º 2
0
 private void restartInquiry() {
   clearMessages();
   addMessage("Restart Inquiry");
   state = STATE.w4_scan_result;
   services.clear();
   devices.clear();
   counter = 0;
   btstack.HCIInquiry(HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
 }
Ejemplo n.º 3
0
  @SuppressLint("DefaultLocale")
  public void handlePacket(Packet packet) {
    if (packet instanceof HCIEventHardwareError) {
      clearMessages();
      addMessage(
          "Received HCIEventHardwareError, \nhandle power cycle of the Bluetooth \nchip of the device.");
    }

    if (packet instanceof HCIEventDisconnectionComplete) {
      HCIEventDisconnectionComplete event = (HCIEventDisconnectionComplete) packet;
      testHandle = event.getConnectionHandle();
      addMessage(
          String.format(
              "Received disconnect, status %d, handle %x", event.getStatus(), testHandle));
      restartInquiry();
      return;
    }

    switch (state) {
      case w4_btstack_working:
        if (packet instanceof BTstackEventState) {
          BTstackEventState event = (BTstackEventState) packet;
          if (event.getState() == 2) {
            addMessage("BTstack working. Set write inquiry mode.");
            state = STATE.w4_write_inquiry_mode;
            btstack.HCIWriteInquiryMode(1); // with RSSI
          }
        }
        break;

      case w4_write_inquiry_mode:
        if (packet instanceof HCIEventCommandComplete) {
          state = STATE.w4_scan_result;
          btstack.HCIInquiry(HCI_INQUIRY_LAP, INQUIRY_INTERVAL, 0);
        }
        break;

      case w4_scan_result:
        if (packet instanceof HCIEventInquiryResultWithRssi) {
          HCIEventInquiryResultWithRssi result = (HCIEventInquiryResultWithRssi) packet;
          devices.add(
              new RemoteDevice(
                  result.getBdAddr(), result.getPageScanRepetitionMode(), result.getClockOffset()));
          addMessage("Found device: " + result.getBdAddr());
        }

        if (packet instanceof HCIEventInquiryComplete) {
          state = STATE.w4_remote_name;
          if (hasMoreRemoteNameRequests()) {
            doNextRemoteNameRequest();
            break;
          }
        }
        break;

      case w4_remote_name:
        if (packet instanceof HCIEventRemoteNameRequestComplete) {
          HCIEventRemoteNameRequestComplete result = (HCIEventRemoteNameRequestComplete) packet;
          if (result.getStatus() == 0) {
            // store name on success
            setNameForDeviceWithBDAddr(result.getRemoteName(), result.getBdAddr());
          }

          if (hasMoreRemoteNameRequests()) {
            doNextRemoteNameRequest();
            break;
          }

          // discovery done, connect to device with remote name prefix

          RemoteDevice remoteDevice = null;
          for (RemoteDevice device : devices) {
            if (device.getName() != null
                && device.getName().startsWith(REMOTE_DEVICE_NAME_PREFIX)) {
              remoteDevice = device;
            }
          }

          // try first one otherwise
          if (remoteDevice == null && devices.size() > 0) {
            remoteDevice = devices.get(0);
          }

          // no device, restart inquiry
          if (remoteDevice == null) {
            restartInquiry();
            break;
          }

          // start SDP query for RFCOMMM services
          remoteBDAddr = remoteDevice.getBDAddress();
          if (remoteDevice.getName() == null) {
            addMessage("Start SDP Query of " + remoteDevice.getBDAddress());
          } else {
            addMessage("Start SDP Query of " + remoteDevice.getName());
          }
          state = STATE.w4_sdp_query_result;
          byte[] serviceSearchPattern = Util.serviceSearchPatternForUUID16(SPP_UUID);
          btstack.SDPClientQueryRFCOMMServices(remoteBDAddr, serviceSearchPattern);
          break;
        }
        break;

      case w4_sdp_query_result:
        if (packet instanceof SDPQueryRFCOMMService) {
          SDPQueryRFCOMMService service = (SDPQueryRFCOMMService) packet;
          services.add(service);
          addMessage(
              String.format(
                  "Found \"%s\", channel %d", service.getName(), service.getRFCOMMChannel()));
        }
        if (packet instanceof SDPQueryComplete) {
          // find service with "SPP" prefix
          SDPQueryRFCOMMService selectedService = null;
          for (SDPQueryRFCOMMService service : services) {
            if (service.getName().startsWith(RFCOMM_SERVICE_PREFIX)) {
              selectedService = service;
              break;
            }
          }
          // restart demo, if no service with prefix found
          if (selectedService == null) {
            restartInquiry();
            break;
          }

          // connect
          state = STATE.w4_connected;
          clearMessages();
          addMessage("SPP Test Application / Part 2");
          addMessage("Connect to channel nr " + selectedService.getRFCOMMChannel());
          btstack.RFCOMMCreateChannel(remoteBDAddr, selectedService.getRFCOMMChannel());
        }

        break;

      case w4_connected:
        if (packet instanceof RFCOMMEventOpenChannelComplete) {
          RFCOMMEventOpenChannelComplete e = (RFCOMMEventOpenChannelComplete) packet;
          if (e.getStatus() != 0) {
            addMessage("RFCOMM channel open failed, status " + e.getStatus());
          } else {
            state = STATE.active;
            rfcommChannelID = e.getRFCOMMCid();
            mtu = e.getMaxFrameSize();
            addMessage(
                String.format(
                    "RFCOMM channel open succeeded. \nNew RFCOMM Channel ID %d,\n 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;
                            try {
                              data =
                                  String.format("BTstack SPP Counter %d\n", counter)
                                      .getBytes("utf8");
                              if (counter < Integer.MAX_VALUE) {
                                counter++;
                              } else {
                                counter = 0;
                              }
                              btstack.RFCOMMSendData(rfcommChannelID, data);
                            } catch (UnsupportedEncodingException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                            }
                          }
                        } catch (InterruptedException e) {
                        }
                      }
                    })
                .start();
          }
        }
        break;

      case active:
        if (packet instanceof RFCOMMDataPacket) {
          addTempMessage("Received RFCOMM data packet: \n" + packet.toString());
        }
        break;

      default:
        break;
    }
  }
Ejemplo n.º 4
0
 public void inquireName(BTstack btstack) {
   this.state = NAME_STATE.REMOTE_NAME_INQUIRED;
   btstack.HCIRemoteNameRequest(bdAddr, pageScanRepetitionMode, 0, clockOffset);
 }