Skip to content
/ BlueCon Public
forked from fregmented/BlueCon

BlueCon - Android BT SPP Server & Client Library

License

Notifications You must be signed in to change notification settings

tsdogs/BlueCon

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BlueCon

Download

BlueCon is SPP Server & Client Library for Android.

this Service is support Android 4.4 and grater environment.

Installation

if you use jcenter repository, just add dependency your app.

dependencies {
    ...
    compile 'me.kudryavka:bluecon:0.2.1'
    ...
}

Usage

just simple.

Add permission in your manifest file for using BLUETOOTH like this.

<uses-permission android:name="android.permission.BLUETOOTH" />

Client

as a Service

for using client service, Using SPPClientService. Define Binder and Service connect condition.

private SPPClientService.SPPBinder svBinder;
private boolean isSVConned;

Bind service to Application's context.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    bindService(new Intent(this, SPPClientService.class), serviceConnection, BIND_AUTO_CREATE);
    ...
}

Unbind service when app is dead

@Override
protected void onDestroy() {
    ... 
    unbindService(serviceConnection);
    ...
}

Make ServiceConnection and add SPPLietner to the Service.

public ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.e("SERVICE", "CONNNECTED");
        svBinder = (SPPClientService.SPPBinder)service;
        isSVConned = true;
        svBinder.addSPPListener(new SPPListener() {
            @Override
            public void onPacketReceived(String address, byte[] packet) {
                // Received packets
            }

            @Override
            public void onPacketSended(String address, byte[] packet) {
                // Sended packets
            }

            @Override
            public void onBluetoothDeviceConnected(String name, String address) {
                // on bluetooth device connected
            }

            @Override
            public void onBluetoothDeviceConnecting(@Nullable String name) {
                // on connecting to device
            }

            @Override
            public void onBluetoothDeviceDisconnected(@Nullable String name) {
                // on disconnected from device
            }

            @Override
            public void onBluetoothDisabled() {
                // if bluetooth is off
            }

            @Override
            public void onBluetoothNotSupported() {
                // Android device has not bluetooth module
            }
        });
    }
    
    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.e("SERVICE", "DISCONNNECTED");
        isSVConned = false;
    }
};

You can get Paird device list.

ArrayList<BluetoothDevice> devices = svBinder.getPairedDevices();

You can connect to device using BluetoothDevice or Device's MAC address

svBinder.connect(BluetoothDevice device);
svBinder.connect(String address);

Sending a packet

svBinder.sendPacket(byte[] data)

As a instance

Get Controller's Instance

SPPClientController sppClientController = SPPClientController.getInstance(getApplicationContext());

Add SPPListener to Instance

sppClientController.addSPPListener(new SPPListener() {
    @Override
    public void onPacketReceived(String address, byte[] packet) {
        // Received packets
    }

    @Override
    public void onPacketSended(String address, byte[] packet) {
        // Sended packets
    }

    @Override
    public void onBluetoothDeviceConnected(String name, String address) {
        // on bluetooth device connected
    }

    @Override
    public void onBluetoothDeviceConnecting(@Nullable String name) {
        // on connecting to device
    }

    @Override
    public void onBluetoothDeviceDisconnected(@Nullable String name) {
        // on disconnected from device
    }

    @Override
    public void onBluetoothDisabled() {
        // if bluetooth is off
    }

    @Override
    public void onBluetoothNotSupported() {
        // Android device has not bluetooth module
    }
});

You can get Paird device list.

ArrayList<BluetoothDevice> devices = sppClientController.getPairedDevices();

You can connect to device using BluetoothDevice or Device's MAC address

sppClientController.connect(BluetoothDevice device);
sppClientController.connect(String address);

Sending a packet

sppClientController.sendPacket(byte[] data)

Server

as a Service

for using server service, Using SPPServerService. Define Binder and Service connect condition.

private SPPServerService.SPPBinder svBinder;
private boolean isSVConned;

Bind service to Application's context.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    bindService(new Intent(this, SPPServerService.class), serviceConnection, BIND_AUTO_CREATE);
    ...
}

Unbind service when app is dead

@Override
protected void onDestroy() {
    ... 
    unbindService(serviceConnection);
    ...
}

Make ServiceConnection and add SPPLietner to the Service.

public ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.e("SERVICE", "CONNNECTED");
        svBinder = (SPPServerController.SPPBinder)service;
        isSVConned = true;
        svBinder.addSPPListener(new SPPListener() {
            @Override
            public void onPacketReceived(String address, byte[] packet) {
                // Received packets
            }

            @Override
            public void onPacketSended(String address, byte[] packet) {
                // Sended packets
            }

            @Override
            public void onBluetoothDeviceConnected(String name, String address) {
                // on bluetooth device connected
            }

            @Override
            public void onBluetoothDeviceConnecting(@Nullable String name) {
                // on connecting to device
            }

            @Override
            public void onBluetoothDeviceDisconnected(@Nullable String name) {
                // on disconnected from device
            }

            @Override
            public void onBluetoothDisabled() {
                // if bluetooth is off
            }

            @Override
            public void onBluetoothNotSupported() {
                // Android device has not bluetooth module
            }
        });
    }
    
    @Override
    public void onServiceDisconnected(ComponentName name) {
        Log.e("SERVICE", "DISCONNNECTED");
        isSVConned = false;
    }
};

You can get Connecte device list.

ArrayList<BluetoothDevice> devices = svBinder.getConnectedDevice()

You can start server with server's name, inputStream buffersize, server mode.

SERVER_MODE{SINGLE_CONNECTION, MULTIPLE_CONNECTION}
svBinder.startServer(String serverName, int bufferSize, ENUMS.SERVER_MODE serverMode);

Sending a packet

svBinder.sendPacket(String addressbyte[] data);

or broadcast

svBinder.broadcastPacket(byte[] data);

As a instance

Get Controller's Instance

SPPServerController sppServerController = SPPServerController.getInstance(getApplicationContext());

Add SPPListener to Instance

sppServerController.addSPPListener(new SPPListener() {
    @Override
    public void onPacketReceived(String address, byte[] packet) {
        // Received packets
    }

    @Override
    public void onPacketSended(String address, byte[] packet) {
        // Sended packets
    }

    @Override
    public void onBluetoothDeviceConnected(String name, String address) {
        // on bluetooth device connected
    }

    @Override
    public void onBluetoothDeviceConnecting(@Nullable String name) {
        // on connecting to device
    }

    @Override
    public void onBluetoothDeviceDisconnected(@Nullable String name) {
        // on disconnected from device
    }

    @Override
    public void onBluetoothDisabled() {
        // if bluetooth is off
    }

    @Override
    public void onBluetoothNotSupported() {
        // Android device has not bluetooth module
    }
});

You can get Connecte device list.

ArrayList<BluetoothDevice> devices = sppServerController.getConnectedDevice()

You can start server with server's name, inputStream buffersize, server mode.

SERVER_MODE{SINGLE_CONNECTION, MULTIPLE_CONNECTION}
sppServerController.startServer(String serverName, int bufferSize, ENUMS.SERVER_MODE serverMode);

Sending a packet

sppServerController.sendPacket(String addressbyte[] data);

or broadcast

sppServerController.broadcastPacket(byte[] data);

About

BlueCon - Android BT SPP Server & Client Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%