aboutsummaryrefslogtreecommitdiff
path: root/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java
blob: ba021cf0ef95d8e5fcf9335901ab56b8fcc99674 (plain)
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
/*
 * IBusIntentsService.java
 * Copyright (C) 2013 David Barksdale <amatus.amongus@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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 UUID16ServiceClassSerialPort =
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

  public IBusIntentsService() {
    super(Name);
  }

  @Override
  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
  public void onDestroy()
  {
    Log.d(Name, "onDestroy");
    super.onDestroy();
  }
}
/* vim: set expandtab ts=2 sw=2: */