Пример #1
0
 /**
  * Create and open a Connection. This Implementation only supports the <code>btl2cap://</code>
  * protocol schema. If url begins with <code>btl2cap://localhost</code>, this method call will
  * return a <code>javax.bluetooth.L2CAPConnectionNotifier</code>.
  *
  * @param url The URL for the connection.
  * @return A new <code>javax.bluetooth.L2CAPConnection</code> or a <code>
  *     javax.bluetooth.L2CAPConnectionNotifier</code>
  * @throws IllegalArgumentException If a parameter is invalid.
  * @throws IOException If some other kind of I/O error occurs.
  * @see javax.bluetooth.L2CAPConnection
  * @see javax.bluetooth.L2CAPConnectionNotifier
  */
 public static Connection open(String url) throws IOException {
   try {
     if (url.startsWith("btl2cap://localhost:")) {
       int endIndex = url.indexOf(';');
       String psmString = url.substring(20, endIndex);
       Short psmShort = Short.decode(psmString);
       short psm = psmShort.shortValue();
       return new JSR82ConnectionNotifier(psm);
     }
     if (url.startsWith("btl2cap://")) {
       byte[] bdAddrBytes = new byte[12];
       String bdAddrString = url.substring(10, 22);
       Long bdAddrLong = Long.decode("0x" + bdAddrString);
       long remoteAddrLong = bdAddrLong.longValue();
       int endIndex = url.indexOf(';', 22);
       String psmString = url.substring(23, endIndex);
       Short psmShort = Short.decode(psmString);
       short psm = psmShort.shortValue();
       BluetoothStack bluetooth = BluetoothStack.getBluetoothStack();
       LocalDevice localDev = LocalDevice.getLocalDevice();
       DiscoveryAgent discovery = localDev.getDiscoveryAgent();
       RemoteDevice remoteDevice = discovery.getRemoteDevice(remoteAddrLong);
       if (remoteDevice != null) {
         JSR82Channel channel = new JSR82Channel();
         bluetooth.connectL2CAPChannel(channel, remoteDevice, psm);
         return channel;
       } else throw new IllegalArgumentException("Unable to locate Bluetooth Device.");
     }
   } catch (BluetoothStateException e) {
     throw new IOException("" + e);
   } catch (HCIException e) {
     throw new IOException("" + e);
   }
   throw new IllegalArgumentException(
       "This implementation of Connector only supports btl2cap:// Connections.");
 }
Пример #2
0
  public static final void main(String[] args) throws Exception {
    /*DataElement resultAttributes = new DataElement(DataElement.DATSEQ);
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,0));
    resultAttributes.addElement(new DataElement(DataElement.U_INT_4,0x0100));
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,1));
    DataElement serviceClassIDList = new DataElement(DataElement.DATSEQ);
    UUID serviceClassUUID = new UUID(0x1111);
    serviceClassIDList.addElement(new DataElement(DataElement.UUID,serviceClassUUID));
    resultAttributes.addElement(serviceClassIDList);
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,4));
    DataElement protocolDescriptorList = new DataElement(DataElement.DATSEQ);
    DataElement l2capDescriptor = new DataElement(DataElement.DATSEQ);
    l2capDescriptor.addElement(new DataElement(DataElement.UUID,new UUID(0x1111)));
    protocolDescriptorList.addElement(l2capDescriptor);
    resultAttributes.addElement(protocolDescriptorList);
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,5));
    DataElement browseClassIDList = new DataElement(DataElement.DATSEQ);
    UUID browseClassUUID = new UUID(0x1002);
    browseClassIDList.addElement(new DataElement(DataElement.UUID,browseClassUUID));
    resultAttributes.addElement(browseClassIDList);
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,6));
    DataElement languageBaseAttributeIDList = new DataElement(DataElement.DATSEQ);
    languageBaseAttributeIDList.addElement(new DataElement(DataElement.U_INT_2,25966));
    languageBaseAttributeIDList.addElement(new DataElement(DataElement.U_INT_2,106));
    languageBaseAttributeIDList.addElement(new DataElement(DataElement.U_INT_2,256));
    resultAttributes.addElement(languageBaseAttributeIDList);
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,9));
    DataElement profileDescriptorList = new DataElement(DataElement.DATSEQ);
    DataElement profileDescriptor = new DataElement(DataElement.DATSEQ);
    profileDescriptor.addElement(new DataElement(DataElement.UUID,new UUID(0x1111)));
    profileDescriptor.addElement(new DataElement(DataElement.U_INT_2,256));
    profileDescriptorList.addElement(profileDescriptor);
    resultAttributes.addElement(profileDescriptorList);
    resultAttributes.addElement(new DataElement(DataElement.U_INT_2,256));
    resultAttributes.addElement(new DataElement(DataElement.STRING,"Tini Service"));
    System.out.println("Result Attributes="+resultAttributes);
    byte[] attrBytes = resultAttributes.toByteArray();
    Debug.println(6,"Result Bytes",attrBytes);
    DataElement reparsed = new DataElement(attrBytes);
    System.out.println("Reparsed Attributes="+reparsed);
    */

    BluetoothStack.init(new BluetoothTCPClient("192.168.10.2", 2600));
    BluetoothStack bluetooth = BluetoothStack.getBluetoothStack();
    bluetooth.send_HCI_HC_Change_Local_Name("TINI BLUE");
    bluetooth.send_HCI_HC_Write_Event_Filter_Connection_Setup((byte) 0x02);
    bluetooth.send_HCI_HC_Write_Event_Filter_Inquiry_Result();
    bluetooth.send_HCI_HC_Write_Scan_Enable((byte) 0x03);
    BluetoothClient blue = new BluetoothClient();
    while (remoteDevice == null) {
      Thread.sleep(1000);
    }
    if (remoteDevice != null) { // System.out.println("bdAddr:"+remoteDevice.getBluetoothAddress());
      // System.out.println("Name:"+remoteDevice.getFriendlyName(false));
      // System.out.println("major dev class :"+remoteDevice.deviceClass.getMajorDeviceClass());
      // System.out.println("minor dev class :"+remoteDevice.deviceClass.getMinorDeviceClass());
      // System.out.println("service classes :"+remoteDevice.deviceClass.getServiceClasses());
      SDPClientChannel sdpChannel = new SDPClientChannel(remoteDevice, blue);
      bluetooth.connectL2CAPChannel(sdpChannel, remoteDevice, (short) 0x0001);
      byte[] uuidList = {0x35, 0x03, 0x19, 0x10, 0x02};
      DataElement uuidListElement = new DataElement(uuidList);
      sdpChannel.send_SDP_ServiceSearchRequest((short) 1, (short) 14, uuidListElement);
    }
    while (serviceRecords == null) {
      Thread.sleep(1000);
    }
    for (int i = 0; i < serviceRecords.length; i++) {
      System.out.println("	" + serviceRecords[i]);
      int[] attrIDs = {
        0xff00
      }; // reversed the values for range... this avoids ranges starting with 00 to be tructated.
      try {
        serviceRecords[i].populateRecord(attrIDs);
      } catch (IOException e) {
        System.out.print("ServiceRecord.populateRecord(attrIDs) failed. " + e);
      }
    }
  }