I've created an Android Bluetooth server, specifically doing the following in my server app's onResume:
Using java Syntax Highlighting
- @Override
- protected void onPause(){
- if(this.btSocketServer != null) {
- this.btSocketServer.interrupt();
- }
- super.onPause();
- }
- @Override
- protected void onResume(){
- super.onResume();
- // make sure the bluetooth adapter singleton gets associated with the right thread...
- BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
- if( ! bta.isEnabled() ){
- // Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- // startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- initServer();
- }
- private void initServer() {
- // kill off any socket server that we happen to have laying around...
- if(this.btSocketServer != null){
- btSocketServer.interrupt();
- }
- this.btSocketServer = null;
- try {
- btSocketServer = new BluetoothSocketServer<BluetoothSocketConnectionClientConnector>(BLUETOOTH_UUID, new MouseGameBluetoothClientConnectorListener()) {
- @Override
- protected BluetoothSocketConnectionClientConnector newClientConnector(
- BluetoothSocketConnection pBluetoothSocketConnection)
- throws IOException {
- BluetoothSocketConnectionClientConnector lRet = null;
- try {
- lRet = new BluetoothSocketConnectionClientConnector(pBluetoothSocketConnection);
- // register ourselves to handle messages from clients
- lRet.registerClientMessage(MouseGameFlags.FLAG_MESSAGE_SERVER_MOVE_CRITTER, MoveCritterClientMessage.class);
- lRet.registerClientMessageHandler(MouseGameFlags.FLAG_MESSAGE_SERVER_MOVE_CRITTER, MouseGameActivity.this);
- lRet.registerClientMessage(MouseGameFlags.FLAG_MESSAGE_REGISTER_CONTROLLER, RegisterControllerClientMessage.class);
- lRet.registerClientMessageHandler(MouseGameFlags.FLAG_MESSAGE_REGISTER_CONTROLLER, MouseGameActivity.this);
- } catch(BluetoothException e){
- Debug.e("Error creating bluetooth controller socket", e);
- }
- return lRet;
- }
- };
- } catch(BluetoothException e){
- Debug.e("Error creating bluetooth controller socket", e);
- }
- if(this.btSocketServer != null) {
- this.btSocketServer.start();
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
On the client side I connect in the onResume method, and disconnect in the onPause method like this:
Using java Syntax Highlighting
- @Override
- protected void onPause() {
- if(this.serverConnector != null) {
- this.serverConnector.interrupt();
- }
- super.onPause();
- }
- @Override
- protected void onResume(){
- // connect to the server and start a new connector thread
- initClient();
- updateServerStatusIndicator();
- super.onResume();
- }
- private boolean initClient(){
- if(serverConnector != null){
- serverConnector.interrupt();
- serverConnector = null;
- }
- try {
- BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
- Set<BluetoothDevice> bondedDevices = bta.getBondedDevices();
- BluetoothSocket lSocket = null;
- Iterator<BluetoothDevice> bondedDevIter = bondedDevices.iterator();
- while(bondedDevIter.hasNext()){
- BluetoothDevice lDev = bondedDevIter.next();
- lSocket = lDev.createRfcommSocketToServiceRecord(UUID.fromString(BLUETOOTH_UUID));
- try {
- if(lSocket != null) {
- // try to connect to the remote bt device...
- bta.cancelDiscovery();
- lSocket.connect();
- this.serverConnector = new BluetoothSocketConnectionServerConnector(new BluetoothSocketConnection(lSocket), new GameServerConnectorListener());
- this.serverConnector.start();
- // register ourselves as controlling a particular mouse!
- this.serverConnector.sendClientMessage(new RegisterControllerClientMessage(boundCritterId));
- break;
- }
- } catch(Throwable t){
- Debug.d(" ---> skipping bt socket: "+lSocket.getRemoteDevice().getName());
- }
- }
- updateServerStatusIndicator();
- } catch (final Throwable t) {
- Debug.e(t);
- }
- if(this.serverConnector != null) {
- return true;
- }
- return false;
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
New connections fire onConnected on the server side just fine, but when I disconnect on the client side, the server's onDisconnected never fires. Anyone have any ideas?
