Git Product home page Git Product logo

ble_peripheral's Introduction

Medium Github Badge Linkedin Badge Gmail Badge Instagram Badge

Hello! Welcome to my profile :octocat:

  • ๐Ÿ”ญ Iโ€™m currently working on Mobile & Web development
  • ๐ŸŒฑ Dart, ๐Ÿ’™ Flutter, Java, Kotlin, Javascript, Python, Swift, PHP, HTML
  • ๐Ÿ‘ฏ Looking to contribute with the organization and Opensource Flutter Apps/Libraries and Web projects ,
  • ๐Ÿ“ซ How to reach me : Telegram
  • โšก Passionate about new mobile technologies.



ble_peripheral's People

Contributors

pipjapp avatar rohitsangwan01 avatar

Stargazers

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

Watchers

 avatar

ble_peripheral's Issues

Android fatal runtime error when not initialized but bluetooth state changes

So i stumbled upon a curious bug, like the title said.

The workflow is currently this:

1: Permissions need to be asked and given, that means the user has some time on page if they refuse the first time around.
2: Then and only then can BlePeripheral.initialize() be called.
Now, if the user declines the permissions, initialize hasn't been called, and the user manually turns bluetooth on or off (or its done by some other way), the whole application will fatal crash and close with the stacktrace:

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.adapter.action.STATE_CHANGED flg=0x4000010 (has extras) } in com.rohit.ble_peripheral.BlePeripheralPlugin$broadcastReceiver$1@f88b5e9
	at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1810)
	at android.app.LoadedApk$ReceiverDispatcher$Args.$r8$lambda$gDuJqgxY6Zb-ifyeubKeivTLAwk(Unknown Source:0)
	at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
	at android.os.Handler.handleCallback(Handler.java:958)
	at android.os.Handler.dispatchMessage(Handler.java:99)
	at android.os.Looper.loopOnce(Looper.java:205)
	at android.os.Looper.loop(Looper.java:294)
	at android.app.ActivityThread.main(ActivityThread.java:8177)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property handler has not been initialized
	at com.rohit.ble_peripheral.BlePeripheralPlugin$broadcastReceiver$1.onReceive(BlePeripheralPlugin.kt:475)
	at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1802)
	... 10 more

So i guess the solution would be to add checks to see if its initialized on the code that catches the state change and if it is, then proceed, if not then don't do anything.

Service is added twice

Hey,

I noticed that one of the services passed to the "addServices" function is being added twice. I believe there is an oversight in the "addServices" function (if it is important, I was adding a list with only one service):

override fun addServices(services: List<BleService>) {
// To make sure that we are safe from race condition
val totalServices = services.size - 1
for (i in 0..totalServices) {
servicesToAdd.add(services[i].toGattService())
}
addService(services.last().toGattService())
}

as you can see, you add all the services to the queue and then explicitly add the last one, so it looks like it is added twice. (First time with explicit add, next time with queued add)

I have never used kotlin, but I think it can be solved this way:

    override fun addServices(services: List<BleService>) {
        // To make sure that we are safe from race condition
        val totalServices = services.size - 1
        val numOfServicesToAdd = if (servicesToAdd.peek() != null) totalServices else (totalServices - 1) 
        
        for (i in 0..numOfServicesToAdd) {
            servicesToAdd.add(services[i].toGattService())
        }
        
        // Start a queue if it isn't started
        if (numOfServicesToAdd != totalServices) {
            addService(services.last().toGattService())
        }
    }

I tested this, and this code seems to solve the problem.

If my code is good enough, I can make PR with this change.

Thanks!

Android if localName not defined

Running this on Android.

Expected: When localName is not defined => do not broadcast name or use device name.

Error: AdvertisingStatus: false Error Data too large

Current workaround: define localName with a "space" to get around error.

onWriteRequest 's value is null

