我想通过BroadcastReceiver获得rssi值。当我在Android 4上实现以下代码时,效果很好(代码也只有BroadcastReceiver并注册了broadcastReceiver,并且没有该权限请求),但是在Android 6及更高版本中没有响应。我读到BroadcastReceiver已针对Android 6+进行了更改,它需要ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION权限。我在android清单中添加了此权限,并在MainActivity中请求了权限,但是当我在装有Android 7的手机上运行代码并在蓝牙设备为在那附近,是抛出异常,当我单击按钮时附近没有东西时,它什么也没显示。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter BTAdapter = BluetoothAdapter.getDefaultAdapter();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
checkLocationPermission();
proceedDiscovery();
}
});
}
private final BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(MainActivity.this,action, Toast.LENGTH_LONG).show();
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.textView);
rssi_msg.setText(rssi_msg.getText() + name + " => " + rssi + "dBm"+"\n");
}
}
};
protected void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
int MY_PERMISSIONS_REQUEST = 200;
int permissions=ContextCompat.checkSelfPermission (this,Manifest.permission.ACCESS_FINE_LOCATION);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST);
}}
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 0: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
proceedDiscovery();
}
else{}
break;
}
}}
protected void proceedDiscovery() {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
registerReceiver(receiver, filter);
BTAdapter.startDiscovery();
}}
并且我的android清单具有以下权限:
<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" />
我从此链接获得权限帮助: correct procedure for BT device discovery on Marshmallow
logcat中的异常:
08-16 14:33:16.341 6630-6630/com.example.hanane.rssi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hanane.rssi, PID: 6630
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } bqHint=1 (has extras) } in com.example.hanane.rssi.MainActivity$2@a1e9394
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1195)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xffffffd4
at android.content.res.Resources.getText(Resources.java:1184)
at android.widget.Toast.makeText(Toast.java:460)
at com.example.hanane.rssi.MainActivity$2.onReceive(MainActivity.java:75)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1185)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)
答案 0 :(得分:0)
正如您所说的异常,问题在于调用
Toast.makeText(MainActivity.this,action, Toast.LENGTH_LONG).show();
如文档所述,Toast.makeText
在传递R.string.<something>
时要求字符串资源标识符(String
)作为一个重载的第二个参数,或另一个action
作为另一个重载,这导致Toast在找不到action
值的标识符的情况下寻找资源。
如果要Toast
的{{1}}值,则必须先将其转换为字符串。最简单的方法是将其用作action
:
action + ""