发现蓝牙设备时未注册/调用广播接收器

时间:2019-11-27 08:07:57

标签: android bluetooth broadcastreceiver android-bluetooth

我的应用程序需要发现附近的蓝牙设备。 我的工作流程如下所示:

检查蓝牙状态->启用IF->禁用它,然后启用并搜索附近的设备,否则,将其启用并搜索附近的设备。

我正在使用广播接收器来获取附近的设备,但是问题是接收器没有被触发。

下面是我的代码。

MyFragment.java:

text = input("Please enter the statement you want to check: ")
word = input("Please enter the word you want to check in the statement: ")

# n is the starting point to find the word, and it's 0 cause you want to start from the very beginning of the string.
n = 0

# position_word is the starting Index of the word in the string
position_word = 0
num_occurrence = 0

if word.upper() in text.upper():
    while position_word != -1:
        position_word = text.upper().find(word.upper(), n, len(text))

        # increasing the value of the stating point for search to find the next word
        n = (position_word + 1)

        # statement.find("word", start, end) returns -1 if the word is not present in the given statement. 
        if position_word != -1:
            num_occurrence += 1

    print (f"{word.title()} is present {num_occurrence} times in the provided statement.")

else:
    print (f"{word.title()} is not present in the provided statement.")

让我知道错误/错误在哪里。 预先感谢。

1 个答案:

答案 0 :(得分:0)

我认为这是你所寻找的:

Android Broadcast Receiver bluetooth events catching

还要检查清单文件或添加所需文件:

 <uses-feature android:name="android.hardware.bluetooth" />
 <uses-permission android:name="android.permission.BLUETOOTH" />
 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

还要在清单中添加广播接收器,确保您添加了类似于此目的的意图过滤器

 <receiver android:name=".MyBroadcastReceiver"  android:exported="true">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
    <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
</intent-filter>

https://developer.android.com/guide/components/broadcasts

相关问题