diff options
author | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-20 11:18:35 +0200 |
---|---|---|
committer | Andrzej J.R. Hunt <andrzej@ahunt.org> | 2012-08-20 11:21:13 +0200 |
commit | 95ebf4e3b3702d5820ba4bbea3ff5eae5e12fe62 (patch) | |
tree | 468c25e9ff675943bd7e63e18e40c1ac56ff11cb /android | |
parent | 64792f61a1c67d6cd447d3a0d44188339efdd467 (diff) |
Bluetooth discovery and attempts at communication.
Change-Id: I88e261b7d0f830f67166d4eaf592298015b1c5ae
Diffstat (limited to 'android')
5 files changed, 154 insertions, 39 deletions
diff --git a/android/sdremote/AndroidManifest.xml b/android/sdremote/AndroidManifest.xml index 796cf81a1af3..04255fda0049 100644 --- a/android/sdremote/AndroidManifest.xml +++ b/android/sdremote/AndroidManifest.xml @@ -5,6 +5,8 @@ android:versionName="0.1-Alpha" > <uses-permission android:name="android.permission.INTERNET" /> + <uses-permission android:name="android.permission.BLUETOOTH" /> + <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-sdk android:minSdkVersion="14" diff --git a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java index a239312bd98f..f6f9514de9af 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java +++ b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java @@ -67,7 +67,7 @@ public class SelectorActivity extends Activity { @Override public void onBackPressed() { - mCommunicationService.stopFindingServers(); + mCommunicationService.stopSearching(); Intent aIntent = new Intent(this, CommunicationService.class); stopService(aIntent); super.onBackPressed(); @@ -76,6 +76,10 @@ public class SelectorActivity extends Activity { @Override protected void onResume() { super.onResume(); + mNetworkList.removeAllViews(); + mBluetoothList.removeAllViews(); + mNetworkServers.clear(); + mBluetoothServers.clear(); doBindService(); } @@ -84,7 +88,7 @@ public class SelectorActivity extends Activity { // TODO Auto-generated method stub super.onPause(); if (mCommunicationService != null) { - mCommunicationService.stopFindingServers(); + mCommunicationService.stopSearching(); } doUnbindService(); } @@ -105,7 +109,7 @@ public class SelectorActivity extends Activity { IBinder aService) { mCommunicationService = ((CommunicationService.CBinder) aService) .getService(); - mCommunicationService.startFindingServers(); + mCommunicationService.startSearching(); } @Override @@ -137,15 +141,20 @@ public class SelectorActivity extends Activity { // Bluetooth -- Remove old for (Entry<Server, View> aEntry : mBluetoothServers.entrySet()) { if (!Arrays.asList(aServers).contains(aEntry.getKey())) { + System.out.println("Removing view " + + aEntry.getKey().getName()); mBluetoothServers.remove(aEntry.getKey()); - mBluetoothList.removeView(aEntry.getValue()); + mBluetoothList.removeView((View) aEntry.getValue() + .getParent()); } } // Network -- Remove old for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) { if (!Arrays.asList(aServers).contains(aEntry.getKey())) { - mNetworkServers.remove(aEntry.getKey()); - mNetworkList.removeView(aEntry.getValue()); + System.out.println("Removing view"); + mNetworkServers.remove(aEntry.getKey().getName()); + mNetworkList.removeView((View) aEntry.getValue() + .getParent()); } } // Add all new @@ -156,7 +165,7 @@ public class SelectorActivity extends Activity { LinearLayout aLayout = aIsBluetooth ? mBluetoothList : mNetworkList; - if (!aMap.containsValue(aServer)) { + if (!aMap.containsKey(aServer)) { View aView = getLayoutInflater() .inflate(R.layout.activity_selector_sublayout_server, null); @@ -166,7 +175,7 @@ public class SelectorActivity extends Activity { aText.setOnClickListener(mClickListener); aText.setText(aServer.getName()); aLayout.addView(aView); - aMap.put(aServer, aView); + aMap.put(aServer, aText); } } @@ -188,7 +197,7 @@ public class SelectorActivity extends Activity { @Override public void onClick(View aView) { - mCommunicationService.stopFindingServers(); + mCommunicationService.stopSearching(); Server aDesiredServer = null; diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java new file mode 100644 index 000000000000..bfe10a9fefb3 --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothFinder.java @@ -0,0 +1,115 @@ +package org.libreoffice.impressremote.communication; + +import java.util.Collection; +import java.util.HashMap; + +import org.libreoffice.impressremote.communication.Server.Protocol; + +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +public class BluetoothFinder { + + // TODO: add removal of cached items + private Context mContext; + + private static final String CHARSET = "UTF-8"; + + BluetoothAdapter mAdapter; + + public BluetoothFinder(Context aContext) { + mContext = aContext; + mAdapter = BluetoothAdapter.getDefaultAdapter(); + + } + + public void startFinding() { + System.out.println("BT:Discovery starting"); + IntentFilter aFilter = new IntentFilter( + BluetoothAdapter.ACTION_DISCOVERY_FINISHED); + aFilter.addAction(BluetoothDevice.ACTION_FOUND); + mContext.registerReceiver(mReceiver, aFilter); + + mAdapter.enable(); + mAdapter.startDiscovery(); + } + + public void stopFinding() { + mAdapter.cancelDiscovery(); + mContext.unregisterReceiver(mReceiver); + } + + private HashMap<String, Server> mServerList = new HashMap<String, Server>(); + + public Collection<Server> getServerList() { + return mServerList.values(); + } + + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent aIntent) { + // TODO Auto-generated method stub + System.out.println("Received intent"); + System.out.println(aIntent.getAction()); + if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) { + BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras() + .get(BluetoothDevice.EXTRA_DEVICE); + Server aServer = new Server(Protocol.BLUETOOTH, + aDevice.getAddress(), aDevice.getName(), + System.currentTimeMillis()); + mServerList.put(aServer.getAddress(), aServer); + System.out.println("Added " + aServer.getName()); + System.out.println("Now we have: " + mServerList.size()); + Intent aNIntent = new Intent( + CommunicationService.MSG_SERVERLIST_CHANGED); + mContext.sendBroadcast(aNIntent); + // System.out.println("Found " + aDevice.getName()); + // try { + // // "f36d0a20-e876-11e1-aff1-0800200c9a66" + // BluetoothSocket aSocket = aDevice + // .createRfcommSocketToServiceRecord(UUID + // .fromString("00001101-0000-1000-8000-00805F9B34FB")); + // aSocket.connect(); + // } catch (IOException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // System.out.println("Fallback"); + // Method m; + // try { + // m = aDevice.getClass().getMethod("createRfcommSocket", + // new Class[] { int.class }); + // BluetoothSocket aFSocket = (BluetoothSocket) m.invoke( + // aDevice, 1); + // + // mAdapter.cancelDiscovery(); + // aFSocket.connect(); + // } catch (NoSuchMethodException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } catch (IllegalArgumentException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } catch (IllegalAccessException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } catch (InvocationTargetException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } catch (IOException e1) { + // // TODO Auto-generated catch block + // e1.printStackTrace(); + // } + // System.out.println("Fallback complete"); + // + // } + } + + } + + }; +} diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 677028c5cdff..b25fcc04b49b 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -8,6 +8,8 @@ */ package org.libreoffice.impressremote.communication; +import java.util.ArrayList; + import android.app.Service; import android.content.Intent; import android.os.Binder; @@ -87,14 +89,17 @@ public class CommunicationService extends Service implements Runnable { if (mState == State.CONNECTING || mState == State.CONNECTED) { disconnect(); } - mFinder.startFinding(); + mNetworkFinder.startFinding(); + mBluetoothFinder.startFinding(); mState = State.SEARCHING; } + new BluetoothFinder(this); } public void stopSearching() { synchronized (mConnectionVariableMutex) { - mFinder.stopFinding(); + mNetworkFinder.stopFinding(); + mBluetoothFinder.stopFinding(); mState = State.DISCONNECTED; } } @@ -102,7 +107,8 @@ public class CommunicationService extends Service implements Runnable { public void connectTo(Server aServer) { synchronized (mConnectionVariableMutex) { if (mState == State.SEARCHING) { - mFinder.stopFinding(); + mNetworkFinder.stopFinding(); + mBluetoothFinder.stopFinding(); mState = State.DISCONNECTED; } mServerDesired = aServer; @@ -150,7 +156,8 @@ public class CommunicationService extends Service implements Runnable { private Receiver mReceiver = new Receiver(this); - private ServerFinder mFinder = new ServerFinder(this); + private ServerFinder mNetworkFinder = new ServerFinder(this); + private BluetoothFinder mBluetoothFinder = new BluetoothFinder(this); @Override public IBinder onBind(Intent intent) { @@ -179,15 +186,10 @@ public class CommunicationService extends Service implements Runnable { } public Server[] getServers() { - return mFinder.getServerList(); - } - - public void startFindingServers() { - mFinder.startFinding(); - } - - public void stopFindingServers() { - mFinder.stopFinding(); + ArrayList<Server> aServers = new ArrayList<Server>(); + aServers.addAll(mNetworkFinder.getServerList()); + aServers.addAll(mBluetoothFinder.getServerList()); + return aServers.toArray(new Server[aServers.size()]); } public SlideShow getSlideShow() { diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java index 7405a25ae5f3..67a886223f3d 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServerFinder.java @@ -6,11 +6,11 @@ import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; +import java.util.Collection; import java.util.HashMap; import android.content.Context; import android.content.Intent; -import android.support.v4.content.LocalBroadcastManager; public class ServerFinder { @@ -42,9 +42,7 @@ public class ServerFinder { try { String aCommand = null; String aName = null; - System.out.println("SF:Reading"); mSocket.receive(aPacket); - System.out.println("SF:Received"); int i; for (i = 0; i < aBuffer.length; i++) { if (aPacket.getData()[i] == '\n') { @@ -69,10 +67,6 @@ public class ServerFinder { System.currentTimeMillis()); mServerList.put(aServer.getAddress(), aServer); - //System.out.println("SF FOUND: IP=" - //+ aPacket.getAddress().toString() + " HOSTNAME=" - //+ aName); - Intent aIntent = new Intent( CommunicationService.MSG_SERVERLIST_CHANGED); mContext.sendBroadcast(aIntent); @@ -92,9 +86,6 @@ public class ServerFinder { mFinishRequested = false; - Intent aIntent = new Intent(CommunicationService.MSG_SERVERLIST_CHANGED); - LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent); - if (mListenerThread == null) { mListenerThread = new Thread() { @Override @@ -104,9 +95,7 @@ public class ServerFinder { mSocket = new DatagramSocket(); mSocket.setSoTimeout(1000 * 10); while (!mFinishRequested) { - System.out.println("SF:Looping"); if (System.currentTimeMillis() - aTime > SEARCH_INTERVAL) { - System.out.println("SF:Sending"); String aString = "LOREMOTE_SEARCH\n"; DatagramPacket aPacket = new DatagramPacket( aString.getBytes(CHARSET), @@ -121,9 +110,7 @@ public class ServerFinder { mServerList.remove(aServer.getAddress()); Intent aIntent = new Intent( CommunicationService.MSG_SERVERLIST_CHANGED); - LocalBroadcastManager.getInstance( - mContext) - .sendBroadcast(aIntent); + mContext.sendBroadcast(aIntent); } } @@ -156,7 +143,7 @@ public class ServerFinder { } } - public Server[] getServerList() { - return mServerList.values().toArray(new Server[mServerList.size()]); + public Collection<Server> getServerList() { + return mServerList.values(); } } |