Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast if a bluetooth device is connecting/disconnecting?

I want start a service, when a specific bluetoothdevice is connected and stop this service when it's disconnected. Obviously I don't want to have a background service that is always checking if a BT device is connected, so I want to achieve this with a Receiver.

Is this actually possible? android.bluetooth.device.action.ACL_CONNECTED is mentioned here, but apparently it didn't work.

Thanks!

like image 881
Force Avatar asked Nov 04 '11 08:11

Force


People also ask

Why does a Bluetooth device connect then disconnect?

There may be too many apps running in the background of the device the Bluetooth is attempting to pair with. Certain applications cause interference with the connection, and some devices are limited in the number of applications that can run concurrently. If in doubt, check with the headset manufacturer.

Why won't my Bluetooth stay connected?

If possible, restart the Bluetooth device. Then, restart your phone or tablet. This can help fix minor issues and may help the Bluetooth device connect again. On devices with a regular Power key, press and hold the Power key until the Power menu is displayed.

Can someone connect to my Bluetooth without me knowing?

Can someone connect to my Bluetooth without me knowing? Theoretically, anyone can connect to your Bluetooth and gain unauthorized access to your device if the visibility of your Bluetooth device is on.


1 Answers

<intent-filter>
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>

These are the filters you have to add with your broadcast receiver.

ACL_CONNECTED signals when bluetooth is connected and ACL_DISCONNECTED signals bluetooth disconnection

For specific device you have to check intents/context in broadcast receiver

The two permission you have to add are:

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

I think this should solve your problem.

like image 81
Abhinav Singh Maurya Avatar answered Sep 18 '22 17:09

Abhinav Singh Maurya