6.01中的蓝牙似乎无法正常工作,具有以下代码和权限,根据更新+ appCompat for Kitkat 4.4.4。
没有返回任何结果,附近有几个可发现的设备。
任何人都有任何洞察力?我在Nexus 5上运行。
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
package bluetoothscanneractivity;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class BluetoothScannerActivity extends Activity {
//private final BroadcastReceiver FoundReceiver = null;
protected ArrayList<BluetoothDevice> foundDevices = new ArrayList<BluetoothDevice>();
private ListView foundDevicesListView;
private ArrayAdapter<BluetoothDevice> btArrayAdapter;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_scanner);
final BluetoothAdapter myBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();
final Button scanb = (Button) findViewById(R.id.button);
final ListView foundDevicesListView = (ListView) findViewById(R.id.listView1);
btArrayAdapter = new ArrayAdapter<BluetoothDevice>(this,
android.R.layout.simple_list_item_1, foundDevices);
foundDevicesListView.setAdapter(btArrayAdapter);
//Turn on Bluetooth
if (myBlueToothAdapter == null)
Toast.makeText(BluetoothScannerActivity.this, "Your device doesnot support Bluetooth", Toast.LENGTH_LONG).show();
else if (!myBlueToothAdapter.isEnabled()) {
Intent BtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(BtIntent, 0);
Toast.makeText(BluetoothScannerActivity.this, "Turning on Bluetooth", Toast.LENGTH_LONG).show();
}
//scan
scanb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
btArrayAdapter.clear();
myBlueToothAdapter.startDiscovery();
Toast.makeText(BluetoothScannerActivity.this, "Scanning Devices", Toast.LENGTH_LONG).show();
}
});
registerReceiver(FoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
IntentFilter filter = new IntentFilter(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(FoundReceiver, filter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(FoundReceiver);
}
private final BroadcastReceiver FoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a new device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (!foundDevices.contains(device)) {
foundDevices.add(device);
btArrayAdapter.notifyDataSetChanged();
}
}
// When discovery cycle finished
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if (foundDevices == null || foundDevices.isEmpty()) {
Toast.makeText(BluetoothScannerActivity.this, "No Devices", Toast.LENGTH_LONG).show();
}
}
}
};
}
答案 0 :(得分:16)
您知道自Marshmallow
以来,您需要为您的任务提供这些权限 -
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
自Marshmallow
起,您必须以编程方式请求权限,即使您已在清单文件中声明了这些权限。
因此,您必须在startDiscovery()
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
用户接受这些权限后,您可以startDiscovery()
。如果他/她否认,你就无法发现这些设备。
您可以在
onRequestPermissionsResult()
回调。
答案 1 :(得分:5)
使用添加的权限,将最小SDk版本更改为23 - 它可以使用以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_blog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final EditText blogContentInput = (EditText) findViewById(R.id.blogContent);
String blogContent = null;
if (blogContentInput != null) {
blogContent = String.valueOf(blogContentInput.getText());
}else{
Log.e("Blog Content","BLog content not instantiated");
}
BlogDbHelper dbHelper = new BlogDbHelper(getApplicationContext());
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (fab != null) {
final String finalBlogContent = blogContent;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Content", "Blog content " + String.valueOf(blogContentInput.getText()));
ContentValues values = new ContentValues();
values.put(BlogContract.BlogEntry.BLOG_TITLE, "Practice_02");
values.put(BlogContract.BlogEntry.BLOG_AUTHOR, "MarkoVlaic");
values.put(BlogContract.BlogEntry.BLOG_CONTENT, String.valueOf(blogContentInput.getText()));
long newRowId;
newRowId = db.insertWithOnConflict(BlogContract.BlogEntry.TABLE_NAME, null, values,SQLiteDatabase.CONFLICT_REPLACE);
blogContentInput.setText("");
Snackbar.make(view, "You uploaded a blog", Snackbar.LENGTH_SHORT).show();
/*Intent intent = new Intent(view.getContext(),MainActivity.class);
startActivity(intent);*/
}
});
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
答案 2 :(得分:2)
以下方法仅在SDK版本为&gt;时执行。棒糖。当我尝试使用它没有这个限制我的应用程序崩溃。 只需在使用.startDiscovery();
之前调用此方法public void checkBTPermissions(){
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
int permissionCheck = this.checkSelfPermission("Manifest.permission.ACCESS_FINE_LOCATION");
permissionCheck += this.checkSelfPermission("Manifest.permission.ACCESS_COARSE_LOCATION");
if (permissionCheck != 0) {
this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1001); //Any number
}
}else{
Log.d(TAG, "checkBTPermissions: No need to check permissions. SDK version < LOLLIPOP.");
}
}