如何接收工作的蓝牙设备意图? [英] How to receive intents for Bluetooth devices working?

查看:166
本文介绍了如何接收工作的蓝牙设备意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有我的应用的服务侦听蓝牙连接和断开的尝试,这样我就可以动态检查/支持蓝牙圈养的网络通信。

I'm trying to have my app's service listen for Bluetooth connection and disconnection attempts, so I can dynamically check/support Bluetooth tethering network communication.

首先,我有两个三星S4S(运行CyanogenMod的10.2,这是基于Android 4.3.1),我可以对就好了。如果我一台设备设置为蓝牙系绳,其他所连接,创建一个新的BT-泛网络接口,并使用DHCP分配IP的时候。我证实了这一点使用iwconfig和ifconfig壳。

First I have two Samsung S4s (running CyanogenMod 10.2, which is Android 4.3.1 based) which I can pair just fine. If I set one device to Bluetooth tether, when the other connects, a new bt-pan network interface is created and DHCP is used to assign IPs. I confirmed this using iwconfig and ifconfig in shell.

我在我的应用程序如下权限:(还有更多,我只是指出我加入了BT烫发)

I have the following perms in my app: (there's more, I'm just pointing out the BT perms I added)

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

下面是我服务的onCreate在那里设置我IntentFilters(注:我有吐司在这里,但我是用日志记录。原来)

Here's my Service's onCreate where I set my IntentFilters: (note I've got Toasts here, but I was working with Logging originally)

@Override
public void onCreate() {
    super.onCreate();
    ...     
    this.mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);

    mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);
}

下面是我的BroadcastReceiver实现:

Here's my BroadcastReceiver implementation:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals(BluetoothDevice.ACTION_FOUND)) {
            Toast.makeText(getApplicationContext(), "BT found", Toast.LENGTH_SHORT).show();
        } else if(action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
            Toast.makeText(getApplicationContext(), "BT Connected", 
        } else if(action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(getApplicationContext(), "BT Disconnect requested", Toast.LENGTH_SHORT).show();
        }
    }
}

现在,当我打开蓝牙/关闭,连接/断开配对设备,没有火灾。我祈祷,周围与两端的设备。没有什么是广播。

Now, when I turn Bluetooth off/on, connect/disconnect to a paired device, nothing fires. I've payed around with the devices from both ends. Nothing is broadcast.

任何人有一个建议?我真的需要接收这些蓝牙事件。请不要指向另一篇文章/网站使用相同的烫发和意图过滤器。谢谢你。

Anyone have a suggestion? I really need to receive these bluetooth events. Please don't point to another post/site with the same perms and intent filters. Thanks.

推荐答案

我有一个非常类似code这工作,唯一的主要区别,我发现是这样的:

I have a very similar code which works, the only major difference i found was this:

mLocalBroadcastManager.registerReceiver(mMessageReceiver, filter);

我beleive的 registerReceiver 应该从你想要得到的意图,以在上下文中调用。
尝试调用从方法本。即删除 mLocalBroadcastManager 这样的:

i beleive that registerReceiver should be called from the context you want to get intents to. try calling the method from this. i.e remove the mLocalBroadcastManager like:

registerReceiver(mMessageReceiver, filter);

这篇关于如何接收工作的蓝牙设备意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