Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    problema connessione bluetooth

    Ciao a tutti, da qualche giorno sto cercando di compilare un programmino che consenta la comunicazione e lo scambio di file tra un PC server e dei dispositivi bluetooth posti nelle vicinanze. In particolare, uso come device un Nokia N82, l'applicazione riconosce perfettamente tale dispositivo e i servizi che offre, ma non riesce ad effettuare la put operation ovvero il trasferimento del file, si blocca l'esecuzione come se stesse in attesa di qualcosa, non ottengo nessun errore.
    Grazie anticipatamente.

  2. #2

    codice relativo

    Scusate, ho dimenticato il codice:
    import javax.microedition.io.Connection;
    import javax.microedition.io.Connector;
    import javax.obex.ClientSession;
    import javax.obex.HeaderSet;
    import javax.obex.Operation;


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.OutputStream;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Vector;
    import javax.bluetooth.DeviceClass;
    import javax.bluetooth.DiscoveryAgent;
    import javax.bluetooth.DiscoveryListener;
    import javax.bluetooth.LocalDevice;
    import javax.bluetooth.RemoteDevice;
    import javax.bluetooth.ServiceRecord;
    import javax.bluetooth.UUID;
    /**
    *
    * Class that discovers all bluetooth devices in the neighbourhood,
    *
    * Connects to the chosen device and checks for the presence of OBEX push service in it.
    * and displays their name and bluetooth address.
    *
    *
    */

    public class BluetoothServiceDiscovery4 implements DiscoveryListener{

    //object used for waiting
    private static Object lock=new Object();
    //vector containing the devices discovered
    private static Vector vecDevices=new Vector();
    private static String connectionURL=null;
    /**
    * Entry point.
    */
    public static void main(String[] args) throws IOException {

    BluetoothServiceDiscovery4 bluetoothServiceDiscovery=new BluetoothServiceDiscovery4();
    //display local device address and name
    LocalDevice localDevice = LocalDevice.getLocalDevice();
    System.out.println("Address: "+localDevice.getBluetoothAddress());
    System.out.println("Name: "+localDevice.getFriendlyName());
    //find devices
    DiscoveryAgent agent = localDevice.getDiscoveryAgent();
    System.out.println("Starting device inquiry...");
    agent.startInquiry(DiscoveryAgent.GIAC, bluetoothServiceDiscovery);
    try {
    synchronized(lock){
    lock.wait();
    }
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("Device Inquiry Completed. ");
    //print all devices in vecDevices
    int deviceCount=vecDevices.size();
    if(deviceCount <= 0){
    System.out.println("No Devices Found .");
    }
    else{

    //print bluetooth device addresses and names in the format [ No. address (name) ]
    System.out.println("Bluetooth Devices: ");
    for (int i = 0; i <deviceCount; i++) {
    RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i) ;
    System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(false)+")");
    }
    }

    System.out.print("Choose the device to search for Obex Push service : ");
    BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
    String chosenIndex=bReader.readLine();
    int index=Integer.parseInt(chosenIndex.trim());

    //check for obex service
    RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(in dex-1);
    UUID[] uuidSet = new UUID[1];
    uuidSet[0]=new UUID("1105",true);
    System.out.println("\nSearching for service...");
    agent.searchServices(null,uuidSet,remoteDevice,blu etoothServiceDiscovery);
    try {
    synchronized(lock){
    lock.wait();
    }
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
    if(connectionURL==null){

    System.out.println("Device does not support Object Push.");
    }
    else{
    try {
    String contentType = null;
    File file = new File("C:/send.jpg");
    ClientSession cs;
    cs = (ClientSession) Connector.open(connectionURL);
    HeaderSet hs;
    hs = cs.connect( cs.createHeaderSet() );
    hs.setHeader (HeaderSet.NAME, file);
    // Il content type dipende dal formato del file da spedire:
    // image/jpeg; audio/mp3; etc.
    hs.setHeader (HeaderSet.TYPE, "image/jpeg");
    hs.setHeader (HeaderSet.LENGTH, new Long(file.length()));
    Operation po = cs.put(hs);
    OutputStream os = po.openOutputStream();
    /* Lettura del file */
    FileInputStream fin = new FileInputStream(file);
    byte[] inBuffer = new byte[1024];
    int bytesRead = -1;
    /* Scrittura dell’input stream del file sull’output stream della connessione */
    while ((bytesRead = fin.read(inBuffer)) != -1)
    os.write(inBuffer, 0, bytesRead);
    os.flush();
    os.close();
    po.close();
    cs.disconnect(null);
    cs.close();
    fin.close();
    } catch (Exception e){
    System.out.println("Si è verificato il seguente errore: " + e.getMessage());
    }
    }
    }

    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    //add the device to the vector
    if(!vecDevices.contains(btDevice)){
    vecDevices.addElement(btDevice);
    }
    }
    /**

    * Called when a bluetooth service is discovered.

    * Used for service search.

    */

    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    if(servRecord!=null && servRecord.length>0){
    connectionURL=servRecord[0].getConnectionURL(0,false);
    }
    synchronized(lock){
    lock.notify();
    }
    }
    /**

    * Called when the service search is over.

    */

    public void serviceSearchCompleted(int transID, int respCode) {
    synchronized(lock){
    lock.notify();
    }
    }
    /**

    * Called when the device search is over.
    */
    public void inquiryCompleted(int discType) {
    synchronized(lock){
    lock.notify();
    }
    }//end method
    }//end class

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.