Scan Bluetooth devices in android studio - android

I try to write a code from documentation but every time it asks for self-check permission and I did not get any device name
Help me to write a code to scan devices in the area
GitHub repositor link for my application
https://github.com/sachinraj0093/BluetoothAttendance/blob/master/app/src/main/java/com/example/bluetoothattendenceapplication/MainActivity.java

If your device running on Android 11 or lower, you need to check the Location is turned on. And in your code, you lack of calling discovery in case !mbluetoothAdapter.isDiscovering().
You can check returned value when call BluetoothAdapter.startDiscovery() to examine the status for start discovery action.

Related

Can not run Wifi and Bluetooth scans in background with Flutter

I am working on a part of an indoor localization project. I need my Flutter application scan and get Mac addresses and RSSI values of Wifi and Bluetooth devices. I am using flutter_blue for bluetooth scans and wifi_scan for Wifi scans. The application works well while in the foreground but scans are failing when the application is in background.
What I have tried so far:
Tried to change Bluetooth scan mode from ScanMode.lowLatency to ScanMode.lowPower and ScanMode.opportunistic for the startScan() method for flutter_blue.
flutter_background. I managed to do some tasks in background but wifi and bluetooth did not work.
flutter_background_service the same situation
flutter_foreground_plugin the same situation
flutter_foreground_task the same situation
workmanager did not try it because it says it can trigger the bg operation per every 15 minutes but I am scanning per every 6 seconds.
Is there any way to achieve this? Aren't there any package with the option backgroundMode: true? I don't know the natives. Don't know Kotlin too.
According to the Android Developer documentation, ScanMode.lowPower is the only possible mode for background scanning. You also need to request location access for the background (documentation here and here).
Consider switching to Flutter Blue Plus instead of Flutter Blue; Flutter Blue Plus is the currently-maintained version. This pull request on GitHub shows that Flutter Blue cannot run in the background. The pull request has never been merged.
After a lot of things have been tried, I just tried to get GPS location too.
Used this package: location
And execute location.enableBackgroundMode(enable: true) as it is stated in readme, then my wifi and bluetooth scans starts working in background too.
It's Ridicoulus

Android Ble, Pheriperal Advertise status during the scan

In my application I need to start some BLE scans, get the results and then show them in a list or in a grid.
When i start the scan my device start to look for advertisements basing on which ScanFilters I wrote.
Every time a Pheriperal is found I the application trigger the scan callback method and i can add the new device into the list.
If i got an error the app trigger the callback method and i can tell the user about the broblem.
Example
Imagine the Scan going on for 30s.
At the moment it starts i get 3 Ble Pheriperal.
At second 15 one of them stop to advertise and turn bluetooth off.
At the end of the scan my list will have 3 Ble devices but i will not be able to connect to one of them.
How do I know which of them is the one which turned off?
Where do i get his status?
Thanks for the help.
You can try this, technically it is possible as per the documentation. in onScanResult callback, check for the callbackType parameter. If it is CALLBACK_TYPE_MATCH_LOST then get the device from the results and remove it from your cache.

Android Beacon Library and Update 6.0.1

Yesterday I updated my Samsung Galaxy S5 Neo to the new Android Version 6.0.1. Since then the beacons won't be detected anymore. It does connect the Beacon Service, Updates the scan perios, etc. but it doesn't find any beacons.
Bluetooth is activated and the Beacon scanner of the Beacon Manifacturer still works fine (finds all the beacons). But not my app... It all worked before the new Android Update.
A few things to check:
Verify Location is enabled in Settings -> Location. This must be set to High Accuracy or Battery Saving. If set to Device Only, it won't work. Starting with Android 6, Location must be enabled to do bluetooth scans.
Make sure your app has been granted location permission. Check Settings -> Apps -> Your App -> Permissions, and verify the Location slider is set to on. Starting with Android 6, each app must dynamically request location access at runtime. If your app doesn't have code to do this, you'll need to add it. (Although it is possible to override in settings as described here.)
Make sure your app's manifest declares either FINE_LOCATION or COARSE_LOCATION in its declared permissions.
If the above don't help check LogCat (not just filtering on your application) for bluetooth errors.
See here for more info on the above.

Beacon detection stops when giving ACCESS_FINE_LOCATION permission in Marshmallow

I have set beacon background scan using this tutorial in BaseApplication class but in Marshmallow running device it shows this log:
Caught a RuntimeException from the binder stub implementation.
java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
And finally with this and this reference i was able to give location access for Marshmallow running device to detect beacons.
My Problem:
Even when i give Location access it doesn't detect beacons and also stops to show above Log. Is it the problem as in this ISSUE. My Nexus 5 Build number is MRA58N
UPDATE: When i turn on Location manually now it works. But it's strange. Is it right way to detect beacon?
Android Marshmallow introduces an entirely new spin on application permissions,Users now have the ability to revoke runtime permissions whenever they desire. This means that you can’t assume the app has access to the permission, even if it had been granted previously. You can refer this lib or this guide. And you can create a interface listener location changed after enable GPS, when location != 0. After enable GPS you must resume. I Hope this will help you out.

Is it possible to pair BluetoothDevice programmatically android?

Currently Iam using backport.android.bluetooth2.jar file for bluetooth programming with Android 1.5.Whenever device pairing fails it populates alert dialog for passKey and if i give passkey then in my log iam getting below error
07-21 12:25:52.419: WARN/BluetoothDeviceService(948): setPin(00:0E:6D:BA:89:F3) called but no native data available, ignoring. Maybe the PasskeyAgent Request was cancelled by the remote device or by bluez.
For successfully pair my BT device I always rebooting my tablet and manually pair the device with Settings options.So at last i realised to give this passKey programmatically but i haven't found any method for this.But in google opensource i found one method that is commented with #hide setPassKey(String Address,int passKey) in BluetoothDevice.java of backport.android.bluetooth2.jar .This will n't appear in my jar file.If any way is there for pairing the unbounded device please give reply ASAP?
In the AndroidBluetoothAPI_0.3.jar file following methods are available i,e: pair(address) and setPin() for setting the pin programmatically and connecting the BT device.For finding the signal strength method is available in this api.Below is the code project link
http://code.google.com/p/android-bluetooth/downloads/detail?name=AndroidBluetoothAPI_0.3.jar&can=2&q=

Categories

Resources