1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | style="color: rgb(0, 128, 0);">/* SendReceiveBytes: Written by ScottC on 25 March 2013 using style="color: rgb(0, 128, 0);"> Processing version 2.0b8 style="color: rgb(0, 128, 0);"> Tested on a Samsung Galaxy SII, with Android version 2.3.4 style="color: rgb(0, 128, 0);"> Android ADK - API 10 SDK platform */
style="color: rgb(0, 0, 255);">import android.content.BroadcastReceiver; style="color: rgb(0, 0, 255);">import android.content.Context; style="color: rgb(0, 0, 255);">import android.content.Intent; style="color: rgb(0, 0, 255);">import android.content.IntentFilter; style="color: rgb(0, 0, 255);">import android.widget.Toast; style="color: rgb(0, 0, 255);">import android.view.Gravity; style="color: rgb(0, 0, 255);">import android.bluetooth.BluetoothAdapter; style="color: rgb(0, 0, 255);">import android.bluetooth.BluetoothDevice;
style="color: rgb(0, 0, 255);">import java.util.UUID; style="color: rgb(0, 0, 255);">import java.io.IOException; style="color: rgb(0, 0, 255);">import java.io.InputStream; style="color: rgb(0, 0, 255);">import java.io.OutputStream; style="color: rgb(0, 0, 255);">import android.os.Handler; style="color: rgb(0, 0, 255);">import android.os.Message; style="color: rgb(0, 0, 255);">import android.util.Log;
style="color: rgb(0, 0, 255);">import android.bluetooth.BluetoothServerSocket; style="color: rgb(0, 0, 255);">import android.bluetooth.BluetoothSocket; style="color: rgb(0, 0, 255);">public BluetoothSocket scSocket;
style="color: rgb(43, 145, 175);">boolean foundDevice=style="color: rgb(0, 0, 255);">false; style="color: rgb(0, 128, 0);">//When true, the screen turns green. style="color: rgb(43, 145, 175);">boolean BTisConnected=style="color: rgb(0, 0, 255);">false; style="color: rgb(0, 128, 0);">//When true, the screen turns purple. String serverName = style="color: rgb(163, 21, 21);">"ArduinoBasicsServer";
style="color: rgb(0, 128, 0);">// Message types used by the Handler style="color: rgb(0, 0, 255);">public style="color: rgb(0, 0, 255);">static style="color: rgb(0, 0, 255);">final style="color: rgb(43, 145, 175);">int MESSAGE_WRITE = 1; style="color: rgb(0, 0, 255);">public style="color: rgb(0, 0, 255);">static style="color: rgb(0, 0, 255);">final style="color: rgb(43, 145, 175);">int MESSAGE_READ = 2; String readMessage=style="color: rgb(163, 21, 21);">"";
style="color: rgb(0, 128, 0);">//Get the default Bluetooth adapter BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
style="color: rgb(0, 128, 0);">/*The startActivityForResult() within setup() launches an style="color: rgb(0, 128, 0);"> Activity which is used to request the user to turn Bluetooth on. style="color: rgb(0, 128, 0);"> The following onActivityResult() method is called when this style="color: rgb(0, 128, 0);"> Activity exits. */ @Override style="color: rgb(0, 0, 255);">protected style="color: rgb(43, 145, 175);">void onActivityResult(style="color: rgb(43, 145, 175);">int requestCode, style="color: rgb(43, 145, 175);">int resultCode, Intent data) { style="color: rgb(0, 0, 255);">if (requestCode==0) { style="color: rgb(0, 0, 255);">if (resultCode == RESULT_OK) { ToastMaster(style="color: rgb(163, 21, 21);">"Bluetooth has been switched ON"); } style="color: rgb(0, 0, 255);">else { ToastMaster(style="color: rgb(163, 21, 21);">"You need to turn Bluetooth ON !!!"); } } }
style="color: rgb(0, 128, 0);">/* Create a BroadcastReceiver that will later be used to style="color: rgb(0, 128, 0);"> receive the names of Bluetooth devices in range. */ BroadcastReceiver myDiscoverer = style="color: rgb(0, 0, 255);">new myOwnBroadcastReceiver();
style="color: rgb(0, 128, 0);">/* Create a BroadcastReceiver that will later be used to style="color: rgb(0, 128, 0);"> identify if the Bluetooth device is connected */ BroadcastReceiver checkIsConnected = style="color: rgb(0, 0, 255);">new myOwnBroadcastReceiver();
style="color: rgb(0, 128, 0);">// The Handler that gets information back from the Socket style="color: rgb(0, 0, 255);">private style="color: rgb(0, 0, 255);">final Handler mHandler = style="color: rgb(0, 0, 255);">new Handler() { @Override style="color: rgb(0, 0, 255);">public style="color: rgb(43, 145, 175);">void handleMessage(Message msg) { style="color: rgb(0, 0, 255);">switch (msg.what) { style="color: rgb(0, 0, 255);">case MESSAGE_WRITE: style="color: rgb(0, 128, 0);">//Do something when writing style="color: rgb(0, 0, 255);">break; style="color: rgb(0, 0, 255);">case MESSAGE_READ: style="color: rgb(0, 128, 0);">//Get the bytes from the msg.obj style="color: rgb(43, 145, 175);">byte[] readBuf = (style="color: rgb(43, 145, 175);">byte[]) msg.obj; style="color: rgb(0, 128, 0);">// construct a string from the valid bytes in the buffer readMessage = style="color: rgb(0, 0, 255);">new String(readBuf, 0, msg.arg1); style="color: rgb(0, 0, 255);">break; } } };
style="color: rgb(43, 145, 175);">void setup() { orientation(LANDSCAPE); style="color: rgb(0, 128, 0);">/*IF Bluetooth is NOT enabled, then ask user permission to enable it */ style="color: rgb(0, 0, 255);">if (!bluetooth.isEnabled()) { Intent requestBluetooth = style="color: rgb(0, 0, 255);">new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(requestBluetooth, 0); }
style="color: rgb(0, 128, 0);">/*If Bluetooth is now enabled, then register a broadcastReceiver to report any style="color: rgb(0, 128, 0);"> discovered Bluetooth devices, and then start discovering */ style="color: rgb(0, 0, 255);">if (bluetooth.isEnabled()) { registerReceiver(myDiscoverer, style="color: rgb(0, 0, 255);">new IntentFilter(BluetoothDevice.ACTION_FOUND)); registerReceiver(checkIsConnected, style="color: rgb(0, 0, 255);">new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));
style="color: rgb(0, 128, 0);">//Start bluetooth discovery if it is not doing so already style="color: rgb(0, 0, 255);">if (!bluetooth.isDiscovering()) { bluetooth.startDiscovery(); } } }
style="color: rgb(43, 145, 175);">void draw() { style="color: rgb(0, 128, 0);">//Display a green screen if a device has been found, style="color: rgb(0, 128, 0);">//Display a purple screen when a connection is made to the device style="color: rgb(0, 0, 255);">if (foundDevice) { style="color: rgb(0, 0, 255);">if (BTisConnected) { background(170, 50, 255); style="color: rgb(0, 128, 0);">// purple screen } style="color: rgb(0, 0, 255);">else { background(10, 255, 10); style="color: rgb(0, 128, 0);">// green screen } }
style="color: rgb(0, 128, 0);">//Display anything received from Arduino text(readMessage, 10, 10); }
style="color: rgb(0, 128, 0);">/* This BroadcastReceiver will display discovered Bluetooth devices */ style="color: rgb(0, 0, 255);">public style="color: rgb(0, 0, 255);">class style="color: rgb(43, 145, 175);">myOwnBroadcastReceiver style="color: rgb(0, 0, 255);">extends BroadcastReceiver { ConnectToBluetooth connectBT;
@Override style="color: rgb(0, 0, 255);">public style="color: rgb(43, 145, 175);">void onReceive(Context context, Intent intent) { String action=intent.getAction(); ToastMaster(style="color: rgb(163, 21, 21);">"ACTION:" + action);
style="color: rgb(0, 128, 0);">//Notification that BluetoothDevice is FOUND style="color: rgb(0, 0, 255);">if (BluetoothDevice.ACTION_FOUND.equals(action)) { style="color: rgb(0, 128, 0);">//Display the name of the discovered device String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME); ToastMaster(style="color: rgb(163, 21, 21);">"Discovered: " + discoveredDeviceName);
style="color: rgb(0, 128, 0);">//Display more information about the discovered device BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); ToastMaster(style="color: rgb(163, 21, 21);">"getAddress() = " + discoveredDevice.getAddress()); ToastMaster(style="color: rgb(163, 21, 21);">"getName() = " + discoveredDevice.getName());
style="color: rgb(43, 145, 175);">int bondyState=discoveredDevice.getBondState(); ToastMaster(style="color: rgb(163, 21, 21);">"getBondState() = " + bondyState);
String mybondState; style="color: rgb(0, 0, 255);">switch(bondyState) { style="color: rgb(0, 0, 255);">case 10: mybondState=style="color: rgb(163, 21, 21);">"BOND_NONE"; style="color: rgb(0, 0, 255);">break; style="color: rgb(0, 0, 255);">case 11: mybondState=style="color: rgb(163, 21, 21);">"BOND_BONDING"; style="color: rgb(0, 0, 255);">break; style="color: rgb(0, 0, 255);">case 12: mybondState=style="color: rgb(163, 21, 21);">"BOND_BONDED"; style="color: rgb(0, 0, 255);">break; style="color: rgb(0, 0, 255);">default: mybondState=style="color: rgb(163, 21, 21);">"INVALID BOND STATE"; style="color: rgb(0, 0, 255);">break; } ToastMaster(style="color: rgb(163, 21, 21);">"getBondState() = " + mybondState);
style="color: rgb(0, 128, 0);">//Change foundDevice to true which will make the screen turn green foundDevice=style="color: rgb(0, 0, 255);">true;
style="color: rgb(0, 128, 0);">//Connect to the discovered bluetooth device (SeeedBTSlave) style="color: rgb(0, 0, 255);">if (discoveredDeviceName.equals(style="color: rgb(163, 21, 21);">"SeeedBTSlave")) { ToastMaster(style="color: rgb(163, 21, 21);">"Connecting you Now !!"); unregisterReceiver(myDiscoverer); connectBT = style="color: rgb(0, 0, 255);">new ConnectToBluetooth(discoveredDevice); style="color: rgb(0, 128, 0);">//Connect to the the device in a new thread style="color: rgb(0, 0, 255);">new Thread(connectBT).start(); } }
style="color: rgb(0, 128, 0);">//Notification if bluetooth device is connected style="color: rgb(0, 0, 255);">if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { ToastMaster(style="color: rgb(163, 21, 21);">"CONNECTED _ YAY");
style="color: rgb(0, 0, 255);">while (scSocket==style="color: rgb(0, 0, 255);">null) { style="color: rgb(0, 128, 0);">//do nothing } ToastMaster(style="color: rgb(163, 21, 21);">"scSocket" + scSocket); BTisConnected=style="color: rgb(0, 0, 255);">true; style="color: rgb(0, 128, 0);">//turn screen purple style="color: rgb(0, 0, 255);">if (scSocket!=style="color: rgb(0, 0, 255);">null) { SendReceiveBytes sendReceiveBT = style="color: rgb(0, 0, 255);">new SendReceiveBytes(scSocket); style="color: rgb(0, 0, 255);">new Thread(sendReceiveBT).start(); String red = style="color: rgb(163, 21, 21);">"r"; style="color: rgb(43, 145, 175);">byte[] myByte = stringToBytesUTFCustom(red); sendReceiveBT.write(myByte); } } } } style="color: rgb(0, 0, 255);">public style="color: rgb(0, 0, 255);">static style="color: rgb(43, 145, 175);">byte[] stringToBytesUTFCustom(String str) { style="color: rgb(43, 145, 175);">char[] buffer = str.toCharArray(); style="color: rgb(43, 145, 175);">byte[] b = style="color: rgb(0, 0, 255);">new style="color: rgb(43, 145, 175);">byte[buffer.length << 1]; style="color: rgb(0, 0, 255);">for (style="color: rgb(43, 145, 175);">int i = 0; i < buffer.length; i++) { style="color: rgb(43, 145, 175);">int bpos = i << 1; b[bpos] = (style="color: rgb(43, 145, 175);">byte) ((buffer[i]&0xFF00)>>8); b[bpos + 1] = (style="color: rgb(43, 145, 175);">byte) (buffer[i]&0x00FF); } style="color: rgb(0, 0, 255);">return b; }
style="color: rgb(0, 0, 255);">public style="color: rgb(0, 0, 255);">class style="color: rgb(43, 145, 175);">ConnectToBluetooth style="color: rgb(0, 0, 255);">implements Runnable { style="color: rgb(0, 0, 255);">private BluetoothDevice btShield; style="color: rgb(0, 0, 255);">private BluetoothSocket mySocket = style="color: rgb(0, 0, 255);">null; style="color: rgb(0, 0, 255);">private UUID uuid = UUID.fromString(style="color: rgb(163, 21, 21);">"00001101-0000-1000-8000-00805F9B34FB");
style="color: rgb(0, 0, 255);">public ConnectToBluetooth(BluetoothDevice bluetoothShield) { btShield = bluetoothShield; style="color: rgb(0, 0, 255);">try { mySocket = btShield.createRfcommSocketToServiceRecord(uuid); } style="color: rgb(0, 0, 255);">catch(IOException createSocketException) { style="color: rgb(0, 128, 0);">//Problem with creating a socket Log.e(style="color: rgb(163, 21, 21);">"ConnectToBluetooth", style="color: rgb(163, 21, 21);">"Error with Socket"); } }
@Override style="color: rgb(0, 0, 255);">public style="color: rgb(43, 145, 175);">void run() { style="color: rgb(0, 128, 0);">/* Cancel discovery on Bluetooth Adapter to prevent slow connection */ bluetooth.cancelDiscovery();
style="color: rgb(0, 0, 255);">try { style="color: rgb(0, 128, 0);">/*Connect to the bluetoothShield through the Socket. This will block style="color: rgb(0, 128, 0);"> until it succeeds or throws an IOException */ mySocket.connect(); scSocket=mySocket; } style="color: rgb(0, 0, 255);">catch (IOException connectException) { Log.e(style="color: rgb(163, 21, 21);">"ConnectToBluetooth", style="color: rgb(163, 21, 21);">"Error with Socket Connection"); style="color: rgb(0, 0, 255);">try { mySocket.close(); style="color: rgb(0, 128, 0);">//try to close the socket } style="color: rgb(0, 0, 255);">catch(IOException closeException) { } style="color: rgb(0, 0, 255);">return; } }
style="color: rgb(0, 128, 0);">/* Will cancel an in-progress connection, and close the socket */ style="color: rgb(0, 0, 255);">public style="color: rgb(43, 145, 175);">void cancel() { style="color: rgb(0, 0, 255);">try { mySocket.close(); } style="color: rgb(0, 0, 255);">catch (IOException e) { } } }
style="color: rgb(0, 0, 255);">private style="color: rgb(0, 0, 255);">class style="color: rgb(43, 145, 175);">SendReceiveBytes style="color: rgb(0, 0, 255);">implements Runnable { style="color: rgb(0, 0, 255);">private BluetoothSocket btSocket; style="color: rgb(0, 0, 255);">private InputStream btInputStream = style="color: rgb(0, 0, 255);">null; style="color: rgb(0, 0, 255);">private OutputStream btOutputStream = style="color: rgb(0, 0, 255);">null; String TAG = style="color: rgb(163, 21, 21);">"SendReceiveBytes";
style="color: rgb(0, 0, 255);">public SendReceiveBytes(BluetoothSocket socket) { btSocket = socket; style="color: rgb(0, 0, 255);">try { btInputStream = btSocket.getInputStream(); btOutputStream = btSocket.getOutputStream(); } style="color: rgb(0, 0, 255);">catch (IOException streamError) { Log.e(TAG, style="color: rgb(163, 21, 21);">"Error when getting input or output Stream"); } }
style="color: rgb(0, 0, 255);">public style="color: rgb(43, 145, 175);">void run() { style="color: rgb(43, 145, 175);">byte[] buffer = style="color: rgb(0, 0, 255);">new style="color: rgb(43, 145, 175);">byte[1024]; style="color: rgb(0, 128, 0);">// buffer store for the stream style="color: rgb(43, 145, 175);">int bytes; style="color: rgb(0, 128, 0);">// bytes returned from read()
style="color: rgb(0, 128, 0);">// Keep listening to the InputStream until an exception occurs style="color: rgb(0, 0, 255);">while (style="color: rgb(0, 0, 255);">true) { style="color: rgb(0, 0, 255);">try { style="color: rgb(0, 128, 0);">// Read from the InputStream bytes = btInputStream.read(buffer); style="color: rgb(0, 128, 0);">// Send the obtained bytes to the UI activity mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } style="color: rgb(0, 0, 255);">catch (IOException e) { Log.e(TAG, style="color: rgb(163, 21, 21);">"Error reading from btInputStream"); style="color: rgb(0, 0, 255);">break; } } }
style="color: rgb(0, 128, 0);">/* Call this from the main activity to send data to the remote device */ style="color: rgb(0, 0, 255);">public |