gpt4 book ai didi

android - 蓝牙 ACTION_FOUND broadcastReceiver 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:54 29 4
gpt4 key购买 nike

我正在尝试计算蓝牙 RSSI 并找到了一些示例,但是 broadcastReceiver 不工作。代码是这样的:

private final BroadcastReceiver receiver = new BroadcastReceiver(){

@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
TextView rssi_msg = (TextView) findViewById(R.id.textView1);
rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm\n");
}
}
};

通过这个注册:

registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

当按钮被点击时,BTAdapter.startDiscovery();正在工作中。但是在 textview 中没有任何变化。

你能给我一些建议吗?

再次编辑:我稍微更改了我的代码,我将展示我的整个代码。

public class Blutooth extends Activity {

private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blutooth);

Button boton = (Button) findViewById(R.id.button1);
boton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
BTAdapter.startDiscovery();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.blutooth, menu);
return true;
}
}

接收者类是

public class TestReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String name = intent.getAction();
String device = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
String rssi_msg = Integer.toString(intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE));
Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
}

}

我正在尝试 Toast intent.getAction(); 但什么也没发生。我认为 BluetoothDevice.ACTION_FOUND 没有广播。

再次编辑:我使用的是android 6.0版本。我的整个 list 是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blutooth"
android:versionCode="1"
android:versionName="1.0" >

<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_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="23" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Blutooth"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".TestReceiver">
<intent-filter>
<action android:name="BluetoothDevice.ACTION_FOUND">
</action></intent-filter>
</receiver>
</application>

</manifest>

我听说 6.0 版本以上需要额外的权限才能使用 ACTION_FOUND。因此,我添加了 ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION。我在 Activity 中添加了 filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED); 代码。ACTION_NAME_CHANGED 运行良好,我可以获得 BluetoothDevice.EXTRA_NAME。但是 BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE 不工作。它打印默认值 -32768。BluetoothDevice.ACTION_FOUND 仍然不工作。

最佳答案

我终于找到了答案。Android 6.0 需要额外的权限,但仅向 Manifest 添加 uses-permission 是行不通的。您必须检查用户权限。我这样写我的代码:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_COARSE_LOCATION);

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled())
bluetoothAdapter.enable();
leScanner = bluetoothAdapter.getBluetoothLeScanner();

Button boton = (Button) findViewById(R.id.button1);
boton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
leScanner.startScan(scanCallback);
}
});

此代码显示权限弹出窗口。感谢您的许多回答。

关于android - 蓝牙 ACTION_FOUND broadcastReceiver 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38512993/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com