我正在尝试计算蓝牙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();
}
}
我正在尝试敬酒,intent.getAction();
但没有任何反应。我认为BluetoothDevice.ACTION_FOUND
这不是广播。
再次编辑:我正在使用android 6.0版本。我的整个清单是这样的:
<?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_LOCATION
和ACCESS_COARSE_LOCATION
。我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);
}
});
此代码显示权限弹出窗口。感谢您提供许多答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句