Android棉花糖6.0.1蓝牙扫描未返回结果
根据Kitkat 4.4.4的update+appCompat,6.01中的蓝牙似乎无法按预期工作,具有以下代码和权限 没有返回结果,我在附近发现了几个设备。 有人知道为什么吗?我正在运行Nexus5Android棉花糖6.0.1蓝牙扫描未返回结果,android,bluetooth,sdk,Android,Bluetooth,Sdk,根据Kitkat 4.4.4的update+appCompat,6.01中的蓝牙似乎无法按预期工作,具有以下代码和权限 没有返回结果,我在附近发现了几个设备。 有人知道为什么吗?我正在运行Nexus5 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<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();
}
}
}
};
}
包装蓝牙;
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类BluetoothScanActivity扩展活动{
//专用最终广播接收器FoundReceiver=null;
受保护的ArrayList foundDevices=新ArrayList();
私有ListView foundDevicesListView;
专用阵列适配器btArrayAdapter;
/**
*在首次创建活动时调用。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u蓝牙\u扫描仪);
final BluetoothAdapter myBlueToothAdapter=BluetoothAdapter.getDefaultAdapter();
最终按钮scanb=(按钮)findviewbyd(R.id.Button);
最终ListView foundDevicesListView=(ListView)findViewById(R.id.listView1);
btArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,foundDevices);
FoundDeviceListView.setAdapter(btArrayAdapter);
//打开蓝牙
if(myBlueToothAdapter==null)
Toast.makeText(BluetoothScannerActivity.this,“您的设备不支持蓝牙”,Toast.LENGTH\u LONG.show();
如果(!myBlueToothAdapter.isEnabled()),则为else{
意向BtIntent=新意向(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(BtIntent,0);
Toast.makeText(BluetoothScannerActivity.this,“打开蓝牙”,Toast.LENGTH_LONG.show();
}
//扫描
scanb.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
btArrayAdapter.clear();
myBlueToothAdapter.startDiscovery();
Toast.makeText(BluetoothScannerActivity.this,“扫描设备”,Toast.LENGTH_LONG.show();
}
});
registerReceiver(FoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));
IntentFilter筛选器=新IntentFilter(
BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
此注册表接收程序(FoundReceiver,filter);
}
@凌驾
受保护的空onDestroy(){
//TODO自动生成的方法存根
super.ondestory();
未注册接收人(FoundReceiver);
}
private final BroadcastReceiver FoundReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
//当发现发现发现新设备时
if(BluetoothDevice.ACTION_FOUND.equals(ACTION)){
//从Intent获取BluetoothDevice对象
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
如果(!foundDevices.contains(设备)){
foundDevices.add(设备);
btArrayAdapter.notifyDataSetChanged();
}
}
//发现周期结束时
if(BluetoothAdapter.ACTION\u DISCOVERY\u FINISHED.equals(ACTION)){
if(foundDevices==null | | foundDevices.isEmpty()){
Toast.makeText(BluetoothScannerActivity.this,“无设备”,Toast.LENGTH_LONG.show();
}
}
}
};
}
您知道,由于棉花糖
,您的任务需要这些权限-
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在用户接受这些权限后,您可以startDiscovery()
。如果他/她否认,你就无法发现这些设备。
您可以在中检查用户操作
onRequestPermissionsResult()
回调。添加权限后,将min SDk版本更改为23-它可用于以下功能:
package bluetoothscanneractivity;
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.support.v7.app.AppCompatActivity;
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 android.Manifest;
import java.util.ArrayList;
public class BluetoothScannerActivity extends AppCompatActivity {
//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 doesnt 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();
}
// Quick permission check
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
}
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);
Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show();
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();
}
}
}
};
}
package BluetoothScanGenerality;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.Manifest;
导入java.util.ArrayList;
公共类BluetoothScanActivity扩展了AppCompatActivity{
//专用最终广播接收器FoundReceiver=null;
受保护的ArrayList foundDevices=新ArrayList();
私有ListView foundDevicesListView;
专用阵列适配器btArrayAdapter;
/**
*在首次创建活动时调用。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u蓝牙\u扫描仪);
final BluetoothAdapter myBlueToothAdapter=BluetoothAdapter.getDefaultAdapter();
最终按钮scanb=(按钮)findviewbyd(R.id.Button);
最终ListView foundDevicesListView=(ListView)findViewById(R.id.listView1);
btArrayAdapter=新的ArrayAdapter(此,
安德罗
package bluetoothscanneractivity;
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.support.v7.app.AppCompatActivity;
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 android.Manifest;
import java.util.ArrayList;
public class BluetoothScannerActivity extends AppCompatActivity {
//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 doesnt 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();
}
// Quick permission check
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
}
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);
Toast.makeText(BluetoothScannerActivity.this, "name: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_LONG).show();
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();
}
}
}
};
}
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.");
}
}