diff options
author | David Barksdale <amatus.amongus@gmail.com> | 2013-05-18 00:00:56 -0500 |
---|---|---|
committer | David Barksdale <amatus.amongus@gmail.com> | 2013-05-18 00:00:56 -0500 |
commit | d060e56a3c578622e74af6b9be8903eb763391bc (patch) | |
tree | 94931187a245fca19ca54d3f7b43fe08d2282239 | |
parent | b34eb07e7b541bb9b2043c42746d3874c15c7ab6 (diff) |
Connect to BT2S and read from it.
-rw-r--r-- | android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java b/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java index 0dbf586..ba021cf 100644 --- a/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java +++ b/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java @@ -19,14 +19,20 @@ package name.amatus.ibusintents; import android.app.IntentService; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothSocket; import android.content.Intent; import android.util.Log; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.UUID; public class IBusIntentsService extends IntentService { private static final String Name = "IBusIntentsService"; - private static final UUID BluetoothSDPUUID16ServiceClassSerialPort = + private static final UUID UUID16ServiceClassSerialPort = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); public IBusIntentsService() { @@ -37,6 +43,42 @@ public class IBusIntentsService extends IntentService protected void onHandleIntent(Intent intent) { Log.d(Name, "onHandleIntent(intent=" + intent.toString() + ")"); + if (!intent.hasExtra("device")) { + Log.w(Name, "Intent has no device"); + return; + } + BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + if (adapter == null) { + Log.w(Name, "Unable to get default bluetooth adapter"); + return; + } + BluetoothDevice device = + adapter.getRemoteDevice(intent.getStringExtra("device")); + BluetoothSocket socket; + InputStream stream; + try { + socket = + device.createRfcommSocketToServiceRecord(UUID16ServiceClassSerialPort); + socket.connect(); + stream = socket.getInputStream(); + while (true) { + byte[] buffer = new byte[256]; + int length = stream.read(buffer); + if (-1 == length) { + Log.w(Name, "Read failed"); + break; + } + String hex = new String(); + for (int i = 0; i < length; ++i) { + hex += String.format(" %02X", buffer[i]); + } + Log.d(Name, "I-Bus says:" + hex); + } + socket.close(); + } catch (IOException exception) { + Log.w(Name, "Exception: " + exception.toString()); + return; + } } @Override |