org.symbian.tools.mtw.ui/src/org/symbian/tools/mtw/ui/deployment/bluetooth/BluetoothTargetType.java
changeset 461 7a8f9fa8d278
parent 460 c0bff5ed874c
child 462 cdc4995b1677
equal deleted inserted replaced
460:c0bff5ed874c 461:7a8f9fa8d278
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.mtw.ui.deployment.bluetooth;
       
    20 
       
    21 import java.io.IOException;
       
    22 import java.io.PrintStream;
       
    23 import java.util.Collection;
       
    24 import java.util.Map;
       
    25 import java.util.TreeMap;
       
    26 
       
    27 import javax.bluetooth.BluetoothStateException;
       
    28 import javax.bluetooth.DeviceClass;
       
    29 import javax.bluetooth.DiscoveryAgent;
       
    30 import javax.bluetooth.DiscoveryListener;
       
    31 import javax.bluetooth.LocalDevice;
       
    32 import javax.bluetooth.RemoteDevice;
       
    33 import javax.bluetooth.ServiceRecord;
       
    34 
       
    35 import org.eclipse.core.runtime.CoreException;
       
    36 import org.eclipse.core.runtime.IProgressMonitor;
       
    37 import org.eclipse.core.runtime.IStatus;
       
    38 import org.eclipse.core.runtime.Status;
       
    39 import org.eclipse.core.runtime.jobs.ISchedulingRule;
       
    40 import org.symbian.tools.mtw.core.projects.IMTWProject;
       
    41 import org.symbian.tools.mtw.ui.ConsoleFactory;
       
    42 import org.symbian.tools.mtw.ui.MTWCoreUI;
       
    43 import org.symbian.tools.mtw.ui.deployment.IDeploymentTarget;
       
    44 import org.symbian.tools.mtw.ui.deployment.IDeploymentTargetType;
       
    45 
       
    46 import com.intel.bluetooth.BlueCoveImpl;
       
    47 
       
    48 /**
       
    49  * Discovers Bluetooth-enabled devices. This code is generic and will not 
       
    50  * perform any checks if the discovered device can run application being 
       
    51  * deployed.
       
    52  * 
       
    53  * @author Eugene Ostroukhov (eugeneo@symbian.org)
       
    54  */
       
    55 public class BluetoothTargetType implements IDeploymentTargetType {
       
    56     private final class WrtDiscoveryListener implements DiscoveryListener {
       
    57         final Object inquiryCompletedEvent;
       
    58         boolean isCanceled;
       
    59         final Map<String, BluetoothTarget> prevTargets;
       
    60         final IProgressMonitor progressMonitor;
       
    61 
       
    62         private WrtDiscoveryListener(Map<String, BluetoothTarget> previousTargets, Object inquiryCompletedEvent,
       
    63                 IProgressMonitor progressMonitor) {
       
    64             this.prevTargets = previousTargets;
       
    65             this.inquiryCompletedEvent = inquiryCompletedEvent;
       
    66             this.progressMonitor = progressMonitor;
       
    67         }
       
    68 
       
    69         private void checkCanceled() {
       
    70             if (!isCanceled && progressMonitor != null) {
       
    71                 if (progressMonitor.isCanceled()) {
       
    72                     try {
       
    73                         LocalDevice.getLocalDevice().getDiscoveryAgent().cancelInquiry(listener);
       
    74                     } catch (BluetoothStateException e) {
       
    75                         MTWCoreUI.log(e);
       
    76                     }
       
    77                 }
       
    78             }
       
    79         }
       
    80 
       
    81         public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
       
    82             try {
       
    83                 final String name = btDevice.getFriendlyName(false);
       
    84                 if (name != null && name.length() > 0) {
       
    85                     final BluetoothTarget target = prevTargets.get(name);
       
    86                     if (target != null) {
       
    87                         target.setAddress(btDevice);
       
    88                         targets.put(name, target);
       
    89                     } else {
       
    90                         targets.put(name, new BluetoothTarget(name, btDevice, BluetoothTargetType.this));
       
    91                     }
       
    92                     checkCanceled();
       
    93                 }
       
    94             } catch (BluetoothStateException e) {
       
    95                 MTWCoreUI.log(e.getMessage(), e);
       
    96             } catch (IOException e) {
       
    97                 MTWCoreUI.log(e.getMessage(), e);
       
    98             }
       
    99         }
       
   100 
       
   101         public void inquiryCompleted(int discType) {
       
   102             synchronized (inquiryCompletedEvent) {
       
   103                 inquiryCompletedEvent.notifyAll();
       
   104             }
       
   105         }
       
   106 
       
   107         public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
       
   108         }
       
   109 
       
   110         public void serviceSearchCompleted(int transID, int respCode) {
       
   111         }
       
   112     }
       
   113 
       
   114     private static PrintStream savedSysOut;
       
   115     private boolean discovered = false;
       
   116     private WrtDiscoveryListener listener;
       
   117     private Map<String, BluetoothTarget> targets = new TreeMap<String, BluetoothTarget>();
       
   118 
       
   119     public BluetoothTargetType() {
       
   120         // set parameters for BlueCove
       
   121         String param = Integer.toString(65 * 1024);
       
   122         System.setProperty("bluecove.obex.mtu", param);
       
   123         BlueCoveImpl.setConfigProperty("bluecove.obex.mtu", param);
       
   124     }
       
   125 
       
   126     public void discoverTargets(IProgressMonitor monitor) throws CoreException {
       
   127         if (!isBloothToothConnected()) {
       
   128             throw new CoreException(new Status(IStatus.ERROR, MTWCoreUI.PLUGIN_ID, "Bluetooth is not available"));
       
   129         }
       
   130         monitor.beginTask("Discovering Bluetooth devices", IProgressMonitor.UNKNOWN);
       
   131         final Object inquiryCompletedEvent = new Object();
       
   132         final Map<String, BluetoothTarget> previousTargets = targets;
       
   133         targets = new TreeMap<String, BluetoothTarget>();
       
   134 
       
   135         listener = new WrtDiscoveryListener(previousTargets, inquiryCompletedEvent, monitor);
       
   136 
       
   137         synchronized (inquiryCompletedEvent) {
       
   138             boolean started;
       
   139             try {
       
   140                 started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
       
   141                 if (started) {
       
   142                     inquiryCompletedEvent.wait();
       
   143                     discovered = true;
       
   144                 }
       
   145             } catch (BluetoothStateException e) {
       
   146                 MTWCoreUI.log(e.getMessage(), e);
       
   147             } catch (InterruptedException e) {
       
   148                 MTWCoreUI.log(e.getMessage(), e);
       
   149             }
       
   150         }
       
   151         monitor.done();
       
   152     }
       
   153 
       
   154     /** Toggle BlueCove logging
       
   155      */
       
   156     public void enableBlueCoveDiagnostics(boolean enable) {
       
   157         System.setProperty("bluecove.debug", Boolean.valueOf(enable).toString());
       
   158         BlueCoveImpl.instance().enableNativeDebug(enable);
       
   159         if (enable) {
       
   160             System.setOut(new PrintStream(ConsoleFactory.createStream()));
       
   161         } else {
       
   162             System.setOut(savedSysOut);
       
   163         }
       
   164     }
       
   165 
       
   166     public IDeploymentTarget findTarget(IMTWProject project, String id) {
       
   167         if (!isBloothToothConnected()) {
       
   168             return null;
       
   169         }
       
   170         if (discovered) {
       
   171             return targets.get(id);
       
   172         } else {
       
   173             BluetoothTarget target = targets.get(id);
       
   174             if (target == null) {
       
   175                 target = new BluetoothTarget(id, null, this);
       
   176                 targets.put(id, target);
       
   177             }
       
   178             return target;
       
   179         }
       
   180     }
       
   181 
       
   182     public IDeploymentTarget[] getTargets(IMTWProject project) {
       
   183         if (targets != null) {
       
   184             final Collection<BluetoothTarget> values = targets.values();
       
   185             return values.toArray(new IDeploymentTarget[values.size()]);
       
   186         } else {
       
   187             return null;
       
   188         }
       
   189     }
       
   190 
       
   191     /**
       
   192      * Check whether the bluetooth is on or not.
       
   193      * 
       
   194      * @return whether the device is on.
       
   195      */
       
   196     public boolean isBloothToothConnected() {
       
   197         return LocalDevice.isPowerOn();
       
   198     }
       
   199 
       
   200     public boolean targetsDiscovered() {
       
   201         return discovered;
       
   202     }
       
   203 
       
   204     public ISchedulingRule getSchedulingRule(IDeploymentTarget target) {
       
   205         return BluetoothRule.INSTANCE;
       
   206     }
       
   207 }