Thursday, 15 April 2010

android - How to tell if a bluetooth device is connected? My code isn't working -



android - How to tell if a bluetooth device is connected? My code isn't working -

i trying hear bluetooth connection/disconnection events determine if there bluetooth device connected. i'm trying way app run on older android versions.

i registered receiver in manifest such:

<receiver android:name=".bluetoothreceiver"> <intent-filter> <action android:name="android.bluetooth.device.action.acl_connected" /> </intent-filter> </receiver>

and in bluetoothreceiver class have

public class bluetoothreceiver extends broadcastreceiver{ public final static string tag = "bluetoothreciever"; public void onreceive(context context, intent intent) { log.d(tag, "bluetooth intent recieved"); final bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device); string action = intent.getaction(); if (bluetoothdevice.action_acl_connected.equalsignorecase(action)) { log.d(tag, "connected " + device.getname()); } if (bluetoothdevice.action_acl_disconnected.equalsignorecase(action)) { log.d(tag, "disconnected " + device.getname()); } }}

but when connecting , disconnecting bluetooth, nil happens. doing wrong?

you have manipulate modify_audio_settings finger if bluetooth headset connected. 1 time you've defined intent, , configure manifest correctly should work. headphone jack, can applied bluetooth if utilize above mentioned.

here 2 sites may help explain in more detail:

http://www.grokkingandroid.com/android-tutorial-broadcastreceiver/ http://www.vogella.com/articles/androidbroadcastreceiver/article.html

you have define intent; otherwise won't access scheme function. broadcast receiver; alert application of changes you'd hear for.

every receiver needs subclassed; must include onreceive(). implement onreceive() you'll need create method include 2 items: context & intent.

more service ideal; you'll create service , define context through it. in context; you'll define intent.

an example:

context.startservice (new intent(context, yourservice.class));

very basic example. however; particular goal utilize system-wide broadcast. want application notified of intent.action_headset_plug.

how subscribe through manifest:

<receiver android:name="audiojackreceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.headset_plug" /> </intent-filter> </receiver>

or can define through application; but. particular request; require user permissions if intend observe bluetooth modify_audio_settings.

update:

i misread question, thought trying find bluetooth headsets; not bluetooth paired.

over on question; explain how check paired bluetooth devices.

import bluetooth package.

import android.bluetooth.*;

set permission.

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

access bluetooth adapter:

bluetoothadapter bluetooth = bluetoothadapter.getdefaultadapter();

run check connection not null.

if (bluetooth != null) { // something. }

make sure enabled:

if (bluetooth.isenabled()) { // enabled, work bluetooth } else { // not enabled else. }

display bluetooth state bluetoothadapter.getstate()

by default turned off; you'll have enable it.

string state = bluetooth.getstate(); status = devicename + " : " + devicelocation + " : " + devicestate;

android android-intent

No comments:

Post a Comment