GithubHelp home page GithubHelp logo

javablex's Introduction

JavaBlex

Licence

Bluetooth

Windows macOS Ubuntu Linux

A Java Library for Bluetooth Low Energy of Windows10+ Linux macOS.

Gradle/Maven

allprojects {
    repositories {
    ...
    maven { url 'https://jitpack.io' }
    }
}
dependencies {
        implementation 'com.github.youxingz:javablex:v0.0.3'
}
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.github.youxingz</groupId>
        <artifactId>javablex</artifactId>
        <version>v0.0.3</version>
    </dependency>
</dependencies>

Example

BlexAdapter adapter = BlexAdapter.getAdapter(0);
int count = adapter.getAdapterCount();
System.out.println("Adapter count: " + count);
adapter.setCallbackOnScan(new BlexAdapter.AdapterScanCallback() {
    @Override
    public void onScanStart(BlexAdapter adapter) {
            System.out.println("scan start.");
    }
    
    @Override
    public void onScanStop(BlexAdapter adapter) {
            System.out.println("scan stop.");
    }
    
    @Override
    public void onDeviceUpdate(BlexAdapter adapter, BlexPeripheral peripheral) {
            System.out.println("scan update:");
            System.out.println(peripheral.getAddress());
    }
    
    @Override
    public void onDeviceFound(BlexAdapter adapter, BlexPeripheral peripheral) {
            System.out.println("scan found:");
            System.out.println(peripheral.getAddress());
            }
    });
adapter.setScanTimeout(5000);
adapter.startScan();
// ...
adapter.stopScan();

When you use BlexAdapter find some device, i.e., BlexPeripheral, you can connect disconnect read write notify with that device.

BlexPeripheral peripheral = ...;
peripheral.setCallbackOnConnection(new BlexPeripheral.PeripheralConnectionCallback() {
    @Override
    public void onConnection(BlexPeripheral peripheral, boolean isConnected) {
        System.out.println("Connection? " + isConnected);
    }
});
peripheral.connect();
// wait until connected.
if (peripheral.getServicesCount() == 0) return;
BlexPeripheral.BlexService service = peripheral.getServices(0);
BlexPeripheral.BlexCharacteristic[] characteristics = service.getCharacteristics();
if (characteristics.length == 0) return;
// subscribe a notify event:
peripheral.notify(service.getUuid(), characteristics[0].getUuid(), new BlexPeripheral.NotifyCallback() {
    @Override
    public void onNotify(BlexPeripheral.BlexUUID service, BlexPeripheral.BlexUUID characteristic, byte[] data, int data_length, boolean isIndication) {
        System.out.println("Data income: ");
        System.out.println(Arrays.toString(data));
    }
});
//...
peripheral.disconnect();

javablex's People

Contributors

youxingz avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

roumaimed

javablex's Issues

java.Lang.UnsatisfiedLinkError when running "BlexAdapter.getAdapter(0);" on macOS

Hello,

Thanks for this library. I am trying to integrate it into a Java BLE application for macOS, and am having a few issues at runtime. I am getting "java.lang.UnsatisfiedLinkError" and the plugin is unable to load the native libraries required. Does this project currently work with macOS?

I have a MacBook Air with a M1 chip.

Thanks very much

Missing *.so library for Linux

The library cannot be executed on Linux because the jar doesn't contain the *.so library.

  static {
    try {
      String file = "/lib/libblex";
      if (Platform.isWindows()) {
        file = file + ".dll";
      } else if (Platform.isMac()) {
        file = file + ".dylib";
      } else if (Platform.isLinux()) {
        file = file + ".so";
      }

      instance = (Lib)NativeUtils.loadLibraryFromJar(file, Lib.class);
    } catch (IOException var1) {
      throw new RuntimeException(var1);
    }
  }

Exception:
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: File /lib/libblex.so was not found inside JAR.

peripheral.connect() function crashing program

Hello,

I am trying to integrate this library into a BLE plugin I am writing. I can use my Bluetooth adapter to search for and discover peripherals, but if I then try to connect using the peripheral.connect() function, the program errors with the following:

Connectable: true Connection? true Sending connected libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc ./tuxguitar.sh: line 24: 57426 Abort trap: 6 "${JAVA}" ${VM_ARGS} ${SWT_ARGS} -cp :${CLASSPATH} -Dtuxguitar.home.path="${TG_DIR}" -Djava.library.path="${LD_LIBRARY_PATH}" ${MAINCLASS} "$@"

The associated code which causes the above output is here:

`public void connectToDevice() throws InterruptedException {
	
	myBluetoothPeripheral.setCallbackOnConnection(new BlexPeripheral.PeripheralConnectionCallback() {
		
		@Override
		public void onConnection(BlexPeripheral peripheral, boolean isConnected) {
			System.out.println("Connection? " + isConnected);
			if (isConnected) {
				sendConnected();
			}
		}
	});
	System.out.println("Connectable: " + myBluetoothPeripheral.isConnectable());
	myBluetoothPeripheral.connect();
}`

I am setting myBluetoothPeripheral to one of the discovered BlexPeripherals, based on the device identifier. I have tried various Thread.sleep waits to allow the device to finish connecting (the connection LED on my peripheral goes solid, so I believe connection is occurring), however this does not stop the above crash.

I am using javablex version 0.0.8

Any assistance would be greatly appreciated! Thanks

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.