r686 - in bigfin: . src src/main src/main/java src/main/java/org src/main/java/org/nuiton src/main/java/org/nuiton/bigfin src/test src/test/java src/test/java/com src/test/java/com/codelutin src/test/java/org src/test/java/org/nuiton src/test/java/org/nuiton/bigfin src/test/resources
Author: tchemit Date: 2013-10-13 08:31:24 +0200 (Sun, 13 Oct 2013) New Revision: 686 Url: http://nuiton.org/projects/sandbox/repository/revisions/686 Log: add bluethooth poc Added: bigfin/pom.xml bigfin/src/ bigfin/src/main/ bigfin/src/main/java/ bigfin/src/main/java/org/ bigfin/src/main/java/org/nuiton/ bigfin/src/main/java/org/nuiton/bigfin/ bigfin/src/main/java/org/nuiton/bigfin/BigFinClient.java bigfin/src/main/resources/ bigfin/src/test/ bigfin/src/test/java/ bigfin/src/test/java/com/ bigfin/src/test/java/com/codelutin/ bigfin/src/test/java/com/codelutin/bigfin/ bigfin/src/test/java/org/ bigfin/src/test/java/org/nuiton/ bigfin/src/test/java/org/nuiton/bigfin/ bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java bigfin/src/test/resources/ bigfin/src/test/resources/log4j.properties Modified: bigfin/ Property changes on: bigfin ___________________________________________________________________ Added: svn:ignore + target .idea *.ipr *.iws *.iml Added: bigfin/pom.xml =================================================================== --- bigfin/pom.xml (rev 0) +++ bigfin/pom.xml 2013-10-13 06:31:24 UTC (rev 686) @@ -0,0 +1,112 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>com.codelutin</groupId> + <artifactId>bigfin</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>BigFin acquisition</name> + <description> + Pour tester l'acquisition de données en bluetooth du bingfin. + </description> + <inceptionYear>2013</inceptionYear> + <organization> + <name>CodeLutin</name> + <url>http://www.codelutin.com/</url> + </organization> + <licenses> + <license> + <name>General Public License (GPL)</name> + <url>http://www.gnu.org/licenses/gpl.txt</url> + <distribution>repo</distribution> + </license> + </licenses> + + <developers> + <developer> + <id>chemit</id> + <name>Tony Chemit</name> + <email>chemit at codelutin dot com</email> + <organization>CodeLutin</organization> + <organizationUrl>http://www.codelutin.com</organizationUrl> + <roles> + <role>developer</role> + </roles> + <timezone>Europe/Paris</timezone> + </developer> + </developers> + + <scm> + <url>http://svn.nuiton.org/svn/sandbox/bigfin</url> + <connection> + scm:svn:http://svn.nuiton.org/svn/sandbox/bigfin + </connection> + <developerConnection> + scm:svn:http://svn.nuiton.org/svn/sandbox/bigfin + </developerConnection> + </scm> + + <properties> + + <!-- libraries version --> + + <bluecoveVersion>2.1.0</bluecoveVersion> + + </properties> + + <dependencies> +<dependency> + <groupId>net.sf.bluecove</groupId> + <artifactId>bluecove</artifactId> + <version>${bluecoveVersion}</version> + </dependency> + + <dependency> + <groupId>net.sf.bluecove</groupId> + <artifactId>bluecove-gpl</artifactId> + <version>${bluecoveVersion}</version> + </dependency> + + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + <version>1.1.3</version> + </dependency> + + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>15.0</version> + </dependency> + + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.17</version> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + + </dependencies> + + <build> + <pluginManagement> + <plugins> + + </plugins> + </pluginManagement> + </build> + + <reporting> + <excludeDefaults>true</excludeDefaults> + </reporting> + +</project> Property changes on: bigfin/pom.xml ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: bigfin/src/main/java/org/nuiton/bigfin/BigFinClient.java =================================================================== --- bigfin/src/main/java/org/nuiton/bigfin/BigFinClient.java (rev 0) +++ bigfin/src/main/java/org/nuiton/bigfin/BigFinClient.java 2013-10-13 06:31:24 UTC (rev 686) @@ -0,0 +1,186 @@ +package org.nuiton.bigfin; + +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.bluetooth.BluetoothStateException; +import javax.bluetooth.DataElement; +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; +import java.io.IOException; +import java.util.List; + +/** + * BigFin bluetooth client. + * <p/> + * Created on 10/11/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 1.0 + */ +public class BigFinClient { + + /** Logger. */ + private static final Log log = LogFactory.getLog(BigFinClient.class); + + protected final LocalDevice localDevice; + + protected RemoteDevice remoteDevice; + + protected List<ServiceRecord> services; + + public BigFinClient() throws BluetoothStateException { + LocalDevice localDevice; + try { + localDevice = LocalDevice.getLocalDevice(); + } catch (BluetoothStateException e) { + if (log.isErrorEnabled()) { + log.error("Could not find bluetooth local device"); + } + localDevice = null; + } + this.localDevice = localDevice; + } + + public boolean foundLocalDevice() { + return localDevice!=null; + } + + public List<RemoteDevice> discoverDevices() throws BluetoothStateException, InterruptedException { + + Preconditions.checkState(foundLocalDevice(), + "BlueTooth local device not found"); + + List<RemoteDevice> devices = Lists.newArrayList(); + + MyDiscoveryListener listener = new MyDiscoveryListener(this, devices, null); + + synchronized (this) { + boolean started = localDevice.getDiscoveryAgent().startInquiry(DiscoveryAgent.LIAC, listener); + if (started) { + if (log.isInfoEnabled()) { + log.info("wait for device inquiry to complete..."); + } + this.wait(); + if (log.isInfoEnabled()) { + log.info(devices.size() + " device(s) found"); + } + } + } + return devices; + } + + public List<ServiceRecord> discoverServiceUrls(UUID serviceUUID, RemoteDevice device) throws BluetoothStateException, InterruptedException { + + Preconditions.checkState(foundLocalDevice(), + "BlueTooth local device not found"); + + List<ServiceRecord> serviceRecords = Lists.newArrayList(); + + MyDiscoveryListener listener = new MyDiscoveryListener(this, + null, + serviceRecords); + + synchronized (this) { + + int[] attrIDs = new int[]{ + 0x0100 // Service name + }; + + localDevice.getDiscoveryAgent().searchServices(attrIDs, + new UUID[]{serviceUUID}, + device, + listener); + this.wait(); + } + + return serviceRecords; + } + + protected static class MyDiscoveryListener implements DiscoveryListener { + + final Object lock; + + final List<RemoteDevice> devices; + + final List<ServiceRecord> serviceRecords; + + MyDiscoveryListener(Object lock, + List<RemoteDevice> devices, + List<ServiceRecord> serviceRecords) { + this.lock = lock; + this.devices = devices; + this.serviceRecords = serviceRecords; + } + + + public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { + if (log.isInfoEnabled()) { + log.info("Device " + btDevice.getBluetoothAddress() + " found"); + } + devices.add(btDevice); + try { + String friendlyName = btDevice.getFriendlyName(false); + if (log.isInfoEnabled()) { + log.info("Name: " + friendlyName); + } + } catch (IOException e) { + if (log.isErrorEnabled()) { + log.error("Can't get name of remote", e); + } + } + } + + public void inquiryCompleted(int discType) { + if (log.isDebugEnabled()) { + log.debug("Device Inquiry completed!"); + } + synchronized (lock) { + lock.notifyAll(); + } + } + + public void serviceSearchCompleted(int transID, int respCode) { + if (log.isDebugEnabled()) { + log.debug("Service search completed!"); + } + synchronized (lock) { + lock.notifyAll(); + } + } + + public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { + for (ServiceRecord aServRecord : servRecord) { + String url = aServRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); + if (url == null) { + continue; + } + serviceRecords.add(aServRecord); + String serviceName = getServiceName(aServRecord); + if (log.isDebugEnabled()) { + log.debug("service found " + url + (serviceName == null ? "" : ", name: " + serviceName)); + } + } + } + } + + public static String getServiceName(ServiceRecord serviceRecord) { + DataElement serviceName = serviceRecord.getAttributeValue(0x0100); + String result; + + if (serviceName == null) { + result = null; + } else { + result = String.valueOf(serviceName.getValue()); + } + return result; + } + +} Property changes on: bigfin/src/main/java/org/nuiton/bigfin/BigFinClient.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java =================================================================== --- bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java (rev 0) +++ bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java 2013-10-13 06:31:24 UTC (rev 686) @@ -0,0 +1,124 @@ +package org.nuiton.bigfin; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assume; +import org.junit.Test; + +import javax.bluetooth.RemoteDevice; +import javax.bluetooth.ServiceRecord; +import javax.bluetooth.UUID; +import javax.microedition.io.Connector; +import javax.microedition.io.StreamConnection; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.List; + +/** + * To test {@link BigFinClient}. + * <p/> + * Created on 10/11/13. + * + * @author Tony Chemit <chemit@codelutin.com> + * @since 1.0 + */ +public class BigFinClientTest { + + /** Logger. */ + private static final Log log = LogFactory.getLog(BigFinClientTest.class); + + @Test + public void connect() throws IOException, InterruptedException { + + BigFinClient client = new BigFinClient(); + + Assume.assumeTrue("No local device found, stop test.", + client.foundLocalDevice()); + + // get remote devices + List<RemoteDevice> devices = client.discoverDevices(); + + Assume.assumeTrue("No remote device found, stop test.", + !devices.isEmpty()); + + for (RemoteDevice device : devices) { + + if (log.isInfoEnabled()) { + log.info("found remote device: " + device); + } + + Multimap<Integer, ServiceRecord> services = + ArrayListMultimap.create(); + + int[] indexes = {3, 256, 4353}; + + for (int index : indexes) { + List<ServiceRecord> serviceRecords = client.discoverServiceUrls(new UUID(index), device); + + if (!serviceRecords.isEmpty()) { + if (log.isInfoEnabled()) { + log.info("Found some services for index: " + index); + } + services.putAll(index, serviceRecords); + } + } + +// int index = 0x0004; +// for (int i = 0; i < 20000; i++) { +// +// if (log.isInfoEnabled()) { +// log.info("Try to discover with service id: " + index); +// } +// +// List<ServiceRecord> serviceRecords = client.discoverServiceUrls(new UUID(index), device); +// +// if (!serviceRecords.isEmpty()) { +// if (log.isInfoEnabled()) { +// log.info("Found some services for index: " + index); +// } +// services.putAll(index, serviceRecords); +// +// } +// index++; +// } + + for (Integer serviceIndex : services.keySet()) { + + for (ServiceRecord serviceRecord : services.get(serviceIndex)) { + String url = serviceRecord.getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, true); + + String serviceName = BigFinClient.getServiceName(serviceRecord); + if (log.isInfoEnabled()) { + log.info("Found service(" + serviceIndex + "): " + url + (serviceName == null ? "" : ", name: " + serviceName)); + } + StreamConnection connection = (StreamConnection) Connector.open(url); + + { + RemoteDevice rd = RemoteDevice.getRemoteDevice(connection); + boolean authenticate = rd.authenticate(); + if (log.isInfoEnabled()) { + log.info("Authenticate response: " + authenticate); + } + } + + DataInputStream dataInputStream = connection.openDataInputStream(); + + if (log.isInfoEnabled()) { + log.info("Open data stream: "+dataInputStream); + } + while (true) { + + String c = dataInputStream.readUTF(); + + if (log.isInfoEnabled()) { + log.info("Read: " + c); + } + } + } + } + } + } + +} Property changes on: bigfin/src/test/java/org/nuiton/bigfin/BigFinClientTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: bigfin/src/test/resources/log4j.properties =================================================================== --- bigfin/src/test/resources/log4j.properties (rev 0) +++ bigfin/src/test/resources/log4j.properties 2013-10-13 06:31:24 UTC (rev 686) @@ -0,0 +1,31 @@ +### +# #%L +# Tutti :: Service +# $Id$ +# $HeadURL: http://svn.forge.codelutin.com/svn/tutti/trunk/tutti-service/src/test/resour... $ +# %% +# Copyright (C) 2012 - 2013 Ifremer +# %% +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public +# License along with this program. If not, see +# <http://www.gnu.org/licenses/gpl-3.0.html>. +# #L% +### + +log4j.rootCategory=INFO, A1 +#log4j.logger.no.api=DEBUG +log4j.appender.A1=org.apache.log4j.ConsoleAppender +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n + +log4j.logger.javax.bluetooth=DEBUG \ No newline at end of file Property changes on: bigfin/src/test/resources/log4j.properties ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native
participants (1)
-
tchemit@users.nuiton.org