AI2 Component  (Version nb184)
BluetoothServer.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;
8 
22 
23 import android.os.Handler;
24 import android.util.Log;
25 
26 import java.io.IOException;
27 import java.util.UUID;
28 import java.util.concurrent.atomic.AtomicReference;
29 
36 @DesignerComponent(version = YaVersion.BLUETOOTHSERVER_COMPONENT_VERSION,
37  description = "Bluetooth server component",
38  category = ComponentCategory.CONNECTIVITY,
39  nonVisible = true,
40  iconName = "images/bluetooth.png")
41 @SimpleObject
42 @UsesPermissions(permissionNames =
43  "android.permission.BLUETOOTH, " +
44  "android.permission.BLUETOOTH_ADMIN")
45 public final class BluetoothServer extends BluetoothConnectionBase {
46  private static final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
47 
48  private final Handler androidUIHandler;
49 
50  private final AtomicReference<Object> arBluetoothServerSocket;
51 
55  public BluetoothServer(ComponentContainer container) {
56  super(container, "BluetoothServer");
57  androidUIHandler = new Handler();
58  arBluetoothServerSocket = new AtomicReference<Object>();
59  }
60 
64  @SimpleFunction(description = "Accept an incoming connection with the Serial Port " +
65  "Profile (SPP).")
66  public void AcceptConnection(String serviceName) {
67  accept("AcceptConnection", serviceName, SPP_UUID);
68  }
69 
73  @SimpleFunction(description = "Accept an incoming connection with a specific UUID.")
74  public void AcceptConnectionWithUUID(String serviceName, String uuid) {
75  accept("AcceptConnectionWithUUID", serviceName, uuid);
76  }
77 
78  private void accept(final String functionName, String name, String uuidString) {
79  final Object bluetoothAdapter = BluetoothReflection.getBluetoothAdapter();
80  if (bluetoothAdapter == null) {
81  form.dispatchErrorOccurredEvent(this, functionName,
83  return;
84  }
85 
86  if (!BluetoothReflection.isBluetoothEnabled(bluetoothAdapter)) {
87  form.dispatchErrorOccurredEvent(this, functionName,
88  ErrorMessages.ERROR_BLUETOOTH_NOT_ENABLED);
89  return;
90  }
91 
92  UUID uuid;
93  try {
94  uuid = UUID.fromString(uuidString);
95  } catch (IllegalArgumentException e) {
96  form.dispatchErrorOccurredEvent(this, functionName,
97  ErrorMessages.ERROR_BLUETOOTH_INVALID_UUID, uuidString);
98  return;
99  }
100 
101  try {
102  Object bluetoothServerSocket;
103  if (!secure && SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD_MR1) {
104  // listenUsingInsecureRfcommWithServiceRecord was introduced in level 10
105  bluetoothServerSocket = BluetoothReflection.listenUsingInsecureRfcommWithServiceRecord(
106  bluetoothAdapter, name, uuid);
107  } else {
108  bluetoothServerSocket = BluetoothReflection.listenUsingRfcommWithServiceRecord(
109  bluetoothAdapter, name, uuid);
110  }
111  arBluetoothServerSocket.set(bluetoothServerSocket);
112  } catch (IOException e) {
113  form.dispatchErrorOccurredEvent(this, functionName,
114  ErrorMessages.ERROR_BLUETOOTH_UNABLE_TO_LISTEN);
115  return;
116  }
117 
118  AsynchUtil.runAsynchronously(new Runnable() {
119  public void run() {
120  Object acceptedBluetoothSocket = null;
121 
122  Object bluetoothServerSocket = arBluetoothServerSocket.get();
123  if (bluetoothServerSocket != null) {
124  try {
125  try {
126  acceptedBluetoothSocket = BluetoothReflection.accept(bluetoothServerSocket);
127  } catch (IOException e) {
128  androidUIHandler.post(new Runnable() {
129  public void run() {
130  form.dispatchErrorOccurredEvent(BluetoothServer.this, functionName,
131  ErrorMessages.ERROR_BLUETOOTH_UNABLE_TO_ACCEPT);
132  }
133  });
134  return;
135  }
136  } finally {
137  StopAccepting();
138  }
139  }
140 
141  if (acceptedBluetoothSocket != null) {
142  // Call setConnection and signal the event on the main thread.
143  final Object bluetoothSocket = acceptedBluetoothSocket;
144  androidUIHandler.post(new Runnable() {
145  public void run() {
146  try {
147  setConnection(bluetoothSocket);
148  } catch (IOException e) {
149  Disconnect();
150  form.dispatchErrorOccurredEvent(BluetoothServer.this, functionName,
151  ErrorMessages.ERROR_BLUETOOTH_UNABLE_TO_ACCEPT);
152  return;
153  }
154 
155  ConnectionAccepted();
156  }
157  });
158  }
159  }
160  });
161  }
162 
167  @SimpleProperty(category = PropertyCategory.BEHAVIOR)
168  public final boolean IsAccepting() {
169  return (arBluetoothServerSocket.get() != null);
170  }
171 
175  @SimpleFunction(description = "Stop accepting an incoming connection.")
176  public void StopAccepting() {
177  Object bluetoothServerSocket = arBluetoothServerSocket.getAndSet(null);
178  if (bluetoothServerSocket != null) {
179  try {
180  BluetoothReflection.closeBluetoothServerSocket(bluetoothServerSocket);
181  } catch (IOException e) {
182  Log.w(logTag, "Error while closing bluetooth server socket: " + e.getMessage());
183  }
184  }
185  }
186 
190  @SimpleEvent(description = "Indicates that a bluetooth connection has been accepted.")
191  public void ConnectionAccepted() {
192  Log.i(logTag, "Successfullly accepted bluetooth connection.");
193  EventDispatcher.dispatchEvent(this, "ConnectionAccepted");
194  }
195 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.util.BluetoothReflection.getBluetoothAdapter
static Object getBluetoothAdapter()
Definition: BluetoothReflection.java:44
com.google.appinventor.components
com.google.appinventor.components.runtime.util.BluetoothReflection
Definition: BluetoothReflection.java:29
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.runtime.BluetoothServer.BluetoothServer
BluetoothServer(ComponentContainer container)
Definition: BluetoothServer.java:55
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_BLUETOOTH_NOT_AVAILABLE
static final int ERROR_BLUETOOTH_NOT_AVAILABLE
Definition: ErrorMessages.java:70
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.BluetoothConnectionBase
Definition: BluetoothConnectionBase.java:41
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.BluetoothServer
Definition: BluetoothServer.java:45
com.google.appinventor.components.runtime.util.BluetoothReflection.closeBluetoothServerSocket
static void closeBluetoothServerSocket(Object bluetoothServerSocket)
Definition: BluetoothReflection.java:339
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.util.AsynchUtil
Definition: AsynchUtil.java:17
com.google
com
com.google.appinventor.components.annotations
com.google.appinventor