whilu / lmbluetoothsdk Goto Github PK
View Code? Open in Web Editor NEWA library to make classic bluetooth or BLE easier to use in Android.
A library to make classic bluetooth or BLE easier to use in Android.
Hi could you suggest the ideal way to maintain connection between activities.
I am defining the BluetoothController as static in main activity and defining a local as follows
public BluetoothController mBluetoothController = MainActivity.mBluetoothController;
and then using that object i read the incoming data
mBluetoothController.setBluetoothListener(new BluetoothListener() { ... }
This works 90% of the time but i get this error at times
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x/com.x.yActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void co.lujun.lmbluetoothsdk.BluetoothController.setBluetoothListener(co.lujun.lmbluetoothsdk.base.BluetoothListener)' on a null object reference
Do let me know how i can resolve this issue !!
Recently i have implemented the Bluetooth scanning in the foreground service every 5minutes.
I have noticed that in android 10 Samsung a11 device scan results are empty during screen lock and if its in background.
Also i had quick look on your code to fix this issue but everything seems good in the BLEController.java class you have added scan settings based on different android device.
But still i cant fix it? any guess or fix you may know?
Library cannot be found on Maven Central.
try {
if (mmInStream.read()<=0){
continue;
}else {
Thread.sleep(1000);
}
bytes = mmInStream.read(buffer);
if (bytes > 0) {
byte[] data = Arrays.copyOf(buffer, bytes);
if (mBluetoothListener != null) {
((BluetoothListener) mBluetoothListener).onReadData(mmSocket.getRemoteDevice(), data);
}
}
Hello I am trying to get the connected devices.
getConnectedDevice() what does it return?
I do not use startAsServer()
method but sometimes I got STATE_LISTEN
after STATE_CONNECTING
in onBluetoothServiceStateChanged
. In that case, I cannot connect to my target device (microcontroller). disconnect()
and reconnect()
does not help. What should I do? Is it a bug?
Would it be possible to add gatt advertising to the library?
Eg. http://blog.csdn.net/u013606170/article/details/46038283
including custom characteristics.
Please add callback on finish/stop scanning BLE devices,
Please add check if scanning now.
Thanks for your support.
Why do you call BluetoothService.this.start();
on line BluetoothService.class#316 ?
In the code, if the connection fails (outgoing connection at ConnectionThread), why does it decides to call AcceptThread to start listening for incoming connections?
Hi,
I try to set characteristic notification with:
BluetoothLEController.getInstance().setReadCharacteristic(MY_CHARACTERISTIC_UUID_AS_STRING);
But that doesn't work, onDataChanged(BluetoothCharacteristic characteristic)
is not called.
Following the Bluetooth Smart standard on Android, you also need to write to the descriptor which is not done in the library.
I've downloaded the library from Github and did the following modification:
for (BluetoothGattDescriptor descriptor : descriptors) { if (descriptor.getUuid().equals(Descriptor.CLIENT_CHARACTERISTIC_CONFIGURATION)) { BluetoothLEController.getInstance().setReadCharacteristic(MY_CHARACTERISTIC_UUID_AS_STRING); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); } }
And then I become the notifications. Either you should change this directly in the library or you could send back mBluetoothGatt in onDiscoveringCharacteristics so we can write to the descriptor ourselves.
Let me know what you think about it.
Best,
JB
Solution:
http://stackoverflow.com/questions/33142034/android-6-bluetooth
Hello!
I faced with some issue.
I'm trying to send a number of messages through BT using this lib. My code looks like
for (Banner bannerItem : banners) {
JSONObject bannerJson = new JSONObject();
bannerJson.put("image",bannerItem.getImageUrl());
bannerJson.put("position", bannerItem.getPosition());
ALog.error("Do Send %s", bannerJson.toString());
bluetoothController.write(bannerJson.toString().getBytes());
}
Sometimes receiver app receives current item and previous one, e.g.
first message: {banner ob 1}
second message: {banner ob 1}{banner obj 2}
I used this library for serial data comm from arduino, there is no deliminator support so that way data buffer is filled with bad character values.
@Override
protected void onCreate(){
mBLEController = BluetoothLEController.getInstance().build(this);
mBLEController.setBluetoothListener(bluetoothLEListener);
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
mBLEController.release();
}catch (Exception e){
LogManager.LogEA(e);
}
}
@Override
protected void onPause() {
super.onPause();
/*my custom key, use when scanLe*/
if(getCurrentState() == START_SCAN_STATE) {
mBLEController.cancelScan();
}
}
Please fix, thank you for support.
I am using BLE Simulator (Battery Service).
Please add callback when BLEService was inited, thanks. Sorry, but I use fragment and main activity is owner BLE Service, so I needed callback on service connection at fragment
private final ServiceConnection serviceConnectionListener = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
LogManager.LogEA("Service connected");
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
LogManager.LogEA(getString(R.string.unable_to_initialize_bluetooth));
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setMessage(getString(R.string.unable_to_initialize_bluetooth)).setCancelable(false);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
});
builder.create().show();
}
});
}else{
bluetoothListener.onBluetoothServiceConnected();
LogManager.LogEA(bluetoothListener.getBluetoothListenerId());
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
LogManager.LogEA("Service disconnected");
}
};
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.