/*
* Copyright 2011 Ytai Ben-Tsvi. All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied.
*/
package ioio.lib.android.bluetooth;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;
import ioio.lib.api.IOIOConnection;
import ioio.lib.api.exception.ConnectionLostException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.UUID;
public class BluetoothIOIOConnection implements IOIOConnection
{
private static final String TAG = "BluetoothIOIOConnection";
private BluetoothSocket socket_ = null;
private boolean disconnect_ = false;
private final BluetoothDevice device_;
private final String name_;
private final String address_;
public BluetoothIOIOConnection(BluetoothDevice device) {
device_ = device;
name_ = device.getName();
address_ = device.getAddress();
}
@Override
public void waitForConnect() throws ConnectionLostException {
synchronized (this) {
if (disconnect_) {
throw new ConnectionLostException();
}
try {
socket_ = createSocket(device_);
} catch (IOException e) {
throw new ConnectionLostException(e);
}
}
// keep trying to connect as long as we're not aborting
while (true) {
try {
Log.v(TAG, "Attempting to connect to Bluetooth device: " + name_);
socket_.connect();
Log.v(TAG, "Established connection to device " + name_ + " address: " + address_);
break; // if we got here, we're connected
} catch (Exception e) {
if (disconnect_) {
throw new ConnectionLostException(e);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
}
}
}
}
public static BluetoothSocket createSocket(final BluetoothDevice device)
throws IOException
{
BluetoothSocket socket = null;
try
{
Method m = device.getClass().getMethod("createRfcommSocket", int.class);
socket = (BluetoothSocket) m.invoke(device, 1);
}
catch (NoSuchMethodException ignore)
{
socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
catch (Exception ignore)
{
}
return socket;
}
@Override
public synchronized void disconnect() {
if (disconnect_) {
return;
}
Log.v(TAG, "Client initiated disconnect");
disconnect_ = true;
if (socket_ != null) {
try {
socket_.close();
} catch (IOException e) {
}
}
}
@Override
public InputStream getInputStream() throws ConnectionLostException {
try {
return socket_.getInputStream();
} catch (IOException e) {
throw new ConnectionLostException(e);
}
}
@Override
public OutputStream getOutputStream() throws ConnectionLostException {
try {
return socket_.getOutputStream();
} catch (IOException e) {
throw new ConnectionLostException(e);
}
}
}