Android 蓝牙Le Gatt未找到任何设备
回答:特别感谢下面的Nelson Hoang为我指引了正确的方向并提供了宝贵的资源。一旦添加了添加位置权限的代码(参见下面的Edit2),我就必须手动进入设置并打开应用程序的位置权限。一旦这样做了,它的工作非常好 问题:我正在尝试使用android studio中的标准蓝牙LE Gatt连接蓝牙LE设备。每次我都被迫将gradle从2.8更新到2.10。我已经在不同的项目上自动和手动完成了这项工作 然而,一旦我在我的Galaxy S7上下载了这个应用程序,它扫描并没有发现任何东西,尽管有3个蓝牙LE设备在它们旁边广播。我知道这不是我的S7的硬件问题,因为我使用了蓝果LE,能够拾取信号并连接良好。有什么解决办法吗 编辑:根据建议,我将在下面发布扫描代码。然而,这些都不是我写的。这是蓝牙LE示例代码,您可以从Android Studio访问 Edit2:为了授予应用程序定位权限,我将其添加到清单中:Android 蓝牙Le Gatt未找到任何设备,android,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth Lowenergy,Android Bluetooth,回答:特别感谢下面的Nelson Hoang为我指引了正确的方向并提供了宝贵的资源。一旦添加了添加位置权限的代码(参见下面的Edit2),我就必须手动进入设置并打开应用程序的位置权限。一旦这样做了,它的工作非常好 问题:我正在尝试使用android studio中的标准蓝牙LE Gatt连接蓝牙LE设备。每次我都被迫将gradle从2.8更新到2.10。我已经在不同的项目上自动和手动完成了这项工作 然而,一旦我在我的Galaxy S7上下载了这个应用程序,它扫描并没有发现任何东西,尽管有3个蓝牙
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
这是对普通蓝牙软件包的补充,但没有改变任何东西
package com.example.android.bluetoothlegatt;
import android.app.Activity;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Activity for scanning and displaying available Bluetooth LE devices.
*/
public class DeviceScanActivity extends ListActivity {
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.title_devices);
mHandler = new Handler();
// Use this check to determine whether BLE is supported on the device. Then you can
// selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// Initializes a Bluetooth adapter. For API level 18 and above, get a reference to
// BluetoothAdapter through BluetoothManager.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {
Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
return;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if (!mScanning) {
menu.findItem(R.id.menu_stop).setVisible(false);
menu.findItem(R.id.menu_scan).setVisible(true);
menu.findItem(R.id.menu_refresh).setActionView(null);
} else {
menu.findItem(R.id.menu_stop).setVisible(true);
menu.findItem(R.id.menu_scan).setVisible(false);
menu.findItem(R.id.menu_refresh).setActionView(
R.layout.actionbar_indeterminate_progress);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_scan:
mLeDeviceListAdapter.clear();
scanLeDevice(true);
break;
case R.id.menu_stop:
scanLeDevice(false);
break;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (!mBluetoothAdapter.isEnabled()) {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
// Initializes list view adapter.
mLeDeviceListAdapter = new LeDeviceListAdapter();
setListAdapter(mLeDeviceListAdapter);
scanLeDevice(true);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onPause() {
super.onPause();
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
final Intent intent = new Intent(this, DeviceControlActivity.class);
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
if (mScanning) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
mScanning = false;
}
startActivity(intent);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = DeviceScanActivity.this.getLayoutInflater();
}
public void addDevice(BluetoothDevice device) {
if(!mLeDevices.contains(device)) {
mLeDevices.add(device);
}
}
public BluetoothDevice getDevice(int position) {
return mLeDevices.get(position);
}
public void clear() {
mLeDevices.clear();
}
@Override
public int getCount() {
return mLeDevices.size();
}
@Override
public Object getItem(int i) {
return mLeDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
// General ListView optimization code.
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(i);
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
viewHolder.deviceAddress.setText(device.getAddress());
return view;
}
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
static class ViewHolder {
TextView deviceName;
TextView deviceAddress;
}
}
package com.example.android.bluetoothlegatt;
导入android.app.Activity;
导入android.app.ListActivity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.bluetooth.BluetoothManager;
导入android.content.Context;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.os.Bundle;
导入android.os.Handler;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
/**
*用于扫描和显示可用蓝牙设备的活动。
*/
公共类DeviceScanActivity扩展了ListActivity{
私人LEDEVICELISTAPTER MLEDVICELISTAPTER;
私人蓝牙适配器mBluetoothAdapter;
私有布尔扫描;
私人经理人;
私有静态最终整数请求_ENABLE_BT=1;
//10秒后停止扫描。
专用静态最终长扫描周期=10000;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getActionBar().setTitle(R.string.title_设备);
mHandler=新处理程序();
//使用此检查确定设备上是否支持BLE。然后您可以
//有选择地禁用可编程逻辑相关功能。
如果(!getPackageManager().hasSystemFeature(PackageManager.FEATURE\u BLUETOOTH\u LE)){
Toast.makeText(不支持这个,R.string.ble_,Toast.LENGTH_SHORT).show();
完成();
}
//初始化蓝牙适配器。对于API级别18及以上,请获取对的参考
//BluetoothManager中的BluetoothAdapter。
最终BluetoothManager BluetoothManager=
(BluetoothManager)getSystemService(Context.BLUETOOTH\u服务);
mBluetoothAdapter=bluetoothManager.getAdapter();
//检查设备上是否支持蓝牙。
if(mBluetoothAdapter==null){
Toast.makeText(this,R.string.error\u bluetooth\u不受支持,Toast.LENGTH\u SHORT).show();
完成();
返回;
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.main,menu);
如果(!mScanning){
menu.findItem(R.id.menu\u stop).setVisible(false);
menu.findItem(R.id.menu\u scan).setVisible(true);
menu.findItem(R.id.menu_refresh).setActionView(null);
}否则{
menu.findItem(R.id.menu\u stop).setVisible(true);
menu.findItem(R.id.menu\u scan).setVisible(false);
menu.findItem(R.id.menu\u刷新).setActionView(
R.布局、操作栏(不确定的进度);
}
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.菜单\U扫描:
mLeDeviceListAdapter.clear();
扫描设备(真实);
打破
案例R.id.菜单\停止:
扫描设备(假);
打破
}
返回true;
}
@凌驾
受保护的void onResume(){
super.onResume();
//确保在设备上启用蓝牙。如果当前未启用蓝牙,
//激发显示对话框的意图,要求用户授予启用该对话框的权限。
如果(!mBluetoothAdapter.isEnabled()){
如果(!mBluetoothAdapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
}
//初始化列表视图适配器。
mLeDeviceListAdapter=新的LeDeviceListAdapter();
setListAdapter(mLeDeviceListAdapter);
扫描设备(真实);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//用户选择不启用蓝牙。
if(requestCode==请求\启用\ BT&&resultCode==活动。结果\取消){
完成();
返回;
}
super.onActivityResult(请求代码、结果代码、数据);
}
@凌驾
受保护的void onPause(){
super.onPause();
扫描设备(假);
mLeDeviceListAdapter.clear();
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
最终的
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
import android.Manifest;
import android.os.Build;
import android.util.Log;
private static int PERMISSION_REQUEST_CODE = 1;
private final static String TAG = DeviceScanActivity.class.getSimpleName();
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
if(requestCode == PERMISSION_REQUEST_CODE)
{
//Do something based on grantResults
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
Log.d(TAG, "coarse location permission granted");
}
else
{
Log.d(TAG, "coarse location permission denied");
}
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "Request Location Permissions:");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_CODE);
}
...