Skip to content

Android library for connecting multiple devices over bluetooth

Notifications You must be signed in to change notification settings

pkarira/Androble

Repository files navigation

Androble

Connect nearby devices to your server !

Androble uses Android's Bluetooth API and establishes RFCOMM channels. The server listens for various clients using a unique UUID and the client having the same UUID is connected to the server. In multiple devices, server creates 4 RFCOMM channels with 4 different UUIDs and then each connection is maintained on a different thread by the server so that if anyhow connection with a particular client fails then the connection with other clients is uninterrupted.

Dependency

Add dependency to build.gradle of your app
dependencies
{
compile 'com.mdg.androble:library:0.1.2'
}

Reference

BluetoothManager class is the master class of this library and contains the following functions:

public void Type(String t)

Sets either you want to connect as server or client.

public void scanClients()

Starts scanning clients on server side.

public void connectTo(String s)

Lets client to connect to the desired server from list.

public void sendText(String s)

Allows to send message from client to connected server.

public void sendText(String s,int id)

Allows to send message from server to client specifying the id of client.

public String clientToClient(String s, int id)

Allows to send message from a client to another client specifying the id of recepient client.

public String getAllConnectedDevices()

To fetch list of all connected devices with respective ids.

public void setMessageObject(Object myObject)

Sets the observer object that fetches received messages.

public void setListObject(Object myObject)

Sets the observer object that fetches list of detected devices for client side.

public String disconnect()

To disable your connection.

Usage

Extend the activity in which you want to scan for devices by Discovery instead of Activity / AppCompatActivity like:

public class MainActivity extends Discovery{}

then create a class which implements Observer interface. This facilitates receiving of the list of devices scanned by Bluetooth.

class DeviceList implements Observer {
    @Override
    if(((com.mdg.androble.DeviceList)observable).getContent().equals("bluetooth enabled"))
           {
               bluetoothManager.scanClients();
           }else
           arrayAdapter.add(((com.mdg.androble.DeviceList)observable).getContent());
    }
}

then create another class, again implementing the Observer interface, for receiving text messages.

class receiveMessage implements Observer {
    @Override
    public void update(Observable observable, Object data) {
       String msg = ((receivemsg)observable).getMessage();
       Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
       //do whatever you want to do with received message
    }
}

Now get instance of Bluetooth manager class like :

BluetoothManager bluetoothManager= BluetoothManager.getInstance();

now create objects of DeviceList and receiceMessage class like:

receiveMessage  rm = new receiveMessage();
 DeviceList dl=new DeviceList();

pass these objects to setMessageObject and setListObject respectively , like:

    bluetoothManager.setMessageObject(receiveMessage);
    bluetoothManager.setListObject(deviceList);

Connecting as Server:-

Call Type funtion of BluetoothManager class and pass "SERVER" as parameter:
bluetoothManager.Type("SERVER");
To send text to any client where id is Id of client:-
bluetoothManager.sendText("your message",playerId)
To get the ID's of all connected devices call getAllConnectedDevices() described above.

Connecting as Client:-

``` bluetoothManager.Type("CLIENT"); ``` //now you will be able to connect to a single server device
``` bluetoothManager.clientToClient("your message",id) ``` where id is the id of connected devices
To get the ID's of all connected devices call ```getAllConnectedDevices()``` described above.

Sample code for MainActivity [Wiki](https://github.com/pkarira/Androble/wiki)

App Using This Library

License

Copyright 2016 Pulkit Karira

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Android library for connecting multiple devices over bluetooth

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages