Android的蓝牙 - 从设备检测到断线 [英] Android Bluetooth - detect a disconnection from a device
问题描述
我想抓住一个蓝牙设备断开连接的意图过滤器。 我添加了一个记录到的onReceive但它永远不会达到它并没有在logcat中显示。 我怀疑,问题是我的manifest.xml配置:
清单:
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.company
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =10
机器人:targetSdkVersion =16/>
<使用-权限的Android:名称=android.permission.BLUETOOTH/>
<使用-权限的Android:名称=android.permission.BLUETOOTH_ADMIN/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<接收器的Android版本:NAME =com.company.MyReceiver机器人:启用=真正的机器人:出口=真正的>
<意向滤光器>
<类机器人:名称=android.intent.category.DEFAULT/>
<作用机器人:名称=android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED/>
<作用机器人:名称=android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED/>
<作用机器人:名称=android.bluetooth.adapter.action.DISCOVERY_STARTED/>
&所述; /意图滤光器>
< /接收器>
<活动
机器人:名称=。BTActivity
机器人:标签=BTActivity>
< /活性GT;
< /用途>
< /舱单>
MyReceiver延伸BroadcastReceiver的:
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.i(得到式,得到-IN-);
//串动= intent.getAction();
BluetoothDevice类设备=意图
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i(断开,device.getName());
意图I =新的意图(背景下,BTActivity.class);
叠B =新包();
b.putString(DEVICENAME,device.getName());
intent.putExtras(B); //把你的身份证给你的下一个意向
context.startActivity(ⅰ);
// 完();
}
使用这种code,用以接收断开(Bluetooth时仍是打开的):
<意向滤光器> <作用机器人:名称=android.bluetooth.device.action.ACL_CONNECTED/>
<作用机器人:名称=android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED/>
<作用机器人:名称=android.bluetooth.device.action.ACL_DISCONNECTED/>
&所述; /意图滤光器>
你也需要注册一个然后注册一个广播接收器的蓝牙状态变化的服务,让您的应用程序知道,当蓝牙被关闭,因此放弃所有的有源器件,因为您将不会收到ACL_DISCONNECT当蓝牙只是把关闭。
@覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
IntentFilter的过滤器=新的IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bluetoothTurnedOnOff,过滤器);
返回START_STICKY;
}
私人最终的BroadcastReceiver bluetoothTurnedOnOff =新的BroadcastReceiver(){
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1)== BluetoothAdapter.STATE_OFF){
[...]
I am trying to catch a bluetooth device disconnection intent filter. I added a log to the onReceive but it never reaches it and is not displayed in the logcat. I suspect that the problem is with my manifest.xml configuration:
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.company.MyReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
<action android:name="android.bluetooth.adapter.action.DISCOVERY_STARTED" />
</intent-filter>
</receiver>
<activity
android:name=".BTActivity"
android:label="BTActivity" >
</activity>
</application>
</manifest>
MyReceiver extends BroadcastReceiver:
@Override
public void onReceive(Context context, Intent intent) {
Log.i("got-in", "got-in-");
// String action = intent.getAction();
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.i("disconnect", device.getName());
Intent i = new Intent(context, BTActivity.class);
Bundle b = new Bundle();
b.putString("deviceName", device.getName());
intent.putExtras(b); // Put your id to your next Intent
context.startActivity(i);
// finish();
}
Use this code for receiving a disconnect (when Bluetooth is still turned on):
<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>
Also you have to register a service that then registers a broadcast receiver for Bluetooth status change, so your app knows when Bluetooth was turned off and therefore discards all active devices, as you won't receive a ACL_DISCONNECT when Bluetooth is simply turned off.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bluetoothTurnedOnOff, filter);
return START_STICKY;
}
private final BroadcastReceiver bluetoothTurnedOnOff = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
[...]
这篇关于Android的蓝牙 - 从设备检测到断线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!