AI2 Component  (Version nb184)
BluetoothReflection.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 // Even though the code doesn't need these imports, javadoc does.
10 import android.bluetooth.BluetoothAdapter;
11 import android.bluetooth.BluetoothClass;
12 import android.bluetooth.BluetoothDevice;
13 import android.bluetooth.BluetoothServerSocket;
14 import android.bluetooth.BluetoothSocket;
15 
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21 import java.util.Set;
22 import java.util.UUID;
23 
29 public class BluetoothReflection {
30  private static final int BOND_BONDED = 0xC; // from android.bluetooth.BluetoothDevice
31 
32  private BluetoothReflection() {
33  }
34 
35  // BluetoothAdapter methods
36 
44  public static Object getBluetoothAdapter() {
45  // return BluetoothAdapter.getDefaultAdapter();
46  Class bluetoothAdapterClass;
47  try {
48  bluetoothAdapterClass = Class.forName("android.bluetooth.BluetoothAdapter");
49  } catch (ClassNotFoundException e) {
50  // Bluetooth is not available on this Android device.
51  return null;
52  }
53  return invokeStaticMethod(getMethod(bluetoothAdapterClass, "getDefaultAdapter"));
54  }
55 
63  public static boolean isBluetoothEnabled(Object bluetoothAdapter) {
64  // boolean enabled = bluetoothAdapter.isEnabled();
65  return (Boolean) invokeMethod(getMethod(bluetoothAdapter.getClass(), "isEnabled"),
66  bluetoothAdapter);
67  }
68 
76  public static Set getBondedDevices(Object bluetoothAdapter) {
77  // return bluetoothAdapter.getBondedDevices();
78  return (Set) invokeMethod(getMethod(bluetoothAdapter.getClass(), "getBondedDevices"),
79  bluetoothAdapter);
80  }
81 
90  public static boolean checkBluetoothAddress(Object bluetoothAdapter, String address) {
91  // return bluetoothAdapter.checkBluetoothAddress(address);
92  return (Boolean) invokeMethod(
93  getMethod(bluetoothAdapter.getClass(), "checkBluetoothAddress", String.class),
94  bluetoothAdapter, address);
95  }
96 
105  public static Object getRemoteDevice(Object bluetoothAdapter, String address)
106  throws IllegalArgumentException {
107  // return bluetoothAdapter.getRemoteDevice(address);
108  return invokeMethodThrowsIllegalArgumentException(
109  getMethod(bluetoothAdapter.getClass(), "getRemoteDevice", String.class),
110  bluetoothAdapter, address);
111  }
112 
122  public static Object listenUsingRfcommWithServiceRecord(Object bluetoothAdapter, String name,
123  UUID uuid) throws IOException {
124  // return bluetoothAdapter.listenUsingRfcommWithServiceRecord(name, uuid);
125  return invokeMethodThrowsIOException(getMethod(bluetoothAdapter.getClass(),
126  "listenUsingRfcommWithServiceRecord", String.class, UUID.class),
127  bluetoothAdapter, name, uuid);
128  }
129 
130  // Note: The following @link generates a Javadoc warning because we build against a
131  // version of the jar that doesn't yet have the method, which was added in API level 10.
142  Object bluetoothAdapter, String name, UUID uuid) throws IOException {
143  // return bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(name, uuid);
144  return invokeMethodThrowsIOException(getMethod(bluetoothAdapter.getClass(),
145  "listenUsingInsecureRfcommWithServiceRecord", String.class, UUID.class),
146  bluetoothAdapter, name, uuid);
147  }
148 
149  // BluetoothDevice methods
150 
158  public static String getBluetoothDeviceName(Object bluetoothDevice) {
159  // return bluetoothDevice.getName();
160  return (String) invokeMethod(getMethod(bluetoothDevice.getClass(), "getName"),
161  bluetoothDevice);
162  }
163 
171  public static String getBluetoothDeviceAddress(Object bluetoothDevice) {
172  // return bluetoothDevice.getAddress();
173  return (String) invokeMethod(getMethod(bluetoothDevice.getClass(), "getAddress"),
174  bluetoothDevice);
175  }
176 
184  public static boolean isBonded(Object bluetoothDevice) {
185  // return bluetoothDevice.getBondState() == BOND_BONDED;
186  int bondState = (Integer) invokeMethod(getMethod(bluetoothDevice.getClass(), "getBondState"),
187  bluetoothDevice);
188  return bondState == BOND_BONDED;
189  }
190 
198  public static Object getBluetoothClass(Object bluetoothDevice) {
199  // return bluetoothDevice.getBluetoothClass();
200  return invokeMethod(getMethod(bluetoothDevice.getClass(), "getBluetoothClass"),
201  bluetoothDevice);
202  }
203 
212  public static Object createRfcommSocketToServiceRecord(Object bluetoothDevice, UUID uuid)
213  throws IOException {
214  // return bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
215  return invokeMethodThrowsIOException(
216  getMethod(bluetoothDevice.getClass(), "createRfcommSocketToServiceRecord", UUID.class),
217  bluetoothDevice, uuid);
218  }
219 
220  // Note: The following @link generates a Javadoc warning because we build against a
221  // version of the jar that doesn't yet have the method, which was added in API level 10.
230  public static Object createInsecureRfcommSocketToServiceRecord(Object bluetoothDevice, UUID uuid)
231  throws IOException {
232  // return bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
233  return invokeMethodThrowsIOException(
234  getMethod(bluetoothDevice.getClass(),
235  "createInsecureRfcommSocketToServiceRecord", UUID.class),
236  bluetoothDevice, uuid);
237  }
238 
239  // BluetoothClass methods
240 
249  public static int getDeviceClass(Object bluetoothClass) {
250  // return bluetoothClass.getBluetoothClass();
251  return (Integer) invokeMethod(getMethod(bluetoothClass.getClass(), "getDeviceClass"),
252  bluetoothClass);
253  }
254 
255  // BluetoothSocket methods
256 
263  public static void connectToBluetoothSocket(Object bluetoothSocket) throws IOException {
264  // bluetoothSocket.connect();
265  invokeMethodThrowsIOException(getMethod(bluetoothSocket.getClass(), "connect"),
266  bluetoothSocket);
267  }
268 
275  public static boolean isBluetoothSocketConnected(Object bluetoothSocket) {
276  return (Boolean)invokeMethod(getMethod(bluetoothSocket.getClass(), "isConnected"),
277  bluetoothSocket);
278  }
279 
287  public static InputStream getInputStream(Object bluetoothSocket) throws IOException {
288  // return bluetoothSocket.getInputStream();
289  return (InputStream) invokeMethodThrowsIOException(
290  getMethod(bluetoothSocket.getClass(), "getInputStream"),
291  bluetoothSocket);
292  }
293 
301  public static OutputStream getOutputStream(Object bluetoothSocket) throws IOException {
302  // return bluetoothSocket.getOutputStream();
303  return (OutputStream) invokeMethodThrowsIOException(
304  getMethod(bluetoothSocket.getClass(), "getOutputStream"),
305  bluetoothSocket);
306  }
307 
314  public static void closeBluetoothSocket(Object bluetoothSocket) throws IOException {
315  // bluetoothSocket.close();
316  invokeMethodThrowsIOException(getMethod(bluetoothSocket.getClass(), "close"),
317  bluetoothSocket);
318  }
319 
320  // BluetoothServerSocket methods
321 
328  public static Object accept(Object bluetoothServerSocket) throws IOException {
329  // return bluetoothServerSocket.accept();
330  return invokeMethodThrowsIOException(getMethod(bluetoothServerSocket.getClass(), "accept"),
331  bluetoothServerSocket);
332  }
333 
339  public static void closeBluetoothServerSocket(Object bluetoothServerSocket) throws IOException {
340  // bluetoothServerSocket.close();
341  invokeMethodThrowsIOException(getMethod(bluetoothServerSocket.getClass(), "close"),
342  bluetoothServerSocket);
343  }
344 
345 
346  // Reflection helper methods
347 
348  private static Method getMethod(Class clazz, String name) {
349  try {
350  return clazz.getMethod(name, new Class[0]);
351  } catch (NoSuchMethodException e) {
352  throw new RuntimeException(e);
353  }
354  }
355 
356  private static Method getMethod(Class clazz, String name, Class<?>... parameterTypes) {
357  try {
358  return clazz.getMethod(name, parameterTypes);
359  } catch (NoSuchMethodException e) {
360  throw new RuntimeException(e);
361  }
362  }
363 
364  private static Object invokeStaticMethod(Method method) {
365  try {
366  return method.invoke(null);
367  } catch (IllegalAccessException e) {
368  throw new RuntimeException(e);
369  } catch (InvocationTargetException e) {
370  Throwable cause = e.getCause();
371  cause.printStackTrace();
372  if (cause instanceof RuntimeException) {
373  throw (RuntimeException) cause;
374  } else {
375  throw new RuntimeException(cause);
376  }
377  }
378  }
379 
380  private static Object invokeMethod(Method method, Object thisObject, Object... args) {
381  try {
382  return method.invoke(thisObject, args);
383  } catch (IllegalAccessException e) {
384  throw new RuntimeException(e);
385  } catch (InvocationTargetException e) {
386  Throwable cause = e.getCause();
387  cause.printStackTrace();
388  if (cause instanceof RuntimeException) {
389  throw (RuntimeException) cause;
390  } else {
391  throw new RuntimeException(cause);
392  }
393  }
394  }
395 
396  private static Object invokeMethodThrowsIllegalArgumentException(Method method,
397  Object thisObject, Object... args) throws IllegalArgumentException {
398  try {
399  return method.invoke(thisObject, args);
400  } catch (IllegalAccessException e) {
401  throw new RuntimeException(e);
402  } catch (InvocationTargetException e) {
403  Throwable cause = e.getCause();
404  cause.printStackTrace();
405  if (cause instanceof IllegalArgumentException) {
406  throw (IllegalArgumentException) cause;
407  } else if (cause instanceof RuntimeException) {
408  throw (RuntimeException) cause;
409  } else {
410  throw new RuntimeException(e);
411  }
412  }
413  }
414 
415  private static Object invokeMethodThrowsIOException(Method method, Object thisObject,
416  Object... args) throws IOException {
417  try {
418  return method.invoke(thisObject, args);
419  } catch (IllegalAccessException e) {
420  throw new RuntimeException(e);
421  } catch (InvocationTargetException e) {
422  Throwable cause = e.getCause();
423  cause.printStackTrace();
424  if (cause instanceof IOException) {
425  throw (IOException) cause;
426  } else if (cause instanceof RuntimeException) {
427  throw (RuntimeException) cause;
428  } else {
429  throw new RuntimeException(e);
430  }
431  }
432  }
433 }
com.google.appinventor.components.runtime.util.BluetoothReflection.getBluetoothDeviceAddress
static String getBluetoothDeviceAddress(Object bluetoothDevice)
Definition: BluetoothReflection.java:171
com.google.appinventor.components.runtime.util.BluetoothReflection.accept
static Object accept(Object bluetoothServerSocket)
Definition: BluetoothReflection.java:328
com.google.appinventor.components.runtime.util.BluetoothReflection.getBluetoothClass
static Object getBluetoothClass(Object bluetoothDevice)
Definition: BluetoothReflection.java:198
com.google.appinventor.components.runtime.util.BluetoothReflection.closeBluetoothSocket
static void closeBluetoothSocket(Object bluetoothSocket)
Definition: BluetoothReflection.java:314
com.google.appinventor.components.runtime.util.BluetoothReflection.connectToBluetoothSocket
static void connectToBluetoothSocket(Object bluetoothSocket)
Definition: BluetoothReflection.java:263
com.google.appinventor.components.runtime.util.BluetoothReflection.getOutputStream
static OutputStream getOutputStream(Object bluetoothSocket)
Definition: BluetoothReflection.java:301
com.google.appinventor.components.runtime.util.BluetoothReflection.listenUsingRfcommWithServiceRecord
static Object listenUsingRfcommWithServiceRecord(Object bluetoothAdapter, String name, UUID uuid)
Definition: BluetoothReflection.java:122
com.google.appinventor.components.runtime.util.BluetoothReflection.getBluetoothAdapter
static Object getBluetoothAdapter()
Definition: BluetoothReflection.java:44
com.google.appinventor.components.runtime.util.BluetoothReflection
Definition: BluetoothReflection.java:29
com.google.appinventor.components.runtime.util.BluetoothReflection.isBonded
static boolean isBonded(Object bluetoothDevice)
Definition: BluetoothReflection.java:184
com.google.appinventor.components.runtime.util.BluetoothReflection.createInsecureRfcommSocketToServiceRecord
static Object createInsecureRfcommSocketToServiceRecord(Object bluetoothDevice, UUID uuid)
Definition: BluetoothReflection.java:230
com.google.appinventor.components.runtime.util.BluetoothReflection.getRemoteDevice
static Object getRemoteDevice(Object bluetoothAdapter, String address)
Definition: BluetoothReflection.java:105
com.google.appinventor.components.runtime.util.BluetoothReflection.isBluetoothSocketConnected
static boolean isBluetoothSocketConnected(Object bluetoothSocket)
Definition: BluetoothReflection.java:275
com.google.appinventor.components.runtime.util.BluetoothReflection.listenUsingInsecureRfcommWithServiceRecord
static Object listenUsingInsecureRfcommWithServiceRecord(Object bluetoothAdapter, String name, UUID uuid)
Definition: BluetoothReflection.java:141
com.google.appinventor.components.runtime.util.BluetoothReflection.isBluetoothEnabled
static boolean isBluetoothEnabled(Object bluetoothAdapter)
Definition: BluetoothReflection.java:63
com.google.appinventor.components.runtime.util.BluetoothReflection.checkBluetoothAddress
static boolean checkBluetoothAddress(Object bluetoothAdapter, String address)
Definition: BluetoothReflection.java:90
com.google.appinventor.components.runtime.util.BluetoothReflection.closeBluetoothServerSocket
static void closeBluetoothServerSocket(Object bluetoothServerSocket)
Definition: BluetoothReflection.java:339
com.google.appinventor.components.runtime.util.BluetoothReflection.createRfcommSocketToServiceRecord
static Object createRfcommSocketToServiceRecord(Object bluetoothDevice, UUID uuid)
Definition: BluetoothReflection.java:212
com.google.appinventor.components.runtime.util.BluetoothReflection.getDeviceClass
static int getDeviceClass(Object bluetoothClass)
Definition: BluetoothReflection.java:249
com.google.appinventor.components.runtime.util.BluetoothReflection.getBondedDevices
static Set getBondedDevices(Object bluetoothAdapter)
Definition: BluetoothReflection.java:76
com.google.appinventor.components.runtime.util.BluetoothReflection.getBluetoothDeviceName
static String getBluetoothDeviceName(Object bluetoothDevice)
Definition: BluetoothReflection.java:158
com.google.appinventor.components.runtime.util.BluetoothReflection.getInputStream
static InputStream getInputStream(Object bluetoothSocket)
Definition: BluetoothReflection.java:287