1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.googlecode.android_scripting.facade.bluetooth; 16 17 import com.googlecode.android_scripting.facade.FacadeManager; 18 import com.googlecode.android_scripting.rpc.Rpc; 19 import com.googlecode.android_scripting.rpc.RpcDefault; 20 import com.googlecode.android_scripting.rpc.RpcOptional; 21 import com.googlecode.android_scripting.rpc.RpcParameter; 22 23 import java.io.IOException; 24 import java.util.Map; 25 26 /** 27 * Bluetooth functions. 28 * 29 */ 30 // Discovery functions added by Eden Sayag 31 32 public class BluetoothRfcommFacade extends BluetoothSocketConnFacade { 33 BluetoothRfcommFacade(FacadeManager manager)34 public BluetoothRfcommFacade(FacadeManager manager) { 35 super(manager); 36 } 37 38 /** 39 * Create L2CAP socket to Bluetooth device 40 * 41 * @param address the bluetooth address to open the socket on 42 * @param channel the channel to open the socket on 43 * @throws Exception 44 */ 45 @Rpc(description = "Create L2CAP socket to Bluetooth deice") rfcommCreateL2capSocket(@pcParametername = "address") String address, @RpcParameter(name = "channel") Integer channel)46 public void rfcommCreateL2capSocket(@RpcParameter(name = "address") String address, 47 @RpcParameter(name = "channel") Integer channel) throws Exception { 48 bluetoothSocketConnCreateL2capSocket(address, channel); 49 } 50 51 /** 52 * Create RFCOMM socket to Bluetooth device 53 * 54 * @param address the bluetooth address to open the socket on 55 * @param channel the channel to open the socket on 56 * @throws Exception 57 */ 58 @Rpc(description = "Create RFCOMM socket to Bluetooth deice") rfcommCreateRfcommSocket(@pcParametername = "address") String address, @RpcParameter(name = "channel") Integer channel)59 public void rfcommCreateRfcommSocket(@RpcParameter(name = "address") String address, 60 @RpcParameter(name = "channel") Integer channel) throws Exception { 61 bluetoothSocketConnCreateL2capSocket(address, channel); 62 } 63 64 /** 65 * Begin Connect Thread 66 * 67 * @param address the mac address of the device to connect to 68 * @param uuid the UUID that is used by the server device 69 * @throws Exception 70 */ 71 @Rpc(description = "Begins a thread initiate an Rfcomm connection over Bluetooth. ") bluetoothRfcommBeginConnectThread( @pcParametername = "address", description = "The mac address of the device to connect to.") String address, @RpcParameter(name = "uuid", description = "The UUID passed here must match the UUID used by the server device.") @RpcDefault(DEFAULT_UUID) String uuid)72 public void bluetoothRfcommBeginConnectThread( 73 @RpcParameter(name = "address", 74 description = "The mac address of the device to connect to.") String address, 75 @RpcParameter(name = "uuid", 76 description = "The UUID passed here must match the UUID used by the server device.") 77 @RpcDefault(DEFAULT_UUID) String uuid) 78 throws IOException { 79 bluetoothSocketConnBeginConnectThreadUuid(address, uuid); 80 } 81 82 /** 83 * Kill thread 84 */ 85 @Rpc(description = "Kill thread") bluetoothRfcommKillConnThread()86 public void bluetoothRfcommKillConnThread() { 87 bluetoothSocketConnKillConnThread(); 88 } 89 90 /** 91 * Closes an active Rfcomm Client socket 92 */ 93 @Rpc(description = "Close an active Rfcomm Client socket") bluetoothRfcommEndConnectThread()94 public void bluetoothRfcommEndConnectThread() throws IOException { 95 bluetoothSocketConnEndConnectThread(); 96 } 97 98 /** 99 * Closes an active Rfcomm Server socket 100 */ 101 @Rpc(description = "Close an active Rfcomm Server socket") bluetoothRfcommEndAcceptThread()102 public void bluetoothRfcommEndAcceptThread() throws IOException { 103 bluetoothSocketConnEndAcceptThread(); 104 } 105 106 /** 107 * Returns active Bluetooth mConnections 108 */ 109 @Rpc(description = "Returns active Bluetooth mConnections.") bluetoothRfcommActiveConnections()110 public Map<String, String> bluetoothRfcommActiveConnections() { 111 return bluetoothSocketConnActiveConnections(); 112 } 113 114 /** 115 * Returns the name of the connected device 116 */ 117 @Rpc(description = "Returns the name of the connected device.") bluetoothRfcommGetConnectedDeviceName( @pcParametername = "connID", description = "Connection id") @pcOptional @pcDefault"") String connID)118 public String bluetoothRfcommGetConnectedDeviceName( 119 @RpcParameter(name = "connID", description = "Connection id") @RpcOptional 120 @RpcDefault("") String connID) 121 throws IOException { 122 return bluetoothSocketConnGetConnectedDeviceName(connID); 123 } 124 125 /** 126 * Begins a thread to accept an Rfcomm connection over Bluetooth 127 */ 128 @Rpc(description = "Begins a thread to accept an Rfcomm connection over Bluetooth. ") bluetoothRfcommBeginAcceptThread( @pcParametername = "uuid") @pcDefaultDEFAULT_UUID) String uuid, @RpcParameter(name = "timeout", description = "How long to wait for a new connection, 0 is wait for ever") @RpcDefault("0") Integer timeout)129 public void bluetoothRfcommBeginAcceptThread( 130 @RpcParameter(name = "uuid") @RpcDefault(DEFAULT_UUID) String uuid, 131 @RpcParameter(name = "timeout", 132 description = "How long to wait for a new connection, 0 is wait for ever") 133 @RpcDefault("0") Integer timeout) 134 throws IOException { 135 bluetoothSocketConnBeginAcceptThreadUuid(uuid, timeout); 136 } 137 138 /** 139 * Sends ASCII characters over the currently open Bluetooth connection 140 */ 141 @Rpc(description = "Sends ASCII characters over the currently open Bluetooth connection.") bluetoothRfcommWrite(@pcParametername = "ascii") String ascii, @RpcParameter(name = "connID", description = "Connection id") @RpcDefault("") String connID)142 public void bluetoothRfcommWrite(@RpcParameter(name = "ascii") String ascii, 143 @RpcParameter(name = "connID", description = "Connection id") 144 @RpcDefault("") String connID) 145 throws IOException { 146 bluetoothSocketConnWrite(ascii, connID); 147 } 148 149 /** 150 * Read up to bufferSize ASCII characters 151 */ 152 @Rpc(description = "Read up to bufferSize ASCII characters.") bluetoothRfcommRead( @pcParametername = "bufferSize") @pcDefault"4096") Integer bufferSize, @RpcParameter(name = "connID", description = "Connection id") @RpcOptional @RpcDefault("") String connID)153 public String bluetoothRfcommRead( 154 @RpcParameter(name = "bufferSize") @RpcDefault("4096") Integer bufferSize, 155 @RpcParameter(name = "connID", description = "Connection id") @RpcOptional 156 @RpcDefault("") String connID) 157 throws IOException { 158 return bluetoothSocketConnRead(bufferSize, connID); 159 } 160 161 /** 162 * Send bytes over the currently open Bluetooth connection 163 */ 164 @Rpc(description = "Send bytes over the currently open Bluetooth connection.") bluetoothRfcommWriteBinary( @pcParametername = "base64", description = "A base64 encoded String of the bytes to be sent.") String base64, @RpcParameter(name = "connID", description = "Connection id") @RpcDefault("") @RpcOptional String connID)165 public void bluetoothRfcommWriteBinary( 166 @RpcParameter(name = "base64", 167 description = "A base64 encoded String of the bytes to be sent.") String base64, 168 @RpcParameter(name = "connID", 169 description = "Connection id") @RpcDefault("") @RpcOptional String connID) 170 throws IOException { 171 bluetoothSocketConnWriteBinary(base64, connID); 172 } 173 174 /** 175 * Read up to bufferSize bytes and return a chunked, base64 encoded string 176 */ 177 @Rpc(description = "Read up to bufferSize bytes and return a chunked, base64 encoded string.") bluetoothRfcommReadBinary( @pcParametername = "bufferSize") @pcDefault"4096") Integer bufferSize, @RpcParameter(name = "connID", description = "Connection id") @RpcDefault("") @RpcOptional String connID)178 public String bluetoothRfcommReadBinary( 179 @RpcParameter(name = "bufferSize") @RpcDefault("4096") Integer bufferSize, 180 @RpcParameter(name = "connID", description = "Connection id") @RpcDefault("") 181 @RpcOptional String connID) 182 throws IOException { 183 return bluetoothSocketConnReadBinary(bufferSize, connID); 184 } 185 186 /** 187 * Returns True if the next read is guaranteed not to block 188 */ 189 @Rpc(description = "Returns True if the next read is guaranteed not to block.") bluetoothRfcommReadReady( @pcParametername = "connID", description = "Connection id") @pcDefault"") @pcOptional String connID)190 public Boolean bluetoothRfcommReadReady( 191 @RpcParameter(name = "connID", description = "Connection id") @RpcDefault("") 192 @RpcOptional String connID) 193 throws IOException { 194 return bluetoothSocketConnReadReady(connID); 195 } 196 197 /** 198 * Read the next line 199 */ 200 @Rpc(description = "Read the next line.") bluetoothRfcommReadLine( @pcParametername = "connID", description = "Connection id") @pcOptional @pcDefault"") String connID)201 public String bluetoothRfcommReadLine( 202 @RpcParameter(name = "connID", description = "Connection id") @RpcOptional 203 @RpcDefault("") String connID) 204 throws IOException { 205 return bluetoothSocketConnReadLine(connID); 206 } 207 208 /** 209 * Stops Bluetooth connection 210 */ 211 @Rpc(description = "Stops Bluetooth connection.") bluetoothRfcommStop( @pcParametername = "connID", description = "Connection id") @pcOptional @pcDefault"") String connID)212 public void bluetoothRfcommStop( 213 @RpcParameter(name = "connID", description = "Connection id") @RpcOptional 214 @RpcDefault("") String connID) { 215 bluetoothSocketConnStop(connID); 216 } 217 218 @Override shutdown()219 public void shutdown() { 220 super.shutdown(); 221 } 222 223 } 224 225