15 package com.neuronrobotics.sdk.wireless.bluetooth;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.util.ArrayList;
22 import javax.bluetooth.BluetoothStateException;
23 import javax.bluetooth.DeviceClass;
24 import javax.bluetooth.DiscoveryAgent;
25 import javax.bluetooth.DiscoveryListener;
26 import javax.bluetooth.LocalDevice;
27 import javax.bluetooth.RemoteDevice;
28 import javax.bluetooth.ServiceRecord;
29 import javax.bluetooth.UUID;
30 import javax.microedition.io.Connector;
31 import javax.microedition.io.StreamConnection;
33 import com.neuronrobotics.sdk.common.Log;
34 import com.neuronrobotics.sdk.common.MissingNativeLibraryException;
48 ArrayList<ServiceRecord> records=
new ArrayList<ServiceRecord>();
51 ArrayList<RemoteDevice> deviceList =
new ArrayList<RemoteDevice>();
54 static final UUID uuid = com.intel.bluetooth.BluetoothConsts.RFCOMM_PROTOCOL_UUID;
60 private DataInputStream
ins;
63 private DataOutputStream
outs;
66 private StreamConnection
conn;
83 for (RemoteDevice d:deviceList) {
84 if(d.getBluetoothAddress().equals(
selected)){
85 try {Thread.sleep(100);}
catch (InterruptedException e) {}
92 LocalDevice.getLocalDevice().getDiscoveryAgent().cancelInquiry(
this);
93 LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC,
this);
94 try {wait();}
catch (InterruptedException e) {e.printStackTrace();}
96 }
catch (BluetoothStateException e) {
99 for (RemoteDevice d:deviceList) {
101 Log.
info(
"Device name: " + d.getFriendlyName(
false)+
" address: " + d.getBluetoothAddress());
102 }
catch (IOException e) {
106 if(deviceList.size()==0)
115 deviceList.add(btDevice);
131 for (
int i=0; i< servRecord.length;i++)
132 records.add(servRecord[i]);
152 String [] s=
new String[deviceList.size()];
155 for (RemoteDevice d: deviceList){
157 tmp=d.getFriendlyName(
false);
160 s[i]=tmp+
"_"+d.getBluetoothAddress();
161 }
catch (Exception e) {
162 s[i]=
"Failed Name"+
"_"+d.getBluetoothAddress();
175 public synchronized RemoteDevice
getDevice(String name){
176 String addr = name.substring(name.indexOf(
'_')+1);
179 for (
int i=0;i<s.length;i++){
180 if(s[i].contains(addr)){
182 RemoteDevice dev = deviceList.get(i);
185 }
catch(Exception e) {
208 LocalDevice.getLocalDevice().getDiscoveryAgent().cancelInquiry(
this);
210 LocalDevice.getLocalDevice().getDiscoveryAgent().cancelServiceSearch(
searchId);
211 }
catch (Exception e) {
231 public synchronized void connect(String devAddress){
233 if(deviceList.contains(d))
237 if(!deviceList.contains(d))
238 throw new RuntimeException(
"Divice no longer availiable");
256 }
catch (Exception e){
257 throw new RuntimeException(e);
260 synchronized (
this) {
263 int[] attrSet =
null;
264 UUID[]
id =
new UUID[] { uuid };
266 LocalDevice.getLocalDevice().getDiscoveryAgent().cancelServiceSearch(
searchId);
268 try {wait();}
catch (InterruptedException e) {e.printStackTrace();}
270 }
catch (BluetoothStateException e) {
275 if (records.size()==0){
276 System.err.println(
"No compatible records");
281 String url = records.get(0).getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT,
false);
283 StreamConnection c =(StreamConnection) Connector.open(url);
285 }
catch (IOException ex){
286 System.err.println(
"Failed to connect to "+url);
287 if(!ex.getMessage().contains(
"Device or resource busy"))
291 ins =
conn.openDataInputStream();
296 }
catch (Exception e) {
298 System.err.println(
"No connection");
310 throw new RuntimeException();
321 throw new RuntimeException();
static void info(String message)
synchronized boolean connect()
void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
synchronized RemoteDevice getDevice(String name)
String[] getAvailableSerialDevices(boolean refresh)
synchronized DataInputStream getDataIns()
synchronized void serviceSearchCompleted(int transID, int respCode)
synchronized DataOutputStream getDataOuts()
synchronized void inquiryCompleted(int discType)
synchronized void disconnect()
synchronized void connect(String devAddress)
void servicesDiscovered(int transID, ServiceRecord[] servRecord)