7 package com.google.appinventor.components.runtime.util;
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;
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;
22 import java.util.UUID;
30 private static final int BOND_BONDED = 0xC;
46 Class bluetoothAdapterClass;
48 bluetoothAdapterClass = Class.forName(
"android.bluetooth.BluetoothAdapter");
49 }
catch (ClassNotFoundException e) {
53 return invokeStaticMethod(getMethod(bluetoothAdapterClass,
"getDefaultAdapter"));
65 return (Boolean) invokeMethod(getMethod(bluetoothAdapter.getClass(),
"isEnabled"),
78 return (Set) invokeMethod(getMethod(bluetoothAdapter.getClass(),
"getBondedDevices"),
92 return (Boolean) invokeMethod(
93 getMethod(bluetoothAdapter.getClass(),
"checkBluetoothAddress", String.class),
94 bluetoothAdapter, address);
106 throws IllegalArgumentException {
108 return invokeMethodThrowsIllegalArgumentException(
109 getMethod(bluetoothAdapter.getClass(),
"getRemoteDevice", String.class),
110 bluetoothAdapter, address);
123 UUID uuid)
throws IOException {
125 return invokeMethodThrowsIOException(getMethod(bluetoothAdapter.getClass(),
126 "listenUsingRfcommWithServiceRecord", String.class, UUID.class),
127 bluetoothAdapter, name, uuid);
142 Object bluetoothAdapter, String name, UUID uuid)
throws IOException {
144 return invokeMethodThrowsIOException(getMethod(bluetoothAdapter.getClass(),
145 "listenUsingInsecureRfcommWithServiceRecord", String.class, UUID.class),
146 bluetoothAdapter, name, uuid);
160 return (String) invokeMethod(getMethod(bluetoothDevice.getClass(),
"getName"),
173 return (String) invokeMethod(getMethod(bluetoothDevice.getClass(),
"getAddress"),
184 public static boolean isBonded(Object bluetoothDevice) {
186 int bondState = (Integer) invokeMethod(getMethod(bluetoothDevice.getClass(),
"getBondState"),
188 return bondState == BOND_BONDED;
200 return invokeMethod(getMethod(bluetoothDevice.getClass(),
"getBluetoothClass"),
215 return invokeMethodThrowsIOException(
216 getMethod(bluetoothDevice.getClass(),
"createRfcommSocketToServiceRecord", UUID.class),
217 bluetoothDevice, uuid);
233 return invokeMethodThrowsIOException(
234 getMethod(bluetoothDevice.getClass(),
235 "createInsecureRfcommSocketToServiceRecord", UUID.class),
236 bluetoothDevice, uuid);
251 return (Integer) invokeMethod(getMethod(bluetoothClass.getClass(),
"getDeviceClass"),
265 invokeMethodThrowsIOException(getMethod(bluetoothSocket.getClass(),
"connect"),
276 return (Boolean)invokeMethod(getMethod(bluetoothSocket.getClass(),
"isConnected"),
287 public static InputStream
getInputStream(Object bluetoothSocket)
throws IOException {
289 return (InputStream) invokeMethodThrowsIOException(
290 getMethod(bluetoothSocket.getClass(),
"getInputStream"),
301 public static OutputStream
getOutputStream(Object bluetoothSocket)
throws IOException {
303 return (OutputStream) invokeMethodThrowsIOException(
304 getMethod(bluetoothSocket.getClass(),
"getOutputStream"),
316 invokeMethodThrowsIOException(getMethod(bluetoothSocket.getClass(),
"close"),
328 public static Object
accept(Object bluetoothServerSocket)
throws IOException {
330 return invokeMethodThrowsIOException(getMethod(bluetoothServerSocket.getClass(),
"accept"),
331 bluetoothServerSocket);
341 invokeMethodThrowsIOException(getMethod(bluetoothServerSocket.getClass(),
"close"),
342 bluetoothServerSocket);
348 private static Method getMethod(Class clazz, String name) {
350 return clazz.getMethod(name,
new Class[0]);
351 }
catch (NoSuchMethodException e) {
352 throw new RuntimeException(e);
356 private static Method getMethod(Class clazz, String name, Class<?>... parameterTypes) {
358 return clazz.getMethod(name, parameterTypes);
359 }
catch (NoSuchMethodException e) {
360 throw new RuntimeException(e);
364 private static Object invokeStaticMethod(Method method) {
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;
375 throw new RuntimeException(cause);
380 private static Object invokeMethod(Method method, Object thisObject, Object... args) {
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;
391 throw new RuntimeException(cause);
396 private static Object invokeMethodThrowsIllegalArgumentException(Method method,
397 Object thisObject, Object... args)
throws IllegalArgumentException {
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;
410 throw new RuntimeException(e);
415 private static Object invokeMethodThrowsIOException(Method method, Object thisObject,
416 Object... args)
throws IOException {
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;
429 throw new RuntimeException(e);