├── Bluetooth ├── .gitignore ├── .idea │ ├── .name │ ├── compiler.xml │ ├── copyright │ │ └── profiles_settings.xml │ ├── dictionaries │ │ └── Rami_MARTIN.xml │ ├── encodings.xml │ ├── gradle.xml │ ├── libraries │ │ ├── appcompat_v7_19_1_0.xml │ │ ├── butterknife_4_0_1.xml │ │ ├── eventbus_2_2_0.xml │ │ └── support_v4_19_1_0.xml │ ├── misc.xml │ ├── modules.xml │ ├── scopes │ │ └── scope_settings.xml │ └── vcs.xml ├── Bluetooth.iml ├── Bluetooth_lib │ ├── .gitignore │ ├── Bluetooth.iml │ ├── Bluetooth_lib.iml │ ├── build.gradle │ ├── pom.xml │ ├── proguard-rules.txt │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── ramimartin │ │ │ └── bluetooth │ │ │ ├── BluetoothClient.java │ │ │ ├── BluetoothConnector.java │ │ │ ├── BluetoothManager.java │ │ │ ├── BluetoothServer.java │ │ │ ├── activity │ │ │ ├── BluetoothActivity.java │ │ │ └── BluetoothFragmentActivity.java │ │ │ ├── bus │ │ │ ├── BluetoothCommunicator.java │ │ │ ├── BondedDevice.java │ │ │ ├── ClientConnectionFail.java │ │ │ ├── ClientConnectionSuccess.java │ │ │ ├── ServeurConnectionFail.java │ │ │ └── ServeurConnectionSuccess.java │ │ │ └── fragment │ │ │ └── BluetoothFragment.java │ │ └── res │ │ ├── drawable-hdpi │ │ └── ic_launcher.png │ │ ├── drawable-mdpi │ │ └── ic_launcher.png │ │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ │ └── values │ │ └── strings.xml ├── Bluetooth_sample │ ├── .gitignore │ ├── Bluetooth-Bluetooth.iml │ ├── Bluetooth_sample.iml │ ├── build.gradle │ ├── pom.xml │ ├── proguard-rules.txt │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── ramimartin │ │ │ └── sample │ │ │ └── bluetooth │ │ │ └── MainActivity.java │ │ └── res │ │ ├── drawable-hdpi │ │ └── ic_launcher.png │ │ ├── drawable-mdpi │ │ └── ic_launcher.png │ │ ├── drawable-xhdpi │ │ ├── ic_launcher.png │ │ └── rami_logo.png │ │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ │ ├── layout │ │ └── activity_main.xml │ │ ├── menu │ │ └── main.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── README.md └── img ├── nexus_client.png └── nexus_server.png /Bluetooth/.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /Bluetooth/.idea/.name: -------------------------------------------------------------------------------- 1 | Bluetooth -------------------------------------------------------------------------------- /Bluetooth/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Bluetooth/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Bluetooth/.idea/dictionaries/Rami_MARTIN.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Bluetooth/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Bluetooth/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Bluetooth/.idea/libraries/appcompat_v7_19_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Bluetooth/.idea/libraries/butterknife_4_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Bluetooth/.idea/libraries/eventbus_2_2_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Bluetooth/.idea/libraries/support_v4_19_1_0.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Bluetooth/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | Android 23 | 24 | 25 | Android Lint 26 | 27 | 28 | 29 | 30 | Abstraction issues 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Bluetooth/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Bluetooth/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /Bluetooth/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/Bluetooth.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/Bluetooth_lib.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android-library' 2 | 3 | android { 4 | compileSdkVersion 18 5 | buildToolsVersion "19.0.3" 6 | 7 | lintOptions { 8 | abortOnError false 9 | } 10 | 11 | packagingOptions { 12 | exclude 'META-INF/services/javax.annotation.processing.Processor' 13 | exclude 'META-INF/LICENSE.txt' 14 | exclude 'META-INF/LICENSE' 15 | exclude 'META-INF/NOTICE' 16 | } 17 | 18 | defaultConfig { 19 | minSdkVersion 8 20 | targetSdkVersion 16 21 | versionCode 1 22 | versionName "1.0" 23 | } 24 | } 25 | 26 | dependencies { 27 | compile 'com.android.support:appcompat-v7:19.+' 28 | compile 'de.greenrobot:eventbus:2.2.0' 29 | } 30 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.ramimartin.bluetooth 4 | AndroidBluetoothLibrary 5 | 1.0.1-SNAPSHOT 6 | jar 7 | Android Bluetooth Library 8 | 9 | 10 | 11 | Apache License, Version 2.0 12 | http://www.apache.org/licenses/LICENSE-2.0.txt 13 | repo 14 | 15 | 16 | 17 | 18 | 19 | arissa34-ftp 20 | ftp://arissa34@ftpperso.free.fr/maven2 21 | 22 | 23 | 24 | 25 | 26 | de.greenrobot 27 | eventbus 28 | 2.2.0 29 | 30 | 31 | com.google.android 32 | android 33 | 4.1.1.4 34 | provided 35 | 36 | 37 | com.google.android 38 | support-v4 39 | r6 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.wagon 47 | wagon-ftp 48 | 1.0-alpha-6 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:/android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the ProGuard 5 | # include property in project.properties. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/BluetoothClient.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth; 2 | 3 | import android.bluetooth.BluetoothAdapter; 4 | import android.bluetooth.BluetoothDevice; 5 | import android.bluetooth.BluetoothSocket; 6 | import android.util.Log; 7 | 8 | import com.ramimartin.bluetooth.bus.BluetoothCommunicator; 9 | import com.ramimartin.bluetooth.bus.ClientConnectionFail; 10 | import com.ramimartin.bluetooth.bus.ClientConnectionSuccess; 11 | 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.io.OutputStreamWriter; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | import java.util.UUID; 18 | 19 | import de.greenrobot.event.EventBus; 20 | 21 | /** 22 | * Created by Rami MARTIN on 13/04/2014. 23 | */ 24 | public class BluetoothClient implements Runnable { 25 | 26 | private boolean CONTINUE_READ_WRITE = true; 27 | 28 | private BluetoothAdapter mBluetoothAdapter; 29 | private BluetoothDevice mBluetoothDevice; 30 | private UUID mUuid; 31 | private String mAdressMac; 32 | 33 | private BluetoothSocket mSocket; 34 | private BluetoothSocket fallbackSocket; 35 | private InputStream mInputStream; 36 | private OutputStreamWriter mOutputStreamWriter; 37 | 38 | private BluetoothConnector mBluetoothConnector; 39 | 40 | public BluetoothClient(BluetoothAdapter bluetoothAdapter, UUID uuid, String adressMac) { 41 | mBluetoothAdapter = bluetoothAdapter; 42 | mUuid = uuid; 43 | mAdressMac = adressMac; 44 | } 45 | 46 | @Override 47 | public void run() { 48 | 49 | mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mAdressMac); 50 | List uuidCandidates = new ArrayList(); 51 | uuidCandidates.add(mUuid); 52 | mBluetoothConnector = new BluetoothConnector(mBluetoothDevice, true, mBluetoothAdapter, uuidCandidates); 53 | 54 | try { 55 | mSocket = mBluetoothConnector.connect().getUnderlyingSocket(); 56 | } catch (IOException e1) { 57 | EventBus.getDefault().post(new ClientConnectionFail()); 58 | e1.printStackTrace(); 59 | } 60 | 61 | if (mSocket == null) { 62 | Log.e("", "mSocket == Null"); 63 | return; 64 | } 65 | 66 | try { 67 | 68 | mInputStream = mSocket.getInputStream(); 69 | mOutputStreamWriter = new OutputStreamWriter(mSocket.getOutputStream()); 70 | 71 | int bufferSize = 1024; 72 | int bytesRead = -1; 73 | byte[] buffer = new byte[bufferSize]; 74 | 75 | EventBus.getDefault().post(new ClientConnectionSuccess()); 76 | 77 | while (CONTINUE_READ_WRITE) { 78 | 79 | final StringBuilder sb = new StringBuilder(); 80 | bytesRead = mInputStream.read(buffer); 81 | if (bytesRead != -1) { 82 | String result = ""; 83 | while ((bytesRead == bufferSize) && (buffer[bufferSize] != 0)) { 84 | result = result + new String(buffer, 0, bytesRead); 85 | bytesRead = mInputStream.read(buffer); 86 | } 87 | result = result + new String(buffer, 0, bytesRead); 88 | sb.append(result); 89 | } 90 | EventBus.getDefault().post(new BluetoothCommunicator(sb.toString())); 91 | } 92 | } catch (IOException e) { 93 | e.printStackTrace(); 94 | EventBus.getDefault().post(new ClientConnectionFail()); 95 | } 96 | } 97 | 98 | public void write(String message) { 99 | try { 100 | mOutputStreamWriter.write(message); 101 | mOutputStreamWriter.flush(); 102 | } catch (IOException e) { 103 | e.printStackTrace(); 104 | } 105 | } 106 | 107 | public void closeConnexion() { 108 | if (mSocket != null) { 109 | try { 110 | mInputStream.close(); 111 | mInputStream = null; 112 | mOutputStreamWriter.close(); 113 | mOutputStreamWriter = null; 114 | mSocket.close(); 115 | mSocket = null; 116 | mBluetoothConnector.close(); 117 | } catch (Exception e) { 118 | } 119 | CONTINUE_READ_WRITE = false; 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/BluetoothConnector.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth; 2 | 3 | import android.bluetooth.BluetoothAdapter; 4 | import android.bluetooth.BluetoothDevice; 5 | import android.bluetooth.BluetoothSocket; 6 | import android.util.Log; 7 | 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.io.OutputStream; 11 | import java.lang.reflect.Method; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | import java.util.UUID; 15 | 16 | public class BluetoothConnector { 17 | 18 | private BluetoothSocketWrapper bluetoothSocket; 19 | private BluetoothDevice device; 20 | private boolean secure; 21 | private BluetoothAdapter adapter; 22 | private List uuidCandidates; 23 | private int candidate; 24 | 25 | 26 | /** 27 | * @param device the device 28 | * @param secure if connection should be done via a secure socket 29 | * @param adapter the Android BT adapter 30 | * @param uuidCandidates a list of UUIDs. if null or empty, the Serial PP id is used 31 | */ 32 | public BluetoothConnector(BluetoothDevice device, boolean secure, BluetoothAdapter adapter, 33 | List uuidCandidates) { 34 | this.device = device; 35 | this.secure = secure; 36 | this.adapter = adapter; 37 | this.uuidCandidates = uuidCandidates; 38 | 39 | if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) { 40 | this.uuidCandidates = new ArrayList(); 41 | this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 42 | } 43 | } 44 | 45 | public BluetoothSocketWrapper connect() throws IOException { 46 | boolean success = false; 47 | while (selectSocket()) { 48 | adapter.cancelDiscovery(); 49 | 50 | try { 51 | bluetoothSocket.connect(); 52 | success = true; 53 | break; 54 | } catch (IOException e) { 55 | //try the fallback 56 | try { 57 | bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket()); 58 | Thread.sleep(500); 59 | bluetoothSocket.connect(); 60 | success = true; 61 | break; 62 | } catch (FallbackException e1) { 63 | Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e); 64 | } catch (InterruptedException e1) { 65 | Log.w("BT", e1.getMessage(), e1); 66 | } catch (IOException e1) { 67 | Log.w("BT", "Fallback failed. Cancelling.", e1); 68 | } 69 | } 70 | } 71 | 72 | if (!success) { 73 | throw new IOException("Could not connect to device: "+ device.getAddress()); 74 | } 75 | 76 | return bluetoothSocket; 77 | } 78 | 79 | private boolean selectSocket() throws IOException { 80 | if (candidate >= uuidCandidates.size()) { 81 | return false; 82 | } 83 | 84 | BluetoothSocket tmp; 85 | UUID uuid = uuidCandidates.get(candidate++); 86 | 87 | Log.i("BT", "Attempting to connect to Protocol: "+ uuid); 88 | if (secure) { 89 | tmp = device.createRfcommSocketToServiceRecord(uuid); 90 | } else { 91 | tmp = device.createInsecureRfcommSocketToServiceRecord(uuid); 92 | } 93 | bluetoothSocket = new NativeBluetoothSocket(tmp); 94 | 95 | return true; 96 | } 97 | 98 | public void close(){ 99 | if(bluetoothSocket != null){ 100 | try { 101 | bluetoothSocket.close(); 102 | } catch (IOException e) { 103 | e.printStackTrace(); 104 | } 105 | } 106 | } 107 | 108 | public static interface BluetoothSocketWrapper { 109 | 110 | InputStream getInputStream() throws IOException; 111 | 112 | OutputStream getOutputStream() throws IOException; 113 | 114 | String getRemoteDeviceName(); 115 | 116 | void connect() throws IOException; 117 | 118 | String getRemoteDeviceAddress(); 119 | 120 | void close() throws IOException; 121 | 122 | BluetoothSocket getUnderlyingSocket(); 123 | 124 | } 125 | 126 | 127 | public static class NativeBluetoothSocket implements BluetoothSocketWrapper { 128 | 129 | private BluetoothSocket socket; 130 | 131 | public NativeBluetoothSocket(BluetoothSocket tmp) { 132 | this.socket = tmp; 133 | } 134 | 135 | @Override 136 | public InputStream getInputStream() throws IOException { 137 | return socket.getInputStream(); 138 | } 139 | 140 | @Override 141 | public OutputStream getOutputStream() throws IOException { 142 | return socket.getOutputStream(); 143 | } 144 | 145 | @Override 146 | public String getRemoteDeviceName() { 147 | return socket.getRemoteDevice().getName(); 148 | } 149 | 150 | @Override 151 | public void connect() throws IOException { 152 | socket.connect(); 153 | } 154 | 155 | @Override 156 | public String getRemoteDeviceAddress() { 157 | return socket.getRemoteDevice().getAddress(); 158 | } 159 | 160 | @Override 161 | public void close() throws IOException { 162 | socket.close(); 163 | } 164 | 165 | @Override 166 | public BluetoothSocket getUnderlyingSocket() { 167 | return socket; 168 | } 169 | 170 | } 171 | 172 | public class FallbackBluetoothSocket extends NativeBluetoothSocket { 173 | 174 | private BluetoothSocket fallbackSocket; 175 | 176 | public FallbackBluetoothSocket(BluetoothSocket tmp) throws FallbackException { 177 | super(tmp); 178 | try 179 | { 180 | Class clazz = tmp.getRemoteDevice().getClass(); 181 | Class[] paramTypes = new Class[] {Integer.TYPE}; 182 | Method m = clazz.getMethod("createRfcommSocket", paramTypes); 183 | Object[] params = new Object[] {Integer.valueOf(1)}; 184 | fallbackSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params); 185 | } 186 | catch (Exception e) 187 | { 188 | throw new FallbackException(e); 189 | } 190 | } 191 | 192 | @Override 193 | public InputStream getInputStream() throws IOException { 194 | return fallbackSocket.getInputStream(); 195 | } 196 | 197 | @Override 198 | public OutputStream getOutputStream() throws IOException { 199 | return fallbackSocket.getOutputStream(); 200 | } 201 | 202 | 203 | @Override 204 | public void connect() throws IOException { 205 | fallbackSocket.connect(); 206 | } 207 | 208 | 209 | @Override 210 | public void close() throws IOException { 211 | fallbackSocket.close(); 212 | } 213 | 214 | } 215 | 216 | public static class FallbackException extends Exception { 217 | 218 | /** 219 | * 220 | */ 221 | private static final long serialVersionUID = 1L; 222 | 223 | public FallbackException(Exception e) { 224 | super(e); 225 | } 226 | 227 | } 228 | } -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/BluetoothManager.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth; 2 | 3 | import android.app.Activity; 4 | import android.bluetooth.BluetoothAdapter; 5 | import android.bluetooth.BluetoothDevice; 6 | import android.content.BroadcastReceiver; 7 | import android.content.Context; 8 | import android.content.Intent; 9 | import android.content.IntentFilter; 10 | import android.util.Log; 11 | 12 | import com.ramimartin.bluetooth.bus.BondedDevice; 13 | 14 | import java.util.UUID; 15 | 16 | import de.greenrobot.event.EventBus; 17 | 18 | /** 19 | * Created by Rami MARTIN on 13/04/2014. 20 | */ 21 | public class BluetoothManager extends BroadcastReceiver { 22 | 23 | public enum TypeBluetooth{ 24 | Client, 25 | Server; 26 | } 27 | 28 | public static final int REQUEST_DISCOVERABLE_CODE = 114; 29 | 30 | public static int BLUETOOTH_REQUEST_ACCEPTED; 31 | public static final int BLUETOOTH_REQUEST_REFUSED = 0; // NE PAS MODIFIER LA VALEUR 32 | 33 | public static final int BLUETOOTH_TIME_DICOVERY_60_SEC = 60; 34 | public static final int BLUETOOTH_TIME_DICOVERY_120_SEC = 120; 35 | public static final int BLUETOOTH_TIME_DICOVERY_300_SEC = 300; 36 | 37 | private Activity mActivity; 38 | private BluetoothAdapter mBluetoothAdapter; 39 | private BluetoothClient mBluetoothClient; 40 | private BluetoothServer mBluetoothServer; 41 | 42 | private UUID mUUID = null; 43 | private TypeBluetooth mType; 44 | private int mTimeDiscoverable; 45 | public boolean isConnected; 46 | private boolean mBluetoothIsEnableOnStart; 47 | 48 | public BluetoothManager(){ 49 | 50 | } 51 | 52 | public BluetoothManager(Activity activity) { 53 | mActivity = activity; 54 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 55 | mBluetoothIsEnableOnStart = mBluetoothAdapter.isEnabled(); 56 | isConnected = false; 57 | setTimeDiscoverable(BLUETOOTH_TIME_DICOVERY_300_SEC); 58 | } 59 | 60 | public void setUUID(UUID uuid){ 61 | mUUID = uuid; 62 | } 63 | 64 | public void setTimeDiscoverable(int timeInSec){ 65 | mTimeDiscoverable = timeInSec; 66 | BLUETOOTH_REQUEST_ACCEPTED = mTimeDiscoverable; 67 | } 68 | 69 | public boolean checkBluetoothAviability(){ 70 | if (mBluetoothAdapter == null) { 71 | return false; 72 | }else{ 73 | return true; 74 | } 75 | } 76 | 77 | public void cancelDiscovery(){ 78 | if(mBluetoothAdapter.isDiscovering()) { 79 | mBluetoothAdapter.cancelDiscovery(); 80 | } 81 | } 82 | 83 | public boolean isDiscovering(){ 84 | return mBluetoothAdapter.isDiscovering(); 85 | } 86 | 87 | public void startDiscovery() { 88 | if(mUUID == null){ 89 | Log.e("", "YOUR UUID IS NULL !!"); 90 | return; 91 | } 92 | if (mBluetoothAdapter == null) { 93 | return; 94 | } else { 95 | if(BuildConfig.DEBUG) { 96 | Log.e("", " ==> mBluetoothAdapter.isEnabled() : " + mBluetoothAdapter.isEnabled()); 97 | Log.e("", " ==> mBluetoothAdapter.isDiscovering() : " + mBluetoothAdapter.isDiscovering()); 98 | } 99 | if (mBluetoothAdapter.isEnabled() && mBluetoothAdapter.isDiscovering()) { 100 | return; 101 | } else { 102 | Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 103 | discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeDiscoverable); 104 | mActivity.startActivityForResult(discoverableIntent, REQUEST_DISCOVERABLE_CODE); 105 | } 106 | } 107 | } 108 | 109 | public void scanAllBluetoothDevice() { 110 | IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 111 | mActivity.registerReceiver(this, intentFilter); 112 | mBluetoothAdapter.startDiscovery(); 113 | } 114 | 115 | public void createClient(String addressMac) { 116 | mType = TypeBluetooth.Client; 117 | IntentFilter bondStateIntent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 118 | mActivity.registerReceiver(this, bondStateIntent); 119 | mBluetoothClient = new BluetoothClient(mBluetoothAdapter, mUUID, addressMac); 120 | new Thread(mBluetoothClient).start(); 121 | } 122 | 123 | public void createServeur(){ 124 | mType = TypeBluetooth.Server; 125 | mBluetoothServer = new BluetoothServer(mBluetoothAdapter, mUUID); 126 | new Thread(mBluetoothServer).start(); 127 | IntentFilter bondStateIntent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED); 128 | mActivity.registerReceiver(this, bondStateIntent); 129 | } 130 | 131 | public void sendMessage(String message) { 132 | if(mType != null && isConnected){ 133 | if(mType == TypeBluetooth.Server && mBluetoothServer != null){ 134 | mBluetoothServer.write(message); 135 | }else if(mType == TypeBluetooth.Client && mBluetoothClient != null){ 136 | mBluetoothClient.write(message); 137 | } 138 | } 139 | } 140 | 141 | @Override 142 | public void onReceive(Context context, Intent intent) { 143 | if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) { 144 | BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 145 | EventBus.getDefault().post(device); 146 | } 147 | if(intent.getAction().equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){ 148 | //Log.e("", "===> ACTION_BOND_STATE_CHANGED"); 149 | int prevBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1); 150 | int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1); 151 | if (prevBondState == BluetoothDevice.BOND_BONDING) 152 | { 153 | // check for both BONDED and NONE here because in some error cases the bonding fails and we need to fail gracefully. 154 | if (bondState == BluetoothDevice.BOND_BONDED || bondState == BluetoothDevice.BOND_NONE ) 155 | { 156 | //Log.e("", "===> BluetoothDevice.BOND_BONDED"); 157 | EventBus.getDefault().post(new BondedDevice()); 158 | } 159 | } 160 | } 161 | } 162 | 163 | public void resetServer(){ 164 | if(mType == TypeBluetooth.Server && mBluetoothServer != null){ 165 | mBluetoothServer.closeConnexion(); 166 | mBluetoothServer = null; 167 | } 168 | } 169 | 170 | public void resetClient(){ 171 | if(mType == TypeBluetooth.Client && mBluetoothClient != null){ 172 | mBluetoothClient.closeConnexion(); 173 | mBluetoothClient = null; 174 | } 175 | } 176 | 177 | public void closeAllConnexion(){ 178 | if(BuildConfig.DEBUG){ 179 | Log.e("","===> Bluetooth Lib Destroy"); 180 | } 181 | try{ 182 | mActivity.unregisterReceiver(this); 183 | }catch(Exception e){} 184 | 185 | cancelDiscovery(); 186 | 187 | if(!mBluetoothIsEnableOnStart){ 188 | mBluetoothAdapter.disable(); 189 | } 190 | 191 | mBluetoothAdapter = null; 192 | 193 | if(mType != null){ 194 | resetServer(); 195 | resetClient(); 196 | } 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/BluetoothServer.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth; 2 | 3 | import android.bluetooth.BluetoothAdapter; 4 | import android.bluetooth.BluetoothServerSocket; 5 | import android.bluetooth.BluetoothSocket; 6 | import android.util.Log; 7 | 8 | import com.ramimartin.bluetooth.bus.BluetoothCommunicator; 9 | import com.ramimartin.bluetooth.bus.ServeurConnectionFail; 10 | import com.ramimartin.bluetooth.bus.ServeurConnectionSuccess; 11 | 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.io.OutputStreamWriter; 15 | import java.util.UUID; 16 | 17 | import de.greenrobot.event.EventBus; 18 | 19 | /** 20 | * Created by Rami MARTIN on 13/04/2014. 21 | */ 22 | public class BluetoothServer implements Runnable { 23 | 24 | private boolean CONTINUE_READ_WRITE = true; 25 | 26 | private UUID mUUID; 27 | private BluetoothAdapter mBluetoothAdapter; 28 | private BluetoothServerSocket mServerSocket; 29 | private BluetoothSocket mSocket; 30 | private InputStream mInputStream; 31 | private OutputStreamWriter mOutputStreamWriter; 32 | 33 | public BluetoothServer(BluetoothAdapter bluetoothAdapter, UUID uuid){ 34 | mBluetoothAdapter = bluetoothAdapter; 35 | mUUID = uuid; 36 | } 37 | 38 | @Override 39 | public void run() { 40 | try { 41 | mServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("BLTServer", mUUID); 42 | mSocket = mServerSocket.accept(); 43 | mInputStream = mSocket.getInputStream(); 44 | mOutputStreamWriter = new OutputStreamWriter(mSocket.getOutputStream()); 45 | 46 | int bufferSize = 1024; 47 | int bytesRead = -1; 48 | byte[] buffer = new byte[bufferSize]; 49 | 50 | EventBus.getDefault().post(new ServeurConnectionSuccess()); 51 | 52 | while(CONTINUE_READ_WRITE) { 53 | final StringBuilder sb = new StringBuilder(); 54 | bytesRead = mInputStream.read(buffer); 55 | if (bytesRead != -1) { 56 | String result = ""; 57 | while ((bytesRead == bufferSize) && (buffer[bufferSize] != 0)) { 58 | result = result + new String(buffer, 0, bytesRead); 59 | bytesRead = mInputStream.read(buffer); 60 | } 61 | result = result + new String(buffer, 0, bytesRead); 62 | sb.append(result); 63 | } 64 | EventBus.getDefault().post(new BluetoothCommunicator(sb.toString())); 65 | 66 | } 67 | } catch (IOException e) { 68 | Log.e("", "ERROR : " + e.getMessage()); 69 | EventBus.getDefault().post(new ServeurConnectionFail()); 70 | } 71 | } 72 | 73 | public void write(String message) { 74 | try { 75 | mOutputStreamWriter.write(message); 76 | mOutputStreamWriter.flush(); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } 80 | } 81 | 82 | public void closeConnexion(){ 83 | if(mSocket != null){ 84 | try{ 85 | mInputStream.close(); 86 | mInputStream = null; 87 | mOutputStreamWriter.close(); 88 | mOutputStreamWriter = null; 89 | mSocket.close(); 90 | mSocket = null; 91 | mServerSocket.close(); 92 | }catch(Exception e){} 93 | CONTINUE_READ_WRITE = false; 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/activity/BluetoothActivity.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.activity; 2 | 3 | import android.app.Activity; 4 | import android.bluetooth.BluetoothDevice; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | 8 | import com.ramimartin.bluetooth.BluetoothManager; 9 | import com.ramimartin.bluetooth.bus.BluetoothCommunicator; 10 | import com.ramimartin.bluetooth.bus.BondedDevice; 11 | import com.ramimartin.bluetooth.bus.ClientConnectionFail; 12 | import com.ramimartin.bluetooth.bus.ClientConnectionSuccess; 13 | import com.ramimartin.bluetooth.bus.ServeurConnectionFail; 14 | import com.ramimartin.bluetooth.bus.ServeurConnectionSuccess; 15 | 16 | import java.util.UUID; 17 | 18 | import de.greenrobot.event.EventBus; 19 | 20 | /** 21 | * Created by Rami MARTIN on 13/04/2014. 22 | */ 23 | public abstract class BluetoothActivity extends Activity { 24 | 25 | protected BluetoothManager mBluetoothManager; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | mBluetoothManager = new BluetoothManager(this); 31 | checkBluetoothAviability(); 32 | } 33 | 34 | @Override 35 | protected void onStart() { 36 | super.onStart(); 37 | if(!EventBus.getDefault().isRegistered(this)) 38 | EventBus.getDefault().register(this); 39 | mBluetoothManager.setUUID(myUUID()); 40 | } 41 | 42 | @Override 43 | protected void onDestroy() { 44 | super.onDestroy(); 45 | EventBus.getDefault().unregister(this); 46 | closeAllConnexion(); 47 | } 48 | 49 | @Override 50 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 51 | super.onActivityResult(requestCode, resultCode, data); 52 | if (requestCode == BluetoothManager.REQUEST_DISCOVERABLE_CODE) { 53 | if (resultCode == BluetoothManager.BLUETOOTH_REQUEST_REFUSED) { 54 | } else if (resultCode == BluetoothManager.BLUETOOTH_REQUEST_ACCEPTED) { 55 | onBluetoothStartDiscovery(); 56 | } else { 57 | } 58 | } 59 | } 60 | 61 | public void closeAllConnexion(){ 62 | mBluetoothManager.closeAllConnexion(); 63 | } 64 | 65 | public void checkBluetoothAviability(){ 66 | if(!mBluetoothManager.checkBluetoothAviability()){ 67 | onBluetoothNotAviable(); 68 | } 69 | } 70 | 71 | public void setTimeDiscoverable(int timeInSec){ 72 | mBluetoothManager.setTimeDiscoverable(timeInSec); 73 | } 74 | 75 | public void startDiscovery(){ 76 | mBluetoothManager.startDiscovery(); 77 | } 78 | 79 | public void scanAllBluetoothDevice(){ 80 | mBluetoothManager.scanAllBluetoothDevice(); 81 | } 82 | 83 | public void createServeur(){ 84 | mBluetoothManager.createServeur(); 85 | } 86 | 87 | public void createClient(String addressMac){ 88 | mBluetoothManager.createClient(addressMac); 89 | } 90 | 91 | public void sendMessage(String message){ 92 | mBluetoothManager.sendMessage(message); 93 | } 94 | 95 | public abstract UUID myUUID(); 96 | public abstract void onBluetoothDeviceFound(BluetoothDevice device); 97 | public abstract void onClientConnectionSuccess(); 98 | public abstract void onClientConnectionFail(); 99 | public abstract void onServeurConnectionSuccess(); 100 | public abstract void onServeurConnectionFail(); 101 | public abstract void onBluetoothStartDiscovery(); 102 | public abstract void onBluetoothCommunicator(String messageReceive); 103 | public abstract void onBluetoothNotAviable(); 104 | 105 | public void onEventMainThread(BluetoothDevice device){ 106 | onBluetoothDeviceFound(device); 107 | } 108 | 109 | public void onEventMainThread(ClientConnectionSuccess event){ 110 | mBluetoothManager.isConnected = true; 111 | onClientConnectionSuccess(); 112 | } 113 | 114 | public void onEventMainThread(ClientConnectionFail event){ 115 | mBluetoothManager.isConnected = false; 116 | onClientConnectionFail(); 117 | mBluetoothManager.resetClient(); 118 | } 119 | 120 | public void onEventMainThread(ServeurConnectionSuccess event){ 121 | mBluetoothManager.isConnected = true; 122 | onServeurConnectionSuccess(); 123 | } 124 | 125 | public void onEventMainThread(ServeurConnectionFail event){ 126 | mBluetoothManager.isConnected = false; 127 | onServeurConnectionFail(); 128 | mBluetoothManager.resetServer(); 129 | } 130 | 131 | public void onEventMainThread(BluetoothCommunicator event){ 132 | onBluetoothCommunicator(event.mMessageReceive); 133 | } 134 | 135 | public void onEventMainThread(BondedDevice event){ 136 | //mBluetoothManager.sendMessage("BondedDevice"); 137 | } 138 | 139 | } 140 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/activity/BluetoothFragmentActivity.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.activity; 2 | 3 | import android.bluetooth.BluetoothDevice; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v4.app.FragmentActivity; 7 | 8 | import com.ramimartin.bluetooth.BluetoothManager; 9 | import com.ramimartin.bluetooth.bus.BluetoothCommunicator; 10 | import com.ramimartin.bluetooth.bus.BondedDevice; 11 | import com.ramimartin.bluetooth.bus.ClientConnectionFail; 12 | import com.ramimartin.bluetooth.bus.ClientConnectionSuccess; 13 | import com.ramimartin.bluetooth.bus.ServeurConnectionFail; 14 | import com.ramimartin.bluetooth.bus.ServeurConnectionSuccess; 15 | 16 | import java.util.UUID; 17 | 18 | import de.greenrobot.event.EventBus; 19 | 20 | /** 21 | * Created by Rami MARTIN on 13/04/2014. 22 | */ 23 | public abstract class BluetoothFragmentActivity extends FragmentActivity { 24 | 25 | protected BluetoothManager mBluetoothManager; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | mBluetoothManager = new BluetoothManager(this); 31 | checkBluetoothAviability(); 32 | } 33 | 34 | @Override 35 | protected void onStart() { 36 | super.onStart(); 37 | if(!EventBus.getDefault().isRegistered(this)) 38 | EventBus.getDefault().register(this); 39 | mBluetoothManager.setUUID(myUUID()); 40 | } 41 | 42 | @Override 43 | protected void onDestroy() { 44 | super.onDestroy(); 45 | EventBus.getDefault().unregister(this); 46 | closeAllConnexion(); 47 | } 48 | 49 | @Override 50 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 51 | super.onActivityResult(requestCode, resultCode, data); 52 | if (requestCode == BluetoothManager.REQUEST_DISCOVERABLE_CODE) { 53 | if (resultCode == BluetoothManager.BLUETOOTH_REQUEST_REFUSED) { 54 | finish(); 55 | } else if (resultCode == BluetoothManager.BLUETOOTH_REQUEST_ACCEPTED) { 56 | onBluetoothStartDiscovery(); 57 | } else { 58 | finish(); 59 | } 60 | } 61 | } 62 | 63 | public void closeAllConnexion(){ 64 | mBluetoothManager.closeAllConnexion(); 65 | } 66 | 67 | public void checkBluetoothAviability(){ 68 | if(!mBluetoothManager.checkBluetoothAviability()){ 69 | onBluetoothNotAviable(); 70 | } 71 | } 72 | 73 | public void setTimeDiscoverable(int timeInSec){ 74 | mBluetoothManager.setTimeDiscoverable(timeInSec); 75 | } 76 | 77 | public void startDiscovery(){ 78 | mBluetoothManager.startDiscovery(); 79 | } 80 | 81 | public void scanAllBluetoothDevice(){ 82 | mBluetoothManager.scanAllBluetoothDevice(); 83 | } 84 | 85 | public void createServeur(){ 86 | mBluetoothManager.createServeur(); 87 | } 88 | 89 | public void createClient(String addressMac){ 90 | mBluetoothManager.createClient(addressMac); 91 | } 92 | 93 | public void sendMessage(String message){ 94 | mBluetoothManager.sendMessage(message); 95 | } 96 | 97 | public abstract UUID myUUID(); 98 | public abstract void onBluetoothDeviceFound(BluetoothDevice device); 99 | public abstract void onClientConnectionSuccess(); 100 | public abstract void onClientConnectionFail(); 101 | public abstract void onServeurConnectionSuccess(); 102 | public abstract void onServeurConnectionFail(); 103 | public abstract void onBluetoothStartDiscovery(); 104 | public abstract void onBluetoothCommunicator(String messageReceive); 105 | public abstract void onBluetoothNotAviable(); 106 | 107 | public void onEventMainThread(BluetoothDevice device){ 108 | onBluetoothDeviceFound(device); 109 | } 110 | 111 | public void onEventMainThread(ClientConnectionSuccess event){ 112 | mBluetoothManager.isConnected = true; 113 | onClientConnectionSuccess(); 114 | } 115 | 116 | public void onEventMainThread(ClientConnectionFail event){ 117 | mBluetoothManager.isConnected = false; 118 | onClientConnectionFail(); 119 | } 120 | 121 | public void onEventMainThread(ServeurConnectionSuccess event){ 122 | mBluetoothManager.isConnected = true; 123 | onServeurConnectionSuccess(); 124 | } 125 | 126 | public void onEventMainThread(ServeurConnectionFail event){ 127 | mBluetoothManager.isConnected = false; 128 | onServeurConnectionFail(); 129 | } 130 | 131 | public void onEventMainThread(BluetoothCommunicator event){ 132 | onBluetoothCommunicator(event.mMessageReceive); 133 | } 134 | 135 | public void onEventMainThread(BondedDevice event){ 136 | //mBluetoothManager.sendMessage("BondedDevice"); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/bus/BluetoothCommunicator.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.bus; 2 | 3 | /** 4 | * Created by Rami MARTIN on 13/04/2014. 5 | */ 6 | public class BluetoothCommunicator { 7 | 8 | public String mMessageReceive; 9 | 10 | public BluetoothCommunicator(String messageReceive){ 11 | mMessageReceive = messageReceive; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/bus/BondedDevice.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.bus; 2 | 3 | /** 4 | * Created by Rami MARTIN on 21/04/2014. 5 | */ 6 | public class BondedDevice { 7 | } 8 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/bus/ClientConnectionFail.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.bus; 2 | 3 | /** 4 | * Created by Rami MARTIN on 13/04/2014. 5 | */ 6 | public class ClientConnectionFail { 7 | } 8 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/bus/ClientConnectionSuccess.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.bus; 2 | 3 | /** 4 | * Created by Rami MARTIN on 13/04/2014. 5 | */ 6 | public class ClientConnectionSuccess { 7 | } 8 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/bus/ServeurConnectionFail.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.bus; 2 | 3 | /** 4 | * Created by Rami MARTIN on 13/04/2014. 5 | */ 6 | public class ServeurConnectionFail { 7 | } 8 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/bus/ServeurConnectionSuccess.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.bus; 2 | 3 | /** 4 | * Created by Rami MARTIN on 13/04/2014. 5 | */ 6 | public class ServeurConnectionSuccess { 7 | } 8 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/java/com/ramimartin/bluetooth/fragment/BluetoothFragment.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.bluetooth.fragment; 2 | 3 | import android.bluetooth.BluetoothDevice; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v4.app.Fragment; 7 | 8 | import com.ramimartin.bluetooth.BluetoothManager; 9 | import com.ramimartin.bluetooth.bus.BluetoothCommunicator; 10 | import com.ramimartin.bluetooth.bus.BondedDevice; 11 | import com.ramimartin.bluetooth.bus.ClientConnectionFail; 12 | import com.ramimartin.bluetooth.bus.ClientConnectionSuccess; 13 | import com.ramimartin.bluetooth.bus.ServeurConnectionFail; 14 | import com.ramimartin.bluetooth.bus.ServeurConnectionSuccess; 15 | 16 | import java.util.UUID; 17 | 18 | import de.greenrobot.event.EventBus; 19 | 20 | /** 21 | * Created by Rami MARTIN on 13/04/2014. 22 | */ 23 | public abstract class BluetoothFragment extends Fragment { 24 | 25 | protected BluetoothManager mBluetoothManager; 26 | 27 | @Override 28 | public void onActivityCreated(Bundle savedInstanceState) { 29 | super.onActivityCreated(savedInstanceState); 30 | mBluetoothManager = new BluetoothManager(getActivity()); 31 | checkBluetoothAviability(); 32 | mBluetoothManager.setUUID(myUUID()); 33 | } 34 | 35 | @Override 36 | public void onCreate(Bundle savedInstanceState) { 37 | super.onCreate(savedInstanceState); 38 | if(!EventBus.getDefault().isRegistered(this)) 39 | EventBus.getDefault().register(this); 40 | } 41 | 42 | @Override 43 | public void onDestroy() { 44 | super.onDestroy(); 45 | EventBus.getDefault().unregister(this); 46 | closeAllConnexion(); 47 | } 48 | 49 | @Override 50 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 51 | super.onActivityResult(requestCode, resultCode, data); 52 | if (requestCode == BluetoothManager.REQUEST_DISCOVERABLE_CODE) { 53 | if (resultCode == BluetoothManager.BLUETOOTH_REQUEST_REFUSED) { 54 | getActivity().finish(); 55 | } else if (resultCode == BluetoothManager.BLUETOOTH_REQUEST_ACCEPTED) { 56 | onBluetoothStartDiscovery(); 57 | } else { 58 | getActivity().finish(); 59 | } 60 | } 61 | } 62 | 63 | public void closeAllConnexion(){ 64 | mBluetoothManager.closeAllConnexion(); 65 | } 66 | 67 | public void checkBluetoothAviability(){ 68 | if(!mBluetoothManager.checkBluetoothAviability()){ 69 | onBluetoothNotAviable(); 70 | } 71 | } 72 | 73 | public void setTimeDiscoverable(int timeInSec){ 74 | mBluetoothManager.setTimeDiscoverable(timeInSec); 75 | } 76 | 77 | public void startDiscovery(){ 78 | mBluetoothManager.startDiscovery(); 79 | } 80 | 81 | public void scanAllBluetoothDevice(){ 82 | mBluetoothManager.scanAllBluetoothDevice(); 83 | } 84 | 85 | public void createServeur(){ 86 | mBluetoothManager.createServeur(); 87 | } 88 | 89 | public void createClient(String addressMac){ 90 | mBluetoothManager.createClient(addressMac); 91 | } 92 | 93 | public void sendMessage(String message){ 94 | mBluetoothManager.sendMessage(message); 95 | } 96 | 97 | public abstract UUID myUUID(); 98 | public abstract void onBluetoothDeviceFound(BluetoothDevice device); 99 | public abstract void onClientConnectionSuccess(); 100 | public abstract void onClientConnectionFail(); 101 | public abstract void onServeurConnectionSuccess(); 102 | public abstract void onServeurConnectionFail(); 103 | public abstract void onBluetoothStartDiscovery(); 104 | public abstract void onBluetoothCommunicator(String messageReceive); 105 | public abstract void onBluetoothNotAviable(); 106 | 107 | public void onEventMainThread(BluetoothDevice device){ 108 | onBluetoothDeviceFound(device); 109 | } 110 | 111 | public void onEventMainThread(ClientConnectionSuccess event){ 112 | mBluetoothManager.isConnected = true; 113 | onClientConnectionSuccess(); 114 | } 115 | 116 | public void onEventMainThread(ClientConnectionFail event){ 117 | mBluetoothManager.isConnected = false; 118 | onClientConnectionFail(); 119 | } 120 | 121 | public void onEventMainThread(ServeurConnectionSuccess event){ 122 | mBluetoothManager.isConnected = true; 123 | onServeurConnectionSuccess(); 124 | } 125 | 126 | public void onEventMainThread(ServeurConnectionFail event){ 127 | mBluetoothManager.isConnected = false; 128 | onServeurConnectionFail(); 129 | } 130 | 131 | public void onEventMainThread(BluetoothCommunicator event){ 132 | onBluetoothCommunicator(event.mMessageReceive); 133 | } 134 | 135 | public void onEventMainThread(BondedDevice event){ 136 | //mBluetoothManager.sendMessage("BondedDevice"); 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_lib/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_lib/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_lib/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_lib/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_lib/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Bluetooth 3 | 4 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/Bluetooth-Bluetooth.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/Bluetooth_sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android' 2 | 3 | android { 4 | compileSdkVersion 19 5 | buildToolsVersion "19.0.3" 6 | 7 | packagingOptions { 8 | exclude 'META-INF/services/javax.annotation.processing.Processor' 9 | exclude 'META-INF/LICENSE.txt' 10 | exclude 'META-INF/LICENSE' 11 | exclude 'META-INF/NOTICE' 12 | } 13 | 14 | defaultConfig { 15 | minSdkVersion 9 16 | targetSdkVersion 19 17 | versionCode 1 18 | versionName "1.0" 19 | } 20 | buildTypes { 21 | release { 22 | runProguard false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 24 | } 25 | } 26 | } 27 | 28 | dependencies { 29 | compile 'com.android.support:appcompat-v7:19.+' 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | 32 | compile project(':Bluetooth_lib') 33 | compile 'de.greenrobot:eventbus:2.2.0' 34 | compile 'com.jakewharton:butterknife:4.0.1' 35 | } -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.ramimartin.bluetooth 4 | AndroidBluetoothSample 5 | apk 6 | 1.0 7 | Android Bluetooth Sample 8 | 9 | 10 | 11 | arissa34-ftp 12 | Arissa Ftp 13 | http://arissa34.free.fr/maven2 14 | 15 | 16 | 17 | 18 | 19 | com.jakewharton 20 | butterknife 21 | 5.0.0 22 | 23 | 24 | de.greenrobot 25 | eventbus 26 | 2.2.0 27 | 28 | 29 | com.google.android 30 | android 31 | 4.1.1.4 32 | provided 33 | 34 | 35 | com.google.android 36 | support-v4 37 | r6 38 | 39 | 40 | com.ramimartin.bluetooth 41 | AndroidBluetoothLibrary 42 | 1.0.0-SNAPSHOT 43 | 44 | 45 | 46 | 47 | 48 | 49 | com.jayway.maven.plugins.android.generation2 50 | android-maven-plugin 51 | true 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/proguard-rules.txt: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:/android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the ProGuard 5 | # include property in project.properties. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/java/com/ramimartin/sample/bluetooth/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.ramimartin.sample.bluetooth; 2 | 3 | import android.bluetooth.BluetoothDevice; 4 | import android.os.Bundle; 5 | import android.text.TextUtils; 6 | import android.widget.Button; 7 | import android.widget.EditText; 8 | import android.widget.TextView; 9 | 10 | import com.ramimartin.bluetooth.activity.BluetoothActivity; 11 | 12 | import java.util.UUID; 13 | 14 | import butterknife.ButterKnife; 15 | import butterknife.InjectView; 16 | import butterknife.OnClick; 17 | 18 | 19 | public class MainActivity extends BluetoothActivity { 20 | 21 | @InjectView(R.id.log_txt) 22 | TextView mLogTxt; 23 | @InjectView(R.id.scan) 24 | Button mScanBtn; 25 | @InjectView(R.id.send) 26 | Button mSendBtn; 27 | @InjectView(R.id.client) 28 | Button mClientBtn; 29 | @InjectView(R.id.serveur) 30 | Button mServeurBtn; 31 | @InjectView(R.id.communication) 32 | EditText mEditText; 33 | @InjectView(R.id.discovery) 34 | Button mDiscovery; 35 | 36 | @Override 37 | protected void onCreate(Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setContentView(R.layout.activity_main); 40 | ButterKnife.inject(this); 41 | } 42 | 43 | @Override 44 | protected void onStart() { 45 | super.onStart(); 46 | } 47 | 48 | @Override 49 | public UUID myUUID() { 50 | return UUID.fromString("c47914a1-c8a2-45f0-a900-bc686b9328ab"); 51 | } 52 | 53 | @OnClick(R.id.discovery) 54 | public void discovery() { 55 | setTimeDiscoverable(200); 56 | startDiscovery(); 57 | } 58 | 59 | @OnClick(R.id.serveur) 60 | public void serveur() { 61 | setLogText("===> Start Serveur ..."); 62 | createServeur(); 63 | } 64 | 65 | @OnClick(R.id.client) 66 | public void client() { 67 | if (!TextUtils.isEmpty(mEditText.getText().toString())) { 68 | setLogText("===> Start Client connexion on device : " + mEditText.getText().toString()); 69 | createClient(mEditText.getText().toString()); 70 | } 71 | } 72 | 73 | @OnClick(R.id.scan) 74 | public void scan() { 75 | setLogText("===> Start Scanning devices ..."); 76 | scanAllBluetoothDevice(); 77 | } 78 | 79 | @OnClick(R.id.send) 80 | public void send() { 81 | sendMessage(mEditText.getText().toString()); 82 | setLogText("===> Send : " + mEditText.getText().toString()); 83 | } 84 | 85 | @Override 86 | public void onBluetoothStartDiscovery() { 87 | mScanBtn.setEnabled(true); 88 | setLogText("===> Start discovering !"); 89 | mServeurBtn.setEnabled(true); 90 | } 91 | 92 | @Override 93 | public void onBluetoothDeviceFound(BluetoothDevice device) { 94 | setLogText("===> Device detected : " + device.getAddress()); 95 | mEditText.setText(device.getAddress()); 96 | mClientBtn.setEnabled(true); 97 | } 98 | 99 | @Override 100 | public void onClientConnectionSuccess() { 101 | setLogText("===> Client Connexion success !"); 102 | mEditText.setText(""); 103 | mSendBtn.setEnabled(true); 104 | } 105 | 106 | @Override 107 | public void onClientConnectionFail() { 108 | setLogText("===> Client Connexion fail !"); 109 | mClientBtn.setEnabled(false); 110 | } 111 | 112 | @Override 113 | public void onServeurConnectionSuccess() { 114 | setLogText("===> Serveur Connexion success !"); 115 | mEditText.setText(""); 116 | mSendBtn.setEnabled(true); 117 | } 118 | 119 | @Override 120 | public void onServeurConnectionFail() { 121 | setLogText("===> Serveur Connexion fail !"); 122 | } 123 | 124 | @Override 125 | public void onBluetoothCommunicator(String messageReceive) { 126 | setLogText("===> receive msg : " + messageReceive); 127 | } 128 | 129 | @Override 130 | public void onBluetoothNotAviable() { 131 | setLogText("===> Bluetooth not aviable on this device"); 132 | mDiscovery.setEnabled(false); 133 | mClientBtn.setEnabled(false); 134 | mSendBtn.setEnabled(false); 135 | mScanBtn.setEnabled(false); 136 | mServeurBtn.setEnabled(false); 137 | } 138 | 139 | public void setLogText(String text) { 140 | mLogTxt.setText(mLogTxt.getText() + "\n" + text); 141 | } 142 | 143 | } 144 | -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_sample/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_sample/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_sample/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/res/drawable-xhdpi/rami_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_sample/src/main/res/drawable-xhdpi/rami_logo.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arissa34/Android-Bluetooth-Library/de618333620afce46c7923a1d349780e6bff8198/Bluetooth/Bluetooth_sample/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Bluetooth/Bluetooth_sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 13 | 14 | 18 | 19 | 24 | 25 | 32 | 33 | 34 | 35 | 36 | 45 | 46 |