-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IOIO sources from App-IOIO0323
- Loading branch information
Showing
51 changed files
with
7,491 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/main/java/ioio/lib/android/bluetooth/BluetoothIOIOConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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.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(1000); | ||
} catch (InterruptedException e1) { | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static BluetoothSocket createSocket(final BluetoothDevice device) | ||
throws IOException | ||
{ | ||
return device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); | ||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/ioio/lib/android/bluetooth/BluetoothIOIOConnectionBootstrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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.BluetoothAdapter; | ||
import android.bluetooth.BluetoothDevice; | ||
import android.util.Log; | ||
import ioio.lib.api.IOIOConnection; | ||
import ioio.lib.spi.IOIOConnectionBootstrap; | ||
import ioio.lib.spi.IOIOConnectionFactory; | ||
import ioio.lib.spi.NoRuntimeSupportException; | ||
|
||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
public class BluetoothIOIOConnectionBootstrap implements | ||
IOIOConnectionBootstrap | ||
{ | ||
|
||
private static final String TAG = "BluetoothIOIOConnectionDiscovery"; | ||
private final BluetoothAdapter adapter_; | ||
|
||
public BluetoothIOIOConnectionBootstrap() throws NoRuntimeSupportException | ||
{ | ||
try { | ||
adapter_ = BluetoothAdapter.getDefaultAdapter(); | ||
if (adapter_ != null) { | ||
return; | ||
} | ||
} catch (Throwable e) { | ||
} | ||
throw new NoRuntimeSupportException( | ||
"Bluetooth is not supported on this device."); | ||
} | ||
|
||
@Override | ||
public void getFactories(Collection<IOIOConnectionFactory> result) { | ||
try { | ||
Set<BluetoothDevice> bondedDevices = adapter_.getBondedDevices(); | ||
for (final BluetoothDevice device : bondedDevices) { | ||
if (device.getName().startsWith("IOIO")) { | ||
result.add(new IOIOConnectionFactory() { | ||
@Override | ||
public String getType() { | ||
return BluetoothIOIOConnection.class | ||
.getCanonicalName(); | ||
} | ||
|
||
@Override | ||
public Object getExtra() { | ||
return new Object[] { device.getName(), | ||
device.getAddress() }; | ||
} | ||
|
||
@Override | ||
public IOIOConnection createConnection() { | ||
return new BluetoothIOIOConnection(device); | ||
} | ||
}); | ||
} | ||
} | ||
} catch (SecurityException e) { | ||
Log.e(TAG, | ||
"Did you forget to declare uses-permission of android.permission.BLUETOOTH?"); | ||
throw e; | ||
} catch (NoClassDefFoundError e) { | ||
Log.w(TAG, "Bluetooth is not supported on this device.", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.