GithubHelp home page GithubHelp logo

thanosfisherman / blueflow Goto Github PK

View Code? Open in Web Editor NEW
70.0 3.0 11.0 376 KB

Android Bluetooth classic API wrapped in Coroutines Flow.

License: Apache License 2.0

Kotlin 100.00%
android-library kotlin-android kotlin coroutines kotlin-coroutines coroutines-android

blueflow's Introduction

BlueFlow

Android Bluetooth classic API wrapped in Coroutines / Flow inspired by RxBluetooth and AndroidBluetoothLibrary.

Dependency

Download

Add the following to your app module build.gradle file. Note that the respective Coroutine dependencies should also be included in the project.

dependencies {
   implementation "io.github.thanosfisherman.blueflow:blueflow:<latest-version-number-here>"
}

Permissions

The following permissions must be granted for this lib to be able to work correctly.

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- Dangerous Permissions below. Make sure you explicitly ask for location Permissions in your app on Android 6.0+ -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Usage

Under Construction! For now check the sample project which demonstrates the bluetooth device discovery.

Creating an instance

Create a Singleton instance of BlueFlow like so

val blueFlow: BlueFlow = BlueFlow.getInstance(applicationContext)

Checking for Bluetooth availability

You can check whether bluetooth is available on a specific device and/or if it's enabled or not.

      if (blueFlow.isBluetoothAvailable()) { //Checks if bluetooth is supported on this device
          if (blueFlow.isBluetoothEnabled()) { //Checks if bluetooth is actually turned ON for this device
              Toast.makeText(applicationContext, "BLUETOOTH IS ON", Toast.LENGTH_LONG).show()
          } else {
              Toast.makeText(applicationContext, "PLEASE ENABLE BLUETOOTH", Toast.LENGTH_LONG).show()
          }
      } else { //Bluetooth is NOT Supported on the device
              Toast.makeText(applicationContext, "BLUETOOTH NOT AVAILABLE ON THIS DEVICE", Toast.LENGTH_LONG).show()
      }

License

License

Copyright 2020 Thanos Psaridis

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.

blueflow's People

Contributors

thanosfisherman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

blueflow's Issues

Read Flow consumed without a consumer

Hello dev, this is not really an issue and maybe I didn’t understand correctly the nature of the channelFlow used in the readByteArrayStream function. In few words I created a Service to share the socket between activities but switching from an activity to another I loose some message. I tried to write on stackoverflow but it seems that no one knows your lib. Take a look here to check the code I wrote: https://stackoverflow.com/questions/66965201/i-loose-data-from-bluetooth-moving-between-activities-and-using-services?r=SearchResults

Anyway a read example would be appreciated.
Thank you

bt socket closed, read return: -1, but then emission attempt has been detected

If I turn off the connected device I receive this error and I don't receive the "Disconnected" message.

Complete error from your sample:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.thanosfisherman.blueflow.sample, PID: 12390
java.lang.IllegalStateException: Flow exception transparency is violated:
Previous 'emit' call has thrown exception java.io.IOException: bt socket closed, read return: -1, but then emission attempt of value 'io.github.thanosfisherman.blueflow.sample.BtConnection$BtErrorConnectingState@cb152e5' has been detected.
Emissions from 'catch' blocks are prohibited in order to avoid unspecified behaviour, 'Flow.catch' operator can be used instead.
For a more detailed explanation, please refer to Flow documentation.
at kotlinx.coroutines.flow.internal.SafeCollector.exceptionTransparencyViolated(SafeCollector.kt:114)
at kotlinx.coroutines.flow.internal.SafeCollector.checkContext(SafeCollector.kt:82)
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:70)
at kotlinx.coroutines.flow.internal.SafeCollector.emit(SafeCollector.kt:55)
at io.github.thanosfisherman.blueflow.sample.usecase.BtConnectUseCase$connect$1.invokeSuspend(BtConnectUseCase.kt:19)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7399)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)

It would be nice if you can also provide a sample to receive/send data using the mvvm architecture.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.