The value parameter in onWriteRequest method of BleCallback is returning a null value. I'm using the code of the example app. And using the fixes mentioned in #2

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:10909_3

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:10909_3
at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:1449)
at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:1070)
at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:282)
at io.flutter.embedding.engine.dart.DartExecutor$DefaultBinaryMessenger.send(DartExecutor.java:470)
at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:223)
at io.flutter.plugin.common.BasicMessageChannel.send(BasicMessageChannel.java:106)
at com.rohit.ble_peripheral.BleCallback.onWriteRequest(BlePeripheral.g.kt:490)
at com.rohit.ble_peripheral.BlePeripheralPlugin$gattServerCallback$1.onCharacteristicWriteRequest(BlePeripheralPlugin.kt:373)
at android.bluetooth.BluetoothGattServer$1.onCharacteristicWriteRequest(BluetoothGattServer.java:219)
at android.bluetooth.IBluetoothGattServerCallback$Stub.onTransact(IBluetoothGattServerCallback.java:256)
at android.os.Binder.execTransactInternal(Binder.java:1220)
at android.os.Binder.execTransact(Binder.java:1179)

Steps:

  1. Start advertising from first device
  2. Pair and connect as a second device
  3. call Characteristic.write([0x12, 0x34]) from second device
    And crashes in first advertisible device

Please help me!

How to get value from write of a central device ?

First, thank you providing us this repo, it is very helpfull.

From central device with another device I perform a simple write to a characteristic (using flutter blue).

await char1.write(utf8.encode("Test"));

On Peripheral side I am expecting getting the value from callback with

BlePeripheral.setWriteRequestCallback((characteristicId, offset, value) {
  Get.log("WriteRequest: $characteristicId : $offset : $value");
      String? resultString = value != null ? utf8.decode(value) : null;
      print(resultString);
      print(value);
      print(characteristicId);
}

But I get

I/flutter (27720): null
I/flutter (27720): null
I/flutter (27720): 00002a18-0000-1000-8000-00805f9b34fb

But when I trigger this function (from periphal side)

  void updateCharacteristic() async {
    for (var device in devices) {
      try {
        await BlePeripheral.updateCharacteristic(
          deviceId: device,
          characteristicId: characteristicTest,
          value: utf8.encode("Test Data"),
        );
      } catch (e) {
        Get.log("UpdateCharacteristicError: $e");
      }
    }
  }

And then I try to perform a write again from central device, I got the date from the updateCharacterisc in the log each time I trigger the write, and not the null result again.. but still I don't get data from central.

I/flutter (29461): Test Data
I/flutter (29461): [84, 101, 115, 116, 32, 68, 97, 116, 97]
I/flutter (29461): 00002a18-0000-1000-8000-00805f9b34fb

I guess I am missing something here, how could I get within the peripheral the data sent with write from a central device ?

Thank

Throw exception "Bluetooth Permission not granted" even permission granted

When I call intitialize method, it throws this exception:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Exception, java.lang.Exception: Bluetooth Permission not granted, Cause: null, Stacktrace: java.lang.Exception: Bluetooth Permission not granted
at com.rohit.ble_peripheral.BlePeripheralPlugin.initialize(BlePeripheralPlugin.kt:71)
at com.rohit.ble_peripheral.BlePeripheralChannel$Companion.setUp$lambda$1$lambda$0(BlePeripheral.g.kt:277)
at com.rohit.ble_peripheral.BlePeripheralChannel$Companion.$r8$lambda$MnuKy-RVGk0M2vNjak8GFv1UlD4(Unknown Source:0)
at com.rohit.ble_peripheral.BlePeripheralChannel$Companion$$ExternalSyntheticLambda0.onMessage(Unknown Source:2)
at io.flutter.plugin.common.BasicMessageChannel$IncomingMessageHandler.onMessage(BasicMessageChannel.java:219)
at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8663)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
, null)
#0 BlePeripheralChannel.initialize (package:ble_peripheral/src/ble_peripheral.g.dart:249:7)

#1 BlePeripheral.initialize (package:ble_peripheral/ble_peripheral.dart:11:5)

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.