All Projects → OmarAflak → Bluetooth Library

OmarAflak / Bluetooth Library

Bluetooth client library for Android.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Bluetooth Library

Potato Library
Easy to use Utility library for Android
Stars: ✭ 45 (-73.84%)
Mutual labels:  library, bluetooth
Printooth
A well documented, high-level Android interface that makes printing via bluetooth printers easier
Stars: ✭ 231 (+34.3%)
Mutual labels:  library, bluetooth
Easydeviceinfo
📱 [Android Library] Get device information in a super easy way.
Stars: ✭ 1,698 (+887.21%)
Mutual labels:  library, bluetooth
Ble Indoor Positioning
Multilateration using bluetooth beacons
Stars: ✭ 274 (+59.3%)
Mutual labels:  library, bluetooth
Gsm v5
gsm module library for STM32 LL
Stars: ✭ 135 (-21.51%)
Mutual labels:  library, bluetooth
Confetti.js
A simple confetti animation for your website :)
Stars: ✭ 170 (-1.16%)
Mutual labels:  library
Resilient.js
Fault tolerant and reactive HTTP client for node.js and browsers
Stars: ✭ 172 (+0%)
Mutual labels:  discovery
Expanding Collection Android
ExpandingCollection is a material design card peek/pop controller. Android UI Library made by @Ramotion
Stars: ✭ 2,032 (+1081.4%)
Mutual labels:  library
Switch Remoteplay
NOT AN OFFICIAL NINTENDO PRODUCT - Control your Switch remotely (no hacking required)
Stars: ✭ 170 (-1.16%)
Mutual labels:  bluetooth
Fe
前端热门文章阅读
Stars: ✭ 174 (+1.16%)
Mutual labels:  library
Stm32f4 uvc camera
STM32F4-Discovery USB Device UVC Camera examples
Stars: ✭ 173 (+0.58%)
Mutual labels:  discovery
Adaptive Tab Bar
AdaptiveController is a 'Progressive Reduction' Swift UI module for adding custom states to Native or Custom iOS UI elements. Swift UI component by @Ramotion
Stars: ✭ 2,032 (+1081.4%)
Mutual labels:  library
H Ble
Android Ble类库,基于回调,暴露搜索、连接、发送、接收、断开连接等接口,无需关心细节操作即可进行Ble通信。
Stars: ✭ 171 (-0.58%)
Mutual labels:  bluetooth
Riscv Fs
F# RISC-V Instruction Set formal specification
Stars: ✭ 173 (+0.58%)
Mutual labels:  library
Johnny Five
JavaScript Robotics and IoT programming framework, developed at Bocoup.
Stars: ✭ 12,498 (+7166.28%)
Mutual labels:  bluetooth
Tracy
C++ frame profiler
Stars: ✭ 3,115 (+1711.05%)
Mutual labels:  library
Dragview
Android library used to create an awesome Android UI based on a draggable element similar to the last YouTube New graphic component.
Stars: ✭ 171 (-0.58%)
Mutual labels:  library
Authorizer
Authorizer is a Password Manager for Android. It emulates an HID keyboard over USB and enters your credentials on your target device. Additionally it supports OTP 🔑📴
Stars: ✭ 172 (+0%)
Mutual labels:  bluetooth
Crawler Commons
A set of reusable Java components that implement functionality common to any web crawler
Stars: ✭ 173 (+0.58%)
Mutual labels:  library
Json Api
Implementation of JSON API in PHP 7
Stars: ✭ 171 (-0.58%)
Mutual labels:  library

Android Bluetooth Client Library Download

This is an Android library that simplifies the process of bluetooth communication, client side.

This library does not support BLE devices;

Install

Add to your gradle dependencies:

implementation 'me.aflak.libraries:bluetooth:1.3.9'

Enable bluetooth

Careful: You also have to enable phone location in newer versions of Android.

Asking user for bluetooth activation

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    // ...
    // Need to ask for bluetooth permissions before calling constructor !
    // Permissions are {BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_COARSE_LOCATION}
    bluetooth = new Bluetooth(this);
    bluetooth.setBluetoothCallback(bluetoothCallback);
}

@Override
protected void onStart() {
    super.onStart();
    bluetooth.onStart();
    if(bluetooth.isEnabled()){
        // doStuffWhenBluetoothOn() ...
    } else {
        bluetooth.showEnableDialog(ScanActivity.this);
    }
}

@Override
protected void onStop() {
    super.onStop();
    bluetooth.onStop();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    bluetooth.onActivityResult(requestCode, resultCode);
}

private BluetoothCallback bluetoothCallback = new BluetoothCallback() {
    @Override public void onBluetoothTurningOn() {}
    @Override public void onBluetoothTurningOff() {}
    @Override public void onBluetoothOff() {}

    @Override
    public void onBluetoothOn() {
        // doStuffWhenBluetoothOn() ...
    }

    @Override
    public void onUserDeniedActivation() {
        // handle activation denial...
    }
};

Without asking user for bluetooth activation

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    // ...
    // Need to ask for bluetooth permissions before calling constructor !
    // Permissions are {BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_COARSE_LOCATION}
    bluetooth = new Bluetooth(this);
    bluetooth.setBluetoothCallback(bluetoothCallback);
}

@Override
protected void onStart() {
    super.onStart();
    bluetooth.onStart();
    if(bluetooth.isEnabled()){
        // doStuffWhenBluetoothOn() ...
    } else {
        bluetooth.enable()
    }
}

@Override
protected void onStop() {
    super.onStop();
    bluetooth.onStop();
}

private BluetoothCallback bluetoothCallback = new BluetoothCallback() {
    @Override public void onBluetoothTurningOn() {}
    @Override public void onBluetoothTurningOff() {}
    @Override public void onBluetoothOff() {}
    @Override public void onUserDeniedActivation() {}
    
    @Override
    public void onBluetoothOn() {
        // doStuffWhenBluetoothOn() ...
    }
};

Discover devices and pair

Listener

bluetooth.setDiscoveryCallback(new DiscoveryCallback() {
    @Override public void onDiscoveryStarted() {}
    @Override public void onDiscoveryFinished() {}
    @Override public void onDeviceFound(BluetoothDevice device) {}
    @Override public void onDevicePaired(BluetoothDevice device) {}
    @Override public void onDeviceUnpaired(BluetoothDevice device) {}
    @Override public void onError(String message) {}
});

Scan and Pair

bluetooth.startScanning();
bluetooth.pair(device);
bluetooth.pair(device, "optional pin");

Get paired devices

List<BluetoothDevice> devices = bluetooth.getPairedDevices();

Connect to device and communicate

Listener

bluetooth.setDeviceCallback(new DeviceCallback() {
    @Override public void onDeviceConnected(BluetoothDevice device) {}
    @Override public void onDeviceDisconnected(BluetoothDevice device, String message) {}
    @Override public void onMessage(byte[] message) {}
    @Override public void onError(String message) {}
    @Override public void onConnectError(BluetoothDevice device, String message) {}
});

Connect to device

// three options
bluetooth.connectToName("name");
bluetooth.connectToAddress("address");
bluetooth.connectToDevice(device);

Connect to device using port trick

See this post for details: https://stackoverflow.com/a/25647197/5552022

bluetooth.connectToNameWithPortTrick("name");
bluetooth.connectToAddressWithPortTrick("address");
bluetooth.connectToDeviceWithPortTrick(device);

Should be avoided

Send a message

bluetooth.send("hello, world");
bluetooth.send(new byte[]{61, 62, 63});

Receive messages

The default behavior of the library is the read from the input stream until it hits a new line, it will then propagate the message through listeners as a byte array. You can change the way the library reads from the socket by creating your own reader class. It must extend from SocketReader and you should override the byte[] read() throws IOException method. This method must block. It should not return if no values were received.

The default behavior is actually one example of implementation :

public class LineReader extends SocketReader{
    private BufferedReader reader;

    public LineReader(InputStream inputStream) {
        super(inputStream);
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    @Override
    public byte[] read() throws IOException {
        return reader.readLine().getBytes();
    }
}

This is an implementation for a custom delimiter :

public class DelimiterReader extends SocketReader {
    private PushbackInputStream reader;
    private byte delimiter;

    public DelimiterReader(InputStream inputStream) {
        super(inputStream);
        reader = new PushbackInputStream(inputStream);
        delimiter = 0;
    }

    @Override
    public byte[] read() throws IOException {
        List<Byte> byteList = new ArrayList<>();
        byte[] tmp = new byte[1];

        while(true) {
            int n = reader.read();
            reader.unread(n);

            int count = reader.read(tmp);
            if(count > 0) {
                if(tmp[0] == delimiter){
                    byte[] returnBytes = new byte[byteList.size()];
                    for(int i=0 ; i<byteList.size() ; i++){
                        returnBytes[i] = byteList.get(i);
                    }
                    return returnBytes;
                } else {
                    byteList.add(tmp[0]);
                }
            }
        }
    }
}

Then you can use your reader :

bluetooth.setReader(LineReader.class);

JavaDoc

JavaDoc is in doc folder.

Example Code

app module

Download Demo App

Get it on Google Play

License

MIT License

Copyright (c) 2017 Michel Omar Aflak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].