Bluetooth scan

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Bluscre
Posts: 121
Joined: 31 Aug 2017 13:58
Location: Germany
Contact:

Bluetooth scan

Post by Bluscre » 13 Aug 2019 14:12

I am searching for a bluetooth scan for my auto connect script


http://automagic4android.com/flow.php?i ... e8bc7e8619

Or even better a function "connect to any/first paired device in reach"
Unofficial AutoMagic Telegram Group: http://t.me/automagic4android
Check out my other flows here: https://github.com/Bluscream/AutoMagicFlows or here.
Sent from my Lenovo TAB 2 A10-70L running Android 6.0.

User avatar
Martin
Posts: 4345
Joined: 09 Nov 2012 14:23

Re: Bluetooth scan

Post by Martin » 15 Aug 2019 18:58

Hi,

There's no built in action to execute a scan since a scan is very heavyweight and can cause interruptions to already connected BT devices.
You can create your own action with action Script:

Code: Select all

bta=callJavaStaticMethod("android.bluetooth.BluetoothAdapter", "getDefaultAdapter()");
callJavaMethod(bta, "android.bluetooth.BluetoothAdapter", "startDiscovery()")
You can handle the found devices with trigger General Broadcast. You can use example Bluetooth Device found as the base configuration but add following line in the Access intent extras field:

Code: Select all

device = getParcelable("android .bluetooth.device.extra.DEVICE");
The device will contain the address of the device which can be used in action Connect/Disconnect Bluetooth Device.

According to the documentation you should not attempt to create a connection as long as the discovery process is running so you might have to stop discovery first with another action Script:

Code: Select all

bta=callJavaStaticMethod("android.bluetooth.BluetoothAdapter", "getDefaultAdapter()");
callJavaMethod(bta, "android.bluetooth.BluetoothAdapter", "cancelDiscovery()")
Entry point for documentation can be found here:
https://developer.android.com/reference ... othAdapter

I hope this helps.

Regards,
Martin

Post Reply